Skip to content

Commit

Permalink
Lookup instance attibutes on obj in MRO
Browse files Browse the repository at this point in the history
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
  • Loading branch information
jenshnielsen committed Jul 22, 2022
1 parent 8b9a3ed commit b3f24ed
Showing 1 changed file with 15 additions and 10 deletions.
25 changes: 15 additions & 10 deletions sphinx/ext/autosummary/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
from sphinx.util import logging, rst
from sphinx.util.docutils import (NullReporter, SphinxDirective, SphinxRole, new_document,
switch_source_input)
from sphinx.util.inspect import signature_from_str
from sphinx.util.inspect import getmro, signature_from_str
from sphinx.util.matching import Matcher
from sphinx.util.typing import OptionSpec
from sphinx.writers.html import HTMLTranslator
Expand Down Expand Up @@ -711,15 +711,20 @@ def import_ivar_by_name(name: str, prefixes: List[str] = [None],
try:
name, attr = name.rsplit(".", 1)
real_name, obj, parent, modname = import_by_name(name, prefixes, grouped_exception)
qualname = real_name.replace(modname + ".", "")
analyzer = ModuleAnalyzer.for_module(getattr(obj, '__module__', modname))
analyzer.analyze()
# check for presence in `annotations` to include dataclass attributes
attrs_found = {name_attr[1] for name_attr in analyzer.attr_docs} | {
name_attr[1] for name_attr in analyzer.annotations
}
if attr in attrs_found:
return real_name + "." + attr, INSTANCEATTR, obj, modname

candidate_objects = getmro(obj)
if len(candidate_objects) == 0:
candidate_objects = (obj,)

for candidate_obj in candidate_objects:
analyzer = ModuleAnalyzer.for_module(getattr(candidate_obj, '__module__', modname))
analyzer.analyze()
# check for presence in `annotations` to include dataclass attributes
attrs_found = {name_attr[1] for name_attr in analyzer.attr_docs} | {
name_attr[1] for name_attr in analyzer.annotations
}
if attr in attrs_found:
return real_name + "." + attr, INSTANCEATTR, obj, modname
except (ImportError, ValueError, PycodeError) as exc:
raise ImportError from exc
except ImportExceptionGroup:
Expand Down

0 comments on commit b3f24ed

Please sign in to comment.