Skip to content

Commit

Permalink
Close sphinx-doc#9445: autodoc: Support class properties
Browse files Browse the repository at this point in the history
Since python 3.9, `classmethod` starts to support creating a "class
property".  This supports to generate document for it.
  • Loading branch information
tk0miya committed Jul 17, 2021
1 parent 1205255 commit ccb569f
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -13,6 +13,7 @@ Deprecated
Features added
--------------

* #9445: autodoc: Support class properties
* #9445: py domain: ``:py:property:`` directive supports ``:classmethod:``
option to describe the class property

Expand Down
29 changes: 28 additions & 1 deletion sphinx/ext/autodoc/__init__.py
Expand Up @@ -2661,7 +2661,32 @@ class PropertyDocumenter(DocstringStripSignatureMixin, ClassLevelDocumenter): #
@classmethod
def can_document_member(cls, member: Any, membername: str, isattr: bool, parent: Any
) -> bool:
return inspect.isproperty(member) and isinstance(parent, ClassDocumenter)
if isinstance(parent, ClassDocumenter):
if inspect.isproperty(member):
return True
else:
__dict__ = safe_getattr(parent.object, '__dict__', {})
obj = __dict__.get(membername)
return isinstance(obj, classmethod) and inspect.isproperty(obj.__func__)
else:
return False

def import_object(self, raiseerror: bool = False) -> bool:
"""Check the exisitence of uninitialized instance attribute when failed to import
the attribute."""
ret = super().import_object(raiseerror)
if ret and not inspect.isproperty(self.object):
__dict__ = safe_getattr(self.parent, '__dict__', {})
obj = __dict__.get(self.objpath[-1])
if isinstance(obj, classmethod) and inspect.isproperty(obj.__func__):
self.object = obj.__func__
self.isclassmethod = True
return True
else:
return False

self.isclassmethod = False
return ret

def document_members(self, all_members: bool = False) -> None:
pass
Expand All @@ -2675,6 +2700,8 @@ def add_directive_header(self, sig: str) -> None:
sourcename = self.get_sourcename()
if inspect.isabstractmethod(self.object):
self.add_line(' :abstractmethod:', sourcename)
if self.isclassmethod:
self.add_line(' :classmethod:', sourcename)

if safe_getattr(self.object, 'fget', None) and self.config.autodoc_typehints != 'none':
try:
Expand Down
7 changes: 6 additions & 1 deletion tests/roots/test-ext-autodoc/target/properties.py
Expand Up @@ -2,5 +2,10 @@ class Foo:
"""docstring"""

@property
def prop(self) -> int:
def prop1(self) -> int:
"""docstring"""

@classmethod
@property
def prop2(self) -> int:
"""docstring"""
10 changes: 9 additions & 1 deletion tests/test_ext_autodoc_autoclass.py
Expand Up @@ -212,12 +212,20 @@ def test_properties(app):
' docstring',
'',
'',
' .. py:property:: Foo.prop',
' .. py:property:: Foo.prop1',
' :module: target.properties',
' :type: int',
'',
' docstring',
'',
'',
' .. py:property:: Foo.prop2',
' :module: target.properties',
' :classmethod:',
' :type: int',
'',
' docstring',
'',
]


Expand Down
19 changes: 17 additions & 2 deletions tests/test_ext_autodoc_autoproperty.py
Expand Up @@ -16,13 +16,28 @@

@pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_properties(app):
actual = do_autodoc(app, 'property', 'target.properties.Foo.prop')
actual = do_autodoc(app, 'property', 'target.properties.Foo.prop1')
assert list(actual) == [
'',
'.. py:property:: Foo.prop',
'.. py:property:: Foo.prop1',
' :module: target.properties',
' :type: int',
'',
' docstring',
'',
]


@pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_class_properties(app):
actual = do_autodoc(app, 'property', 'target.properties.Foo.prop2')
assert list(actual) == [
'',
'.. py:property:: Foo.prop2',
' :module: target.properties',
' :classmethod:',
' :type: int',
'',
' docstring',
'',
]

0 comments on commit ccb569f

Please sign in to comment.