MCPcopy
hub / github.com/pydantic/pydantic / extract_docstrings_from_cls

Function extract_docstrings_from_cls

pydantic/_internal/_docs_extraction.py:82–113  ·  view source on GitHub ↗

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 n

(cls: type[Any], use_inspect: bool = False)

Source from the content-addressed store, hash-verified

80
81
82def extract_docstrings_from_cls(cls: type[Any], use_inspect: bool = False) -> dict[str, str]:
83 """Map model attributes and their corresponding docstring.
84
85 Args:
86 cls: The class of the Pydantic model to inspect.
87 use_inspect: Whether to skip usage of frames to find the object and use
88 the `inspect` module instead.
89
90 Returns:
91 A mapping containing attribute names and their corresponding docstring.
92 """
93 if use_inspect or sys.version_info >= (3, 13):
94 # On Python < 3.13, `inspect.getsourcelines()` might not work as expected
95 # if two classes have the same name in the same source file.
96 # On Python 3.13+, it will use the new `__firstlineno__` class attribute,
97 # making it way more robust.
98 try:
99 source, _ = inspect.getsourcelines(cls)
100 except OSError: # pragma: no cover
101 return {}
102 else:
103 # TODO remove this implementation when we drop support for Python 3.12:
104 source = _extract_source_from_frame(cls)
105
106 if not source:
107 return {}
108
109 dedent_source = _dedent_source_lines(source)
110
111 visitor = DocstringVisitor()
112 visitor.visit(ast.parse(dedent_source))
113 return visitor.attrs

Callers 2

_typed_dict_schemaMethod · 0.85

Calls 4

visitMethod · 0.95
_dedent_source_linesFunction · 0.85
DocstringVisitorClass · 0.85

Tested by

no test coverage detected