Skip to content

Commit 48c961a

Browse files
committed
Add a test for the @cached_property hack
1 parent 7f87b70 commit 48c961a

5 files changed

Lines changed: 151 additions & 190 deletions

File tree

pyproject.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,16 @@ tests = [
4848
"pympler",
4949
"pytest",
5050
"pytest-xdist[psutil]",
51+
"sphinx",
52+
"sphinx-notfound-page"
53+
"sphinx-basic-ng",
54+
"sphinxcontrib-applehelp",
55+
"sphinxcontrib-devhelp",
56+
"sphinxcontrib-htmlhelp",
57+
"sphinxcontrib-jsmath",
58+
"sphinxcontrib-serializinghtml",
59+
"sphinxcontrib-towncrier",
60+
"myst-parser",
5161
]
5262
cov = [{ include-group = "tests" }, "coverage[toml]"]
5363
pyright = ["pyright", { include-group = "tests" }]

tests/index.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.. autoclass:: tests.test_slots.SphinxDocTest
2+
3+
.. autoproperty:: documented

tests/index.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class tests.test_slots.SphinxDocTest
2+
3+
Test that slotted cached_property shows up in Sphinx docs
4+
5+
property documented
6+
7+
A very well documented function

tests/test_slots.py

Lines changed: 28 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
import functools
88
import pickle
99
import weakref
10-
10+
from pathlib import Path
11+
from tempfile import TemporaryDirectory
1112
from unittest import mock
1213

1314
import pytest
15+
try:
16+
from sphinx.application import Sphinx
17+
except ImportError:
18+
Sphinx = None
1419

1520
import attr
1621
import attrs
@@ -735,26 +740,33 @@ def f(self):
735740
assert B(11).f == 121
736741
assert B(17).f == 289
737742

743+
@attr.s(slots=True)
744+
class SphinxDocTest:
745+
"""Test that slotted cached_property shows up in Sphinx docs"""
738746

739-
def test_slots_cached_property_allows_call():
740-
"""
741-
cached_property in slotted class allows call.
742-
"""
743-
744-
@attr.s(slots=True)
745-
class A:
746-
x = attr.ib()
747+
@functools.cached_property
748+
def documented(self):
749+
"""A very well documented function"""
750+
return True
747751

748-
@functools.cached_property
749-
def f(self):
750-
return self.x
751752

752-
assert A(11).f == 11
753+
@pytest.mark.skipif(Sphinx is None, reason="Sphinx is not installed")
754+
def test_sphinx_autodocuments_cached_property():
755+
here = Path(__file__).parent
756+
with TemporaryDirectory() as td:
757+
tmp_path = Path(td)
758+
app = Sphinx(here, here.parent.joinpath("docs"), tmp_path, tmp_path, "text")
759+
app.build(force_all=True)
760+
with (
761+
tmp_path.joinpath("index.txt").open() as written,
762+
Path(__file__).parent.joinpath("index.txt").open() as good,
763+
):
764+
assert written.read() == good.read()
753765

754766

755-
def test_slots_cached_property_has_docstring():
767+
def test_slots_cached_property_allows_call():
756768
"""
757-
cached_property in slotted class carries its original docstring
769+
cached_property in slotted class allows call.
758770
"""
759771

760772
@attr.s(slots=True)
@@ -763,10 +775,9 @@ class A:
763775

764776
@functools.cached_property
765777
def f(self):
766-
"""What an informative docstring!"""
767778
return self.x
768779

769-
assert A(11).f.__doc__ == "What an informative docstring!"
780+
assert A(11).f == 11
770781

771782

772783
def test_slots_cached_property_class_does_not_have__dict__():

0 commit comments

Comments
 (0)