Fix documenting inherited attributes

This commit is contained in:
Adam Turner 2022-06-08 15:45:27 +01:00
parent 60775ec4c4
commit 3956cf2249
2 changed files with 9 additions and 3 deletions

View File

@ -1670,7 +1670,8 @@ class ClassDocumenter(DocstringSignatureMixin, ModuleLevelDocumenter): # type:
self.add_line(' ' + _('Bases: %s') % ', '.join(base_classes), sourcename) self.add_line(' ' + _('Bases: %s') % ', '.join(base_classes), sourcename)
def get_object_members(self, want_all: bool) -> Tuple[bool, ObjectMembers]: 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 want_all:
if not self.options.members: if not self.options.members:
return False, [] # type: ignore return False, [] # type: ignore

View File

@ -205,8 +205,8 @@ def get_object_members(subject: Any, objpath: List[str], attrgetter: Callable,
return members return members
def get_class_members(subject: Any, objpath: List[str], attrgetter: Callable def get_class_members(subject: Any, objpath: List[str], attrgetter: Callable,
) -> Dict[str, "ObjectMember"]: inherit_docstrings: bool = True) -> Dict[str, "ObjectMember"]:
"""Get members and attributes of target class.""" """Get members and attributes of target class."""
from sphinx.ext.autodoc import INSTANCEATTR, ObjectMember from sphinx.ext.autodoc import INSTANCEATTR, ObjectMember
@ -290,6 +290,11 @@ def get_class_members(subject: Any, objpath: List[str], attrgetter: Callable
elif (ns == qualname and docstring and elif (ns == qualname and docstring and
isinstance(members[name], ObjectMember) and isinstance(members[name], ObjectMember) and
not members[name].docstring): 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. # attribute is already known, because dir(subject) enumerates it.
# But it has no docstring yet # But it has no docstring yet
members[name].docstring = '\n'.join(docstring) members[name].docstring = '\n'.join(docstring)