[Bugfix][CLI] Include inherited field docstrings in get_attr_docs (#49821)
Signed-off-by: Chenglun Hu <chenglunhu@gmail.com> Signed-off-by: hclsys <chenglunhu@gmail.com> Signed-off-by: Harry Mellor <19981378+hmellor@users.noreply.github.com> Co-authored-by: Harry Mellor <19981378+hmellor@users.noreply.github.com>
This commit is contained in:
+22
-5
@@ -158,17 +158,20 @@ def getattr_iter(
|
||||
return default_factory() if default_factory is not None else default
|
||||
|
||||
|
||||
def get_attr_docs(cls: type[Any]) -> dict[str, str]:
|
||||
def _get_own_attr_docs(cls: type[Any], out: dict[str, str]) -> None:
|
||||
"""Get any docstrings placed after attribute assignments in a class body.
|
||||
|
||||
https://davidism.com/mit-license/
|
||||
"""
|
||||
cls_node = ast.parse(textwrap.dedent(inspect.getsource(cls))).body[0]
|
||||
try:
|
||||
source = inspect.getsource(cls)
|
||||
except (OSError, TypeError):
|
||||
# ``object`` and classes without retrievable source (e.g. builtins).
|
||||
return
|
||||
|
||||
cls_node = ast.parse(textwrap.dedent(source)).body[0]
|
||||
if not isinstance(cls_node, ast.ClassDef):
|
||||
raise TypeError("Given object was not a class.")
|
||||
|
||||
out = {}
|
||||
return
|
||||
|
||||
# Consider each pair of nodes.
|
||||
for a, b in pairwise(cls_node.body):
|
||||
@@ -194,6 +197,20 @@ def get_attr_docs(cls: type[Any]) -> dict[str, str]:
|
||||
|
||||
out[target.id] = doc
|
||||
|
||||
|
||||
def get_attr_docs(cls: type[Any]) -> dict[str, str]:
|
||||
"""Get any docstrings placed after attribute assignments in a class body or
|
||||
its bases."""
|
||||
if not isinstance(cls, type):
|
||||
raise TypeError("Given object was not a class.")
|
||||
|
||||
out: dict[str, str] = {}
|
||||
|
||||
# Walk the MRO from the most-base class to ``cls`` so that a docstring
|
||||
# redefined on a subclass overrides the one inherited from a base class.
|
||||
for base in reversed(cls.__mro__):
|
||||
_get_own_attr_docs(base, out)
|
||||
|
||||
return out
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user