Skip to content

Commit 9bb576c

Browse files
tjsmartAlexWaygood
andauthored
gh-107995: Fix doctest collection of functools.cached_property objects (#107996)
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
1 parent 28cab71 commit 9bb576c

File tree

5 files changed

+22
-0
lines changed

5 files changed

+22
-0
lines changed

Lib/functools.py

+1
Original file line numberDiff line numberDiff line change
@@ -984,6 +984,7 @@ def __init__(self, func):
984984
self.func = func
985985
self.attrname = None
986986
self.__doc__ = func.__doc__
987+
self.__module__ = func.__module__
987988

988989
def __set_name__(self, owner, name):
989990
if self.attrname is None:

Lib/test/test_doctest.py

+12
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@ def a_classmethod_property(cls):
111111
"""
112112
return cls.a_class_attribute
113113

114+
@functools.cached_property
115+
def a_cached_property(self):
116+
"""
117+
>>> print(SampleClass(29).get())
118+
29
119+
"""
120+
return "hello"
121+
114122
class NestedClass:
115123
"""
116124
>>> x = SampleClass.NestedClass(5)
@@ -515,6 +523,7 @@ def basics(): r"""
515523
3 SampleClass.NestedClass
516524
1 SampleClass.NestedClass.__init__
517525
1 SampleClass.__init__
526+
1 SampleClass.a_cached_property
518527
2 SampleClass.a_classmethod
519528
1 SampleClass.a_classmethod_property
520529
1 SampleClass.a_property
@@ -571,6 +580,7 @@ def basics(): r"""
571580
3 some_module.SampleClass.NestedClass
572581
1 some_module.SampleClass.NestedClass.__init__
573582
1 some_module.SampleClass.__init__
583+
1 some_module.SampleClass.a_cached_property
574584
2 some_module.SampleClass.a_classmethod
575585
1 some_module.SampleClass.a_classmethod_property
576586
1 some_module.SampleClass.a_property
@@ -613,6 +623,7 @@ def basics(): r"""
613623
3 SampleClass.NestedClass
614624
1 SampleClass.NestedClass.__init__
615625
1 SampleClass.__init__
626+
1 SampleClass.a_cached_property
616627
2 SampleClass.a_classmethod
617628
1 SampleClass.a_classmethod_property
618629
1 SampleClass.a_property
@@ -634,6 +645,7 @@ def basics(): r"""
634645
0 SampleClass.NestedClass.get
635646
0 SampleClass.NestedClass.square
636647
1 SampleClass.__init__
648+
1 SampleClass.a_cached_property
637649
2 SampleClass.a_classmethod
638650
1 SampleClass.a_classmethod_property
639651
1 SampleClass.a_property

Lib/test/test_functools.py

+3
Original file line numberDiff line numberDiff line change
@@ -3105,6 +3105,9 @@ def test_access_from_class(self):
31053105
def test_doc(self):
31063106
self.assertEqual(CachedCostItem.cost.__doc__, "The cost of the item.")
31073107

3108+
def test_module(self):
3109+
self.assertEqual(CachedCostItem.cost.__module__, CachedCostItem.__module__)
3110+
31083111
def test_subclass_with___set__(self):
31093112
"""Caching still works for a subclass defining __set__."""
31103113
class readonly_cached_property(py_functools.cached_property):

Misc/ACKS

+1
Original file line numberDiff line numberDiff line change
@@ -1706,6 +1706,7 @@ Roman Skurikhin
17061706
Ville Skyttä
17071707
Michael Sloan
17081708
Nick Sloan
1709+
Tyler Smart
17091710
Radek Smejkal
17101711
Václav Šmilauer
17111712
Casper W. Smet
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
The ``__module__`` attribute on instances of :class:`functools.cached_property`
2+
is now set to the name of the module in which the cached_property is defined,
3+
rather than "functools". This means that doctests in ``cached_property``
4+
docstrings are now properly collected by the :mod:`doctest` module. Patch by
5+
Tyler Smart.

0 commit comments

Comments
 (0)