From 4591a6030860e7cc31d7b1dc3dae914927f60a53 Mon Sep 17 00:00:00 2001 From: Tyler Smart Date: Wed, 16 Aug 2023 00:09:26 -0600 Subject: [PATCH 1/3] gh-107995: Fix doctest collection of functools.cached_property objects --- Lib/functools.py | 1 + Lib/test/test_doctest.py | 12 ++++++++++++ Lib/test/test_functools.py | 3 +++ .../2023-08-16-00-24-07.gh-issue-107995.TlTp5t.rst | 1 + 4 files changed, 17 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2023-08-16-00-24-07.gh-issue-107995.TlTp5t.rst diff --git a/Lib/functools.py b/Lib/functools.py index be44ccdae6b692..a2fc28779dbddc 100644 --- a/Lib/functools.py +++ b/Lib/functools.py @@ -984,6 +984,7 @@ def __init__(self, func): 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: diff --git a/Lib/test/test_doctest.py b/Lib/test/test_doctest.py index bea52c6de7ec6d..c364e81a6b1495 100644 --- a/Lib/test/test_doctest.py +++ b/Lib/test/test_doctest.py @@ -111,6 +111,14 @@ def a_classmethod_property(cls): """ 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 @@ def basics(): r""" 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 @@ def basics(): r""" 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 @@ def basics(): r""" 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 @@ def basics(): r""" 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 diff --git a/Lib/test/test_functools.py b/Lib/test/test_functools.py index 50770f066a5e16..5ba7f51c91f3b5 100644 --- a/Lib/test/test_functools.py +++ b/Lib/test/test_functools.py @@ -3105,6 +3105,9 @@ def test_access_from_class(self): 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): 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 index 00000000000000..131685476de707 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-08-16-00-24-07.gh-issue-107995.TlTp5t.rst @@ -0,0 +1 @@ +Fix :mod:`doctest` collection of :func:`functools.cached_property` objects. From fed3ecc5e71fb7ded7c04d24a147540ffa653dd7 Mon Sep 17 00:00:00 2001 From: Alex Waygood Date: Fri, 18 Aug 2023 15:47:16 +0100 Subject: [PATCH 2/3] Improve docs --- .../Library/2023-08-16-00-24-07.gh-issue-107995.TlTp5t.rst | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 index 131685476de707..7247f6b3abc9bc 100644 --- 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 @@ -1 +1,5 @@ -Fix :mod:`doctest` collection of :func:`functools.cached_property` objects. +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. From f5930f5c0559a20e220e1d6b57e92079be18fe21 Mon Sep 17 00:00:00 2001 From: Tyler Smart Date: Fri, 18 Aug 2023 09:07:03 -0600 Subject: [PATCH 3/3] Add name to ACKS --- Misc/ACKS | 1 + 1 file changed, 1 insertion(+) diff --git a/Misc/ACKS b/Misc/ACKS index 8b8c5ad8434bd7..475c6ec3dab73b 100644 --- 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