Skip to content

Commit 6ff7c13

Browse files
[3.11] gh-61648: Detect line numbers of properties in doctests (GH-113161) (GH-113165)
(cherry picked from commit 8f8f0f9) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 811972e commit 6ff7c13

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
@@ -1114,6 +1114,8 @@ def _find_lineno(self, obj, source_lines):
11141114

11151115
# Find the line number for functions & methods.
11161116
if inspect.ismethod(obj): obj = obj.__func__
1117+
if isinstance(obj, property):
1118+
obj = obj.fget
11171119
if inspect.isfunction(obj) and getattr(obj, '__doc__', None):
11181120
# We don't use `docstring` var here, because `obj` can be changed.
11191121
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
@@ -673,9 +673,11 @@ def basics(): r"""
673673
30 test.doctest_lineno.ClassWithDoctest
674674
None test.doctest_lineno.ClassWithoutDocstring
675675
None test.doctest_lineno.MethodWrapper
676+
53 test.doctest_lineno.MethodWrapper.classmethod_with_doctest
676677
39 test.doctest_lineno.MethodWrapper.method_with_docstring
677678
45 test.doctest_lineno.MethodWrapper.method_with_doctest
678679
None test.doctest_lineno.MethodWrapper.method_without_docstring
680+
61 test.doctest_lineno.MethodWrapper.property_with_doctest
679681
4 test.doctest_lineno.func_with_docstring
680682
12 test.doctest_lineno.func_with_doctest
681683
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)