Skip to content

Commit c1a4c45

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 8b9a3ed commit c1a4c45

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
@@ -81,7 +81,7 @@
8181
from sphinx.util import logging, rst
8282
from sphinx.util.docutils import (NullReporter, SphinxDirective, SphinxRole, new_document,
8383
switch_source_input)
84-
from sphinx.util.inspect import signature_from_str
84+
from sphinx.util.inspect import signature_from_str, getmro
8585
from sphinx.util.matching import Matcher
8686
from sphinx.util.typing import OptionSpec
8787
from sphinx.writers.html import HTMLTranslator
@@ -711,15 +711,20 @@ def import_ivar_by_name(name: str, prefixes: List[str] = [None],
711711
try:
712712
name, attr = name.rsplit(".", 1)
713713
real_name, obj, parent, modname = import_by_name(name, prefixes, grouped_exception)
714-
qualname = real_name.replace(modname + ".", "")
715-
analyzer = ModuleAnalyzer.for_module(getattr(obj, '__module__', modname))
716-
analyzer.analyze()
717-
# check for presence in `annotations` to include dataclass attributes
718-
attrs_found = {name_attr[1] for name_attr in analyzer.attr_docs} | {
719-
name_attr[1] for name_attr in analyzer.annotations
720-
}
721-
if attr in attrs_found:
722-
return real_name + "." + attr, INSTANCEATTR, obj, modname
714+
715+
candidate_objects = getmro(obj)
716+
if len(candidate_objects) == 0:
717+
candidate_objects = (obj,)
718+
719+
for candidate_obj in candidate_objects:
720+
analyzer = ModuleAnalyzer.for_module(getattr(candidate_obj, '__module__', modname))
721+
analyzer.analyze()
722+
# check for presence in `annotations` to include dataclass attributes
723+
attrs_found = {name_attr[1] for name_attr in analyzer.attr_docs} | {
724+
name_attr[1] for name_attr in analyzer.annotations
725+
}
726+
if attr in attrs_found:
727+
return real_name + "." + attr, INSTANCEATTR, obj, modname
723728
except (ImportError, ValueError, PycodeError) as exc:
724729
raise ImportError from exc
725730
except ImportExceptionGroup:

0 commit comments

Comments
 (0)