77import functools
88import pickle
99import weakref
10-
10+ from pathlib import Path
11+ from tempfile import TemporaryDirectory
1112from unittest import mock
1213
1314import pytest
15+ try :
16+ from sphinx .application import Sphinx
17+ except ImportError :
18+ Sphinx = None
1419
1520import attr
1621import 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
772783def test_slots_cached_property_class_does_not_have__dict__ ():
0 commit comments