Skip to content

Commit

Permalink
Merge pull request #9880 from tk0miya/9879_invalid_doc_attribute
Browse files Browse the repository at this point in the history
Fix #9879: autodoc: AttributeError for object having invalid __doc__
  • Loading branch information
tk0miya committed Nov 22, 2021
2 parents e5424b3 + 80f8426 commit 55e8a3a
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Expand Up @@ -18,6 +18,8 @@ Bugs fixed

* #9838: autodoc: AttributeError is raised on building document for functions
decorated by functools.lru_cache
* #9879: autodoc: AttributeError is raised on building document for an object
having invalid __doc__ atribute

Testing
--------
Expand Down
2 changes: 1 addition & 1 deletion sphinx/ext/autodoc/__init__.py
Expand Up @@ -1704,7 +1704,7 @@ def get_doc(self, ignore: int = None) -> Optional[List[List[str]]]:
classdoc_from = self.options.get('class-doc-from', self.config.autoclass_content)

docstrings = []
attrdocstring = self.get_attr(self.object, '__doc__', None)
attrdocstring = getdoc(self.object, self.get_attr)
if attrdocstring:
docstrings.append(attrdocstring)

Expand Down
11 changes: 9 additions & 2 deletions sphinx/util/inspect.py
Expand Up @@ -871,6 +871,13 @@ def getdoc(obj: Any, attrgetter: Callable = safe_getattr,
* inherited docstring
* inherited decorated methods
"""
def getdoc_internal(obj: Any, attrgetter: Callable = safe_getattr) -> Optional[str]:
doc = attrgetter(obj, '__doc__', None)
if isinstance(doc, str):
return doc
else:
return None

if cls and name and isclassmethod(obj, cls, name):
for basecls in getmro(cls):
meth = basecls.__dict__.get(name)
Expand All @@ -879,7 +886,7 @@ def getdoc(obj: Any, attrgetter: Callable = safe_getattr,
if doc is not None or not allow_inherited:
return doc

doc = attrgetter(obj, '__doc__', None)
doc = getdoc_internal(obj)
if ispartial(obj) and doc == obj.__class__.__doc__:
return getdoc(obj.func)
elif doc is None and allow_inherited:
Expand All @@ -888,7 +895,7 @@ def getdoc(obj: Any, attrgetter: Callable = safe_getattr,
for basecls in getmro(cls):
meth = safe_getattr(basecls, name, None)
if meth is not None:
doc = attrgetter(meth, '__doc__', None)
doc = getdoc_internal(meth)
if doc is not None:
break

Expand Down

0 comments on commit 55e8a3a

Please sign in to comment.