mirror of
https://github.com/sphinx-doc/sphinx.git
synced 2025-02-25 18:55:22 -06:00
Merge pull request #9146 from pbudzyns/autosummary-iattr-include
Autosummary to include instance attributes
This commit is contained in:
@@ -662,8 +662,10 @@ def import_ivar_by_name(name: str, prefixes: List[str] = [None]) -> Tuple[str, A
|
||||
name, attr = name.rsplit(".", 1)
|
||||
real_name, obj, parent, modname = import_by_name(name, prefixes)
|
||||
qualname = real_name.replace(modname + ".", "")
|
||||
analyzer = ModuleAnalyzer.for_module(modname)
|
||||
if (qualname, attr) in analyzer.find_attr_docs():
|
||||
analyzer = ModuleAnalyzer.for_module(getattr(obj, '__module__', modname))
|
||||
analyzer.analyze()
|
||||
# check for presence in `annotations` to include dataclass attributes
|
||||
if (qualname, attr) in analyzer.attr_docs or (qualname, attr) in analyzer.annotations:
|
||||
return real_name + "." + attr, INSTANCEATTR, obj, modname
|
||||
except (ImportError, ValueError, PycodeError):
|
||||
pass
|
||||
|
||||
@@ -239,15 +239,33 @@ def generate_autosummary_content(name: str, obj: Any, parent: Any,
|
||||
name, exc, type='autosummary')
|
||||
return False
|
||||
|
||||
def get_class_members(obj: Any) -> Dict[str, Any]:
|
||||
members = sphinx.ext.autodoc.get_class_members(obj, [qualname], safe_getattr)
|
||||
return {name: member.object for name, member in members.items()}
|
||||
|
||||
def get_module_members(obj: Any) -> Dict[str, Any]:
|
||||
members = {}
|
||||
for name in dir(obj):
|
||||
try:
|
||||
members[name] = safe_getattr(obj, name)
|
||||
except AttributeError:
|
||||
continue
|
||||
return members
|
||||
|
||||
def get_all_members(obj: Any) -> Dict[str, Any]:
|
||||
if doc.objtype == "module":
|
||||
return get_module_members(obj)
|
||||
elif doc.objtype == "class":
|
||||
return get_class_members(obj)
|
||||
return {}
|
||||
|
||||
def get_members(obj: Any, types: Set[str], include_public: List[str] = [],
|
||||
imported: bool = True) -> Tuple[List[str], List[str]]:
|
||||
items: List[str] = []
|
||||
public: List[str] = []
|
||||
for name in dir(obj):
|
||||
try:
|
||||
value = safe_getattr(obj, name)
|
||||
except AttributeError:
|
||||
continue
|
||||
|
||||
all_members = get_all_members(obj)
|
||||
for name, value in all_members.items():
|
||||
documenter = get_documenter(app, value, obj)
|
||||
if documenter.objtype in types:
|
||||
# skip imported members if expected
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
module_attr
|
||||
C.class_attr
|
||||
C.instance_attr
|
||||
C.prop_attr1
|
||||
C.prop_attr2
|
||||
C.C2
|
||||
@@ -51,6 +52,12 @@ class C:
|
||||
#: value is integer.
|
||||
class_attr = 42
|
||||
|
||||
def __init__(self):
|
||||
#: This is an instance attribute
|
||||
#:
|
||||
#: value is a string
|
||||
self.instance_attr = "42"
|
||||
|
||||
def _prop_attr_get(self):
|
||||
"""
|
||||
This is a function docstring
|
||||
|
||||
@@ -161,6 +161,7 @@ def test_get_items_summary(make_app, app_params):
|
||||
'emptyLine': "This is the real summary",
|
||||
'module_attr': 'This is a module attribute',
|
||||
'C.class_attr': 'This is a class attribute',
|
||||
'C.instance_attr': 'This is an instance attribute',
|
||||
'C.prop_attr1': 'This is a function docstring',
|
||||
'C.prop_attr2': 'This is a attribute docstring',
|
||||
'C.C2': 'This is a nested inner class docstring',
|
||||
@@ -329,6 +330,7 @@ def test_autosummary_generate(app, status, warning):
|
||||
' ~Foo.CONSTANT3\n'
|
||||
' ~Foo.CONSTANT4\n'
|
||||
' ~Foo.baz\n'
|
||||
' ~Foo.value\n'
|
||||
' \n' in Foo)
|
||||
|
||||
FooBar = (app.srcdir / 'generated' / 'autosummary_dummy_module.Foo.Bar.rst').read_text()
|
||||
|
||||
Reference in New Issue
Block a user