1"""Utilities related to attribute docstring extraction."""
2
3from __future__ import annotations
4
5import ast
6import inspect
7import textwrap
8from typing import Any
9
10
11class DocstringVisitor(ast.NodeVisitor):
12 def __init__(self) -> None:
13 super().__init__()
14
15 self.target: str | None = None
16 self.attrs: dict[str, str] = {}
17 self.previous_node_type: type[ast.AST] | None = None
18
19 def visit(self, node: ast.AST) -> Any:
20 node_result = super().visit(node)
21 self.previous_node_type = type(node)
22 return node_result
23
24 def visit_AnnAssign(self, node: ast.AnnAssign) -> Any:
25 if isinstance(node.target, ast.Name):
26 self.target = node.target.id
27
28 def visit_Expr(self, node: ast.Expr) -> Any:
29 if (
30 isinstance(node.value, ast.Constant)
31 and isinstance(node.value.value, str)
32 and self.previous_node_type is ast.AnnAssign
33 ):
34 docstring = inspect.cleandoc(node.value.value)
35 if self.target:
36 self.attrs[self.target] = docstring
37 self.target = None
38
39
40def _dedent_source_lines(source: list[str]) -> str:
41 # Required for nested class definitions, e.g. in a function block
42 dedent_source = textwrap.dedent(''.join(source))
43 if dedent_source.startswith((' ', '\t')):
44 # We are in the case where there's a dedented (usually multiline) string
45 # at a lower indentation level than the class itself. We wrap our class
46 # in a function as a workaround.
47 dedent_source = f'def dedent_workaround():\n{dedent_source}'
48 return dedent_source
49
50
51def _extract_source_from_frame(cls: type[Any]) -> list[str] | None:
52 frame = inspect.currentframe()
53
54 while frame:
55 if inspect.getmodule(frame) is inspect.getmodule(cls):
56 lnum = frame.f_lineno
57 try:
58 lines, _ = inspect.findsource(frame)
59 except OSError: # pragma: no cover
60 # Source can't be retrieved (maybe because running in an interactive terminal),
61 # we don't want to error here.
62 pass
63 else:
64 block_lines = inspect.getblock(lines[lnum - 1 :])
65 dedent_source = _dedent_source_lines(block_lines)
66 try:
67 block_tree = ast.parse(dedent_source)
68 except SyntaxError:
69 pass
70 else:
71 stmt = block_tree.body[0]
72 if isinstance(stmt, ast.FunctionDef) and stmt.name == 'dedent_workaround':
73 # `_dedent_source_lines` wrapped the class around the workaround function
74 stmt = stmt.body[0]
75 if isinstance(stmt, ast.ClassDef) and stmt.name == cls.__name__:
76 return block_lines
77
78 frame = frame.f_back
79
80
81def extract_docstrings_from_cls(cls: type[Any], use_inspect: bool = False) -> dict[str, str]:
82 """Map model attributes and their corresponding docstring.
83
84 Args:
85 cls: The class of the Pydantic model to inspect.
86 use_inspect: Whether to skip usage of frames to find the object and use
87 the `inspect` module instead.
88
89 Returns:
90 A mapping containing attribute names and their corresponding docstring.
91 """
92 if use_inspect:
93 # Might not work as expected if two classes have the same name in the same source file.
94 try:
95 source, _ = inspect.getsourcelines(cls)
96 except OSError: # pragma: no cover
97 return {}
98 else:
99 source = _extract_source_from_frame(cls)
100
101 if not source:
102 return {}
103
104 dedent_source = _dedent_source_lines(source)
105
106 visitor = DocstringVisitor()
107 visitor.visit(ast.parse(dedent_source))
108 return visitor.attrs