Skip to content

Commit 8f8f0f9

Browse files
gh-61648: Detect line numbers of properties in doctests (GH-113161)
1 parent 737d23f commit 8f8f0f9

File tree

4 files changed

+21
-0
lines changed

4 files changed

+21
-0
lines changed

Lib/doctest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,6 +1136,8 @@ def _find_lineno(self, obj, source_lines):
11361136

11371137
# Find the line number for functions & methods.
11381138
if inspect.ismethod(obj): obj = obj.__func__
1139+
if isinstance(obj, property):
1140+
obj = obj.fget
11391141
if inspect.isfunction(obj) and getattr(obj, '__doc__', None):
11401142
# We don't use `docstring` var here, because `obj` can be changed.
11411143
obj = obj.__code__

Lib/test/doctest_lineno.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,21 @@ def method_with_doctest(self):
4949
'method_with_doctest'
5050
"""
5151

52+
@classmethod
53+
def classmethod_with_doctest(cls):
54+
"""
55+
This has a doctest!
56+
>>> MethodWrapper.classmethod_with_doctest.__name__
57+
'classmethod_with_doctest'
58+
"""
59+
60+
@property
61+
def property_with_doctest(self):
62+
"""
63+
This has a doctest!
64+
>>> MethodWrapper.property_with_doctest.__name__
65+
'property_with_doctest'
66+
"""
67+
5268
# https://github.com/python/cpython/issues/99433
5369
str_wrapper = object().__str__

Lib/test/test_doctest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,9 +670,11 @@ def basics(): r"""
670670
30 test.doctest_lineno.ClassWithDoctest
671671
None test.doctest_lineno.ClassWithoutDocstring
672672
None test.doctest_lineno.MethodWrapper
673+
53 test.doctest_lineno.MethodWrapper.classmethod_with_doctest
673674
39 test.doctest_lineno.MethodWrapper.method_with_docstring
674675
45 test.doctest_lineno.MethodWrapper.method_with_doctest
675676
None test.doctest_lineno.MethodWrapper.method_without_docstring
677+
61 test.doctest_lineno.MethodWrapper.property_with_doctest
676678
4 test.doctest_lineno.func_with_docstring
677679
12 test.doctest_lineno.func_with_doctest
678680
None test.doctest_lineno.func_without_docstring
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Detect line numbers of properties in doctests.

0 commit comments

Comments
 (0)