]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-107995: Fix doctest collection of functools.cached_property objects (#107996)
authorTyler Smart <40041862+tjsmart@users.noreply.github.com>
Fri, 18 Aug 2023 15:44:38 +0000 (08:44 -0700)
committerGitHub <noreply@github.com>
Fri, 18 Aug 2023 15:44:38 +0000 (15:44 +0000)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Lib/functools.py
Lib/test/test_doctest.py
Lib/test/test_functools.py
Misc/ACKS
Misc/NEWS.d/next/Library/2023-08-16-00-24-07.gh-issue-107995.TlTp5t.rst [new file with mode: 0644]

index be44ccdae6b692b4d424be4c9647280b0d8a6b29..a2fc28779dbddcd238ced775cbe0afe044a0f375 100644 (file)
@@ -984,6 +984,7 @@ class cached_property:
         self.func = func
         self.attrname = None
         self.__doc__ = func.__doc__
+        self.__module__ = func.__module__
 
     def __set_name__(self, owner, name):
         if self.attrname is None:
index bea52c6de7ec6de528d175846004723c919addde..c364e81a6b1495e153472e061fbc1df0ada66aa7 100644 (file)
@@ -111,6 +111,14 @@ class SampleClass:
         """
         return cls.a_class_attribute
 
+    @functools.cached_property
+    def a_cached_property(self):
+        """
+        >>> print(SampleClass(29).get())
+        29
+        """
+        return "hello"
+
     class NestedClass:
         """
         >>> x = SampleClass.NestedClass(5)
@@ -515,6 +523,7 @@ methods, classmethods, staticmethods, properties, and nested classes.
      3  SampleClass.NestedClass
      1  SampleClass.NestedClass.__init__
      1  SampleClass.__init__
+     1  SampleClass.a_cached_property
      2  SampleClass.a_classmethod
      1  SampleClass.a_classmethod_property
      1  SampleClass.a_property
@@ -571,6 +580,7 @@ functions, classes, and the `__test__` dictionary, if it exists:
      3  some_module.SampleClass.NestedClass
      1  some_module.SampleClass.NestedClass.__init__
      1  some_module.SampleClass.__init__
+     1  some_module.SampleClass.a_cached_property
      2  some_module.SampleClass.a_classmethod
      1  some_module.SampleClass.a_classmethod_property
      1  some_module.SampleClass.a_property
@@ -613,6 +623,7 @@ By default, an object with no doctests doesn't create any tests:
      3  SampleClass.NestedClass
      1  SampleClass.NestedClass.__init__
      1  SampleClass.__init__
+     1  SampleClass.a_cached_property
      2  SampleClass.a_classmethod
      1  SampleClass.a_classmethod_property
      1  SampleClass.a_property
@@ -634,6 +645,7 @@ displays.
      0  SampleClass.NestedClass.get
      0  SampleClass.NestedClass.square
      1  SampleClass.__init__
+     1  SampleClass.a_cached_property
      2  SampleClass.a_classmethod
      1  SampleClass.a_classmethod_property
      1  SampleClass.a_property
index 50770f066a5e163ea88c6ec9d8b192dac8f54ad8..5ba7f51c91f3b51ba1720583aecce9c1e2783e75 100644 (file)
@@ -3105,6 +3105,9 @@ class TestCachedProperty(unittest.TestCase):
     def test_doc(self):
         self.assertEqual(CachedCostItem.cost.__doc__, "The cost of the item.")
 
+    def test_module(self):
+        self.assertEqual(CachedCostItem.cost.__module__, CachedCostItem.__module__)
+
     def test_subclass_with___set__(self):
         """Caching still works for a subclass defining __set__."""
         class readonly_cached_property(py_functools.cached_property):
index 8b8c5ad8434bd723b993b5df5b2627d6fa22170c..475c6ec3dab73be7a3c3d877db86070e2137a898 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1706,6 +1706,7 @@ Roman Skurikhin
 Ville Skyttä
 Michael Sloan
 Nick Sloan
+Tyler Smart
 Radek Smejkal
 Václav Šmilauer
 Casper W. Smet
diff --git a/Misc/NEWS.d/next/Library/2023-08-16-00-24-07.gh-issue-107995.TlTp5t.rst b/Misc/NEWS.d/next/Library/2023-08-16-00-24-07.gh-issue-107995.TlTp5t.rst
new file mode 100644 (file)
index 0000000..7247f6b
--- /dev/null
@@ -0,0 +1,5 @@
+The ``__module__`` attribute on instances of :class:`functools.cached_property`
+is now set to the name of the module in which the cached_property is defined,
+rather than "functools". This means that doctests in ``cached_property``
+docstrings are now properly collected by the :mod:`doctest` module. Patch by
+Tyler Smart.