Skip to content

Commit

Permalink
Fix #9866: autodoc: doccoment for the imported class was ignored
Browse files Browse the repository at this point in the history
Autodoc tried to scan doccomment on the module where the class defined.
But it failed to get it if the class is imported from other module.

This analyzes the target module to obtain the doccomment.
  • Loading branch information
tk0miya committed Nov 21, 2021
1 parent 6473141 commit fb92547
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -19,6 +19,7 @@ Features added
Bugs fixed
----------

* #9866: autodoc: doccoment for the imported class was ignored
* #9857: Generated RFC links use outdated base url

Testing
Expand Down
12 changes: 10 additions & 2 deletions sphinx/ext/autodoc/__init__.py
Expand Up @@ -1743,14 +1743,22 @@ def get_doc(self, ignore: int = None) -> Optional[List[List[str]]]:
def get_variable_comment(self) -> Optional[List[str]]:
try:
key = ('', '.'.join(self.objpath))
analyzer = ModuleAnalyzer.for_module(self.get_real_modname())
if self.doc_as_attr:
analyzer = ModuleAnalyzer.for_module(self.modname)
else:
analyzer = ModuleAnalyzer.for_module(self.get_real_modname())
analyzer.analyze()
return list(self.analyzer.attr_docs.get(key, []))
return list(analyzer.attr_docs.get(key, []))
except PycodeError:
return None

def add_content(self, more_content: Optional[StringList], no_docstring: bool = False
) -> None:
if self.doc_as_attr and self.modname != self.get_real_modname():
# override analyzer to obtain doccomment around its definition.
self.analyzer = ModuleAnalyzer.for_module(self.modname)

This comment has been minimized.

Copy link
@lavaux

lavaux Jan 30, 2022

The call to for_module is not guarded by any try...except PycodeError. This causes failure in some cases when the documentation is built from native binary module instead of python modules.

This comment has been minimized.

Copy link
@tk0miya

tk0miya Jan 30, 2022

Author Member

You're right. I'll post a fix soon.

self.analyzer.analyze()

if self.doc_as_attr and not self.get_variable_comment():
try:
more_content = StringList([_('alias of %s') % restify(self.object)], source='')
Expand Down
3 changes: 3 additions & 0 deletions tests/roots/test-ext-autodoc/target/classes.py
Expand Up @@ -37,3 +37,6 @@ class Corge(Quux):

#: docstring
OtherAlias = Bar

#: docstring
IntAlias = int
5 changes: 4 additions & 1 deletion tests/test_ext_autodoc.py
Expand Up @@ -1870,12 +1870,15 @@ def test_autodoc_GenericAlias(app):
' .. py:attribute:: Class.T',
' :module: target.genericalias',
'',
' A list of int',
'',
' alias of :py:class:`~typing.List`\\ [:py:class:`int`]',
'',
'.. py:attribute:: T',
' :module: target.genericalias',
'',
' alias of :py:class:`~typing.List`\\ [:py:class:`int`]',
' A list of int',
'',
]
else:
assert list(actual) == [
Expand Down
12 changes: 12 additions & 0 deletions tests/test_ext_autodoc_autoclass.py
Expand Up @@ -407,6 +407,18 @@ def test_class_alias_having_doccomment(app):
]


def test_class_alias_for_imported_object_having_doccomment(app):
actual = do_autodoc(app, 'class', 'target.classes.IntAlias')
assert list(actual) == [
'',
'.. py:attribute:: IntAlias',
' :module: target.classes',
'',
' docstring',
'',
]


@pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_coroutine(app):
options = {"members": None}
Expand Down

0 comments on commit fb92547

Please sign in to comment.