Skip to content

Commit

Permalink
Fix documenting inherited attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
AA-Turner committed Jun 8, 2022
1 parent 60775ec commit f0ffdc9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
3 changes: 2 additions & 1 deletion sphinx/ext/autodoc/__init__.py
Expand Up @@ -1670,7 +1670,8 @@ def add_directive_header(self, sig: str) -> None:
self.add_line(' ' + _('Bases: %s') % ', '.join(base_classes), sourcename)

def get_object_members(self, want_all: bool) -> Tuple[bool, ObjectMembers]:
members = get_class_members(self.object, self.objpath, self.get_attr)
members = get_class_members(self.object, self.objpath, self.get_attr,
self.config.autodoc_inherit_docstrings)
if not want_all:
if not self.options.members:
return False, [] # type: ignore
Expand Down
9 changes: 7 additions & 2 deletions sphinx/ext/autodoc/importer.py
Expand Up @@ -205,8 +205,8 @@ def get_object_members(subject: Any, objpath: List[str], attrgetter: Callable,
return members


def get_class_members(subject: Any, objpath: List[str], attrgetter: Callable
) -> Dict[str, "ObjectMember"]:
def get_class_members(subject: Any, objpath: List[str], attrgetter: Callable,
inherit_docstrings: bool) -> Dict[str, "ObjectMember"]:
"""Get members and attributes of target class."""
from sphinx.ext.autodoc import INSTANCEATTR, ObjectMember

Expand Down Expand Up @@ -290,6 +290,11 @@ def get_class_members(subject: Any, objpath: List[str], attrgetter: Callable
elif (ns == qualname and docstring and
isinstance(members[name], ObjectMember) and
not members[name].docstring):
if cls != subject and not inherit_docstrings:
# If we are in the MRO of the class and not the class itself,
# and we do not want to inherit docstrings, then skip setting
# the docstring below
continue
# attribute is already known, because dir(subject) enumerates it.
# But it has no docstring yet
members[name].docstring = '\n'.join(docstring)
Expand Down
2 changes: 1 addition & 1 deletion sphinx/ext/autosummary/generate.py
Expand Up @@ -226,7 +226,7 @@ def skip_member(obj: Any, name: str, objtype: str) -> bool:
return False

def get_class_members(obj: Any) -> Dict[str, Any]:
members = sphinx.ext.autodoc.get_class_members(obj, [qualname], safe_getattr)
members = sphinx.ext.autodoc.get_class_members(obj, [qualname], safe_getattr, True)
return {name: member.object for name, member in members.items()}

def get_module_members(obj: Any) -> Dict[str, Any]:
Expand Down

0 comments on commit f0ffdc9

Please sign in to comment.