Skip to content

Commit 553fb13

Browse files
committed
Detect line numbers for cached properties in doctests
1 parent ddf3ab1 commit 553fb13

File tree

4 files changed

+18
-1
lines changed

4 files changed

+18
-1
lines changed

Lib/doctest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ def _test():
9494

9595
import __future__
9696
import difflib
97+
import functools
9798
import inspect
9899
import linecache
99100
import os
@@ -1141,6 +1142,8 @@ def _find_lineno(self, obj, source_lines):
11411142
if inspect.ismethod(obj): obj = obj.__func__
11421143
if isinstance(obj, property):
11431144
obj = obj.fget
1145+
if isinstance(obj, functools.cached_property):
1146+
obj = obj.func
11441147
if inspect.isroutine(obj) and getattr(obj, '__doc__', None):
11451148
# We don't use `docstring` var here, because `obj` can be changed.
11461149
obj = inspect.unwrap(obj)

Lib/test/test_doctest/doctest_lineno.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,14 @@ def cached_func_with_doctest(value):
9494
@functools.cache
9595
def cached_func_without_docstring(value):
9696
return value + 1
97+
98+
99+
class ClassWithACachedProperty:
100+
101+
@functools.cached_property
102+
def cached(self):
103+
"""
104+
>>> X().cached
105+
-1
106+
"""
107+
return 0

Lib/test/test_doctest/test_doctest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,8 @@ def basics(): r"""
678678
>>> for t in tests:
679679
... print('%5s %s' % (t.lineno, t.name))
680680
None test.test_doctest.doctest_lineno
681+
None test.test_doctest.doctest_lineno.ClassWithACachedProperty
682+
102 test.test_doctest.doctest_lineno.ClassWithACachedProperty.cached
681683
22 test.test_doctest.doctest_lineno.ClassWithDocstring
682684
30 test.test_doctest.doctest_lineno.ClassWithDoctest
683685
None test.test_doctest.doctest_lineno.ClassWithoutDocstring
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
Fix retrieval of :attr:`doctest.DocTest.lineno` for :func:`functools.cache`-decorated functions.
1+
Fix retrieval of :attr:`doctest.DocTest.lineno` for objects decorated with
2+
:func:`functools.cache` or :class:`functools.cached_property`.

0 commit comments

Comments
 (0)