Skip to content

Commit

Permalink
Fix #8103: autodoc: cached_property is not considered as a property
Browse files Browse the repository at this point in the history
sphinx.util.inspect:isproperty() does not considers that
cached_property decorator that has been added since Python 3.8 is
a kind of properties.  This fixes it.
  • Loading branch information
tk0miya committed Aug 13, 2020
1 parent 4baa7ce commit 088b049
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -16,6 +16,7 @@ Features added
Bugs fixed
----------

* #8103: autodoc: functools.cached_property is not considered as a property
* #8093: The highlight warning has wrong location in some builders (LaTeX,
singlehtml and so on)

Expand Down
5 changes: 5 additions & 0 deletions sphinx/util/inspect.py
Expand Up @@ -304,6 +304,11 @@ def iscoroutinefunction(obj: Any) -> bool:

def isproperty(obj: Any) -> bool:
"""Check if the object is property."""
if sys.version_info > (3, 8):

This comment has been minimized.

Copy link
@plotski

plotski Dec 1, 2020

Shouldn't this be if sys.version_info >= (3, 8) if cached_property was added in 3.8?

This comment has been minimized.

Copy link
@tk0miya

tk0miya Dec 12, 2020

Author Member

Oops. Absolutely. I'll fix it ASAP.

This comment has been minimized.

Copy link
@tk0miya

tk0miya Dec 12, 2020

Author Member

Wait. I believe this condition works fine even on python 3.8.0:

>>> (3, 8, 0) > (3, 8)
True

I think this is correct.

This comment has been minimized.

Copy link
@plotski

plotski via email Dec 12, 2020

This comment has been minimized.

Copy link
@tk0miya

tk0miya Dec 12, 2020

Author Member

Understandable. I just posted #8527.

This comment has been minimized.

Copy link
@plotski

plotski via email Dec 12, 2020

from functools import cached_property # cached_property is available since py3.8
if isinstance(obj, cached_property):
return True

return isinstance(obj, property)


Expand Down
7 changes: 7 additions & 0 deletions tests/roots/test-ext-autodoc/target/cached_property.py
@@ -0,0 +1,7 @@
from functools import cached_property


class Foo:
@cached_property
def prop(self) -> int:
return 1
20 changes: 20 additions & 0 deletions tests/test_ext_autodoc.py
Expand Up @@ -881,6 +881,26 @@ def test_autodoc_descriptor(app):
]


@pytest.mark.skipif(sys.version_info < (3, 8),
reason='cached_property is available since python3.8.')
@pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_autodoc_cached_property(app):
options = {"members": None,
"undoc-members": True}
actual = do_autodoc(app, 'class', 'target.cached_property.Foo', options)
assert list(actual) == [
'',
'.. py:class:: Foo()',
' :module: target.cached_property',
'',
'',
' .. py:method:: Foo.prop',
' :module: target.cached_property',
' :property:',
'',
]


@pytest.mark.sphinx('html', testroot='ext-autodoc')
def test_autodoc_member_order(app):
# case member-order='bysource'
Expand Down

0 comments on commit 088b049

Please sign in to comment.