Use inspect.isroutine() in DocTest's lineno computation · python/cpython@ddf3ab1 · GitHub
Skip to content

Commit ddf3ab1

Browse files
committed
Use inspect.isroutine() in DocTest's lineno computation
Previously, DocTest's lineno of `functools.cache()`-decorated functions was not properly returned (None was returned) because the underlying computation, in `DocTest._find_lineno()`, relied on `inspect.isfunction()` which does not consider the decorated result as a function. We now use the more generic `inspect.isroutine()`, as elsewhere in doctest's logic, thus fixing lineno computation for functools.cache()-decorated functions.
1 parent 58d305c commit ddf3ab1

4 files changed

Lines changed: 22 additions & 1 deletion

File tree

Lib/doctest.py

Lines changed: 1 addition & 1 deletion

Lib/test/test_doctest/doctest_lineno.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,21 @@ def property_with_doctest(self):
7676
@decorator
7777
def func_with_docstring_wrapped():
7878
"""Some unrelated info."""
79+
80+
81+
# https://github.com/python/cpython/issues/136914
82+
import functools
83+
84+
85+
@functools.cache
86+
def cached_func_with_doctest(value):
87+
"""
88+
>>> cached_func_with_doctest(1)
89+
-1
90+
"""
91+
return -value
92+
93+
94+
@functools.cache
95+
def cached_func_without_docstring(value):
96+
return value + 1

Lib/test/test_doctest/test_doctest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -687,6 +687,8 @@ def basics(): r"""
687687
45 test.test_doctest.doctest_lineno.MethodWrapper.method_with_doctest
688688
None test.test_doctest.doctest_lineno.MethodWrapper.method_without_docstring
689689
61 test.test_doctest.doctest_lineno.MethodWrapper.property_with_doctest
690+
86 test.test_doctest.doctest_lineno.cached_func_with_doctest
691+
None test.test_doctest.doctest_lineno.cached_func_without_docstring
690692
4 test.test_doctest.doctest_lineno.func_with_docstring
691693
77 test.test_doctest.doctest_lineno.func_with_docstring_wrapped
692694
12 test.test_doctest.doctest_lineno.func_with_doctest
Lines changed: 1 addition & 0 deletions

0 commit comments

Comments
 (0)