Skip to content

Commit d9f6eeb

Browse files
committed
Lookup instance attibutes on obj in MRO
Instance attirbutes on objects defined in other modules cannot be found by module analyzer unless we import that module. So try looking for instance attirbutes on objects in MRO order
1 parent 1539888 commit d9f6eeb

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

sphinx/ext/autosummary/__init__.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
from sphinx.util import logging, rst
8383
from sphinx.util.docutils import (NullReporter, SphinxDirective, SphinxRole, new_document,
8484
switch_source_input)
85-
from sphinx.util.inspect import signature_from_str
85+
from sphinx.util.inspect import getmro, signature_from_str
8686
from sphinx.util.matching import Matcher
8787
from sphinx.util.typing import OptionSpec
8888
from sphinx.writers.html import HTMLTranslator
@@ -712,15 +712,20 @@ def import_ivar_by_name(name: str, prefixes: List[str] = [None],
712712
try:
713713
name, attr = name.rsplit(".", 1)
714714
real_name, obj, parent, modname = import_by_name(name, prefixes, grouped_exception)
715-
qualname = real_name.replace(modname + ".", "")
716-
analyzer = ModuleAnalyzer.for_module(getattr(obj, '__module__', modname))
717-
analyzer.analyze()
718-
# check for presence in `annotations` to include dataclass attributes
719-
attrs_found = {name_attr[1] for name_attr in analyzer.attr_docs} | {
720-
name_attr[1] for name_attr in analyzer.annotations
721-
}
722-
if attr in attrs_found:
723-
return real_name + "." + attr, INSTANCEATTR, obj, modname
715+
716+
candidate_objects = getmro(obj)
717+
if len(candidate_objects) == 0:
718+
candidate_objects = (obj,)
719+
720+
for candidate_obj in candidate_objects:
721+
analyzer = ModuleAnalyzer.for_module(getattr(candidate_obj, '__module__', modname))
722+
analyzer.analyze()
723+
# check for presence in `annotations` to include dataclass attributes
724+
attrs_found = {name_attr[1] for name_attr in analyzer.attr_docs} | {
725+
name_attr[1] for name_attr in analyzer.annotations
726+
}
727+
if attr in attrs_found:
728+
return real_name + "." + attr, INSTANCEATTR, obj, modname
724729
except (ImportError, ValueError, PycodeError) as exc:
725730
raise ImportError from exc
726731
except ImportExceptionGroup:

0 commit comments

Comments
 (0)