Skip to content

Commit 1f860cb

Browse files
authored
Merge pull request #9146 from pbudzyns/autosummary-iattr-include
Autosummary to include instance attributes
2 parents 77da494 + abbd005 commit 1f860cb

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

sphinx/ext/autosummary/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -662,8 +662,10 @@ def import_ivar_by_name(name: str, prefixes: List[str] = [None]) -> Tuple[str, A
662662
name, attr = name.rsplit(".", 1)
663663
real_name, obj, parent, modname = import_by_name(name, prefixes)
664664
qualname = real_name.replace(modname + ".", "")
665-
analyzer = ModuleAnalyzer.for_module(modname)
666-
if (qualname, attr) in analyzer.find_attr_docs():
665+
analyzer = ModuleAnalyzer.for_module(getattr(obj, '__module__', modname))
666+
analyzer.analyze()
667+
# check for presence in `annotations` to include dataclass attributes
668+
if (qualname, attr) in analyzer.attr_docs or (qualname, attr) in analyzer.annotations:
667669
return real_name + "." + attr, INSTANCEATTR, obj, modname
668670
except (ImportError, ValueError, PycodeError):
669671
pass

sphinx/ext/autosummary/generate.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,15 +239,33 @@ def skip_member(obj: Any, name: str, objtype: str) -> bool:
239239
name, exc, type='autosummary')
240240
return False
241241

242-
def get_members(obj: Any, types: Set[str], include_public: List[str] = [],
243-
imported: bool = True) -> Tuple[List[str], List[str]]:
244-
items: List[str] = []
245-
public: List[str] = []
242+
def get_class_members(obj: Any) -> Dict[str, Any]:
243+
members = sphinx.ext.autodoc.get_class_members(obj, [qualname], safe_getattr)
244+
return {name: member.object for name, member in members.items()}
245+
246+
def get_module_members(obj: Any) -> Dict[str, Any]:
247+
members = {}
246248
for name in dir(obj):
247249
try:
248-
value = safe_getattr(obj, name)
250+
members[name] = safe_getattr(obj, name)
249251
except AttributeError:
250252
continue
253+
return members
254+
255+
def get_all_members(obj: Any) -> Dict[str, Any]:
256+
if doc.objtype == "module":
257+
return get_module_members(obj)
258+
elif doc.objtype == "class":
259+
return get_class_members(obj)
260+
return {}
261+
262+
def get_members(obj: Any, types: Set[str], include_public: List[str] = [],
263+
imported: bool = True) -> Tuple[List[str], List[str]]:
264+
items: List[str] = []
265+
public: List[str] = []
266+
267+
all_members = get_all_members(obj)
268+
for name, value in all_members.items():
251269
documenter = get_documenter(app, value, obj)
252270
if documenter.objtype in types:
253271
# skip imported members if expected

tests/roots/test-autosummary/dummy_module.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
44
module_attr
55
C.class_attr
6+
C.instance_attr
67
C.prop_attr1
78
C.prop_attr2
89
C.C2
@@ -51,6 +52,12 @@ class C:
5152
#: value is integer.
5253
class_attr = 42
5354

55+
def __init__(self):
56+
#: This is an instance attribute
57+
#:
58+
#: value is a string
59+
self.instance_attr = "42"
60+
5461
def _prop_attr_get(self):
5562
"""
5663
This is a function docstring

tests/test_ext_autosummary.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ def handler(app, what, name, obj, options, lines):
161161
'emptyLine': "This is the real summary",
162162
'module_attr': 'This is a module attribute',
163163
'C.class_attr': 'This is a class attribute',
164+
'C.instance_attr': 'This is an instance attribute',
164165
'C.prop_attr1': 'This is a function docstring',
165166
'C.prop_attr2': 'This is a attribute docstring',
166167
'C.C2': 'This is a nested inner class docstring',
@@ -329,6 +330,7 @@ def test_autosummary_generate(app, status, warning):
329330
' ~Foo.CONSTANT3\n'
330331
' ~Foo.CONSTANT4\n'
331332
' ~Foo.baz\n'
333+
' ~Foo.value\n'
332334
' \n' in Foo)
333335

334336
FooBar = (app.srcdir / 'generated' / 'autosummary_dummy_module.Foo.Bar.rst').read_text()

0 commit comments

Comments
 (0)