Skip to content

Commit ded4307

Browse files
[3.12] gh-61648: Detect line numbers of properties in doctests (GH-113161) (GH-113164)
(cherry picked from commit 8f8f0f9) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 6259dfa commit ded4307

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

11211121
# Find the line number for functions & methods.
11221122
if inspect.ismethod(obj): obj = obj.__func__
1123+
if isinstance(obj, property):
1124+
obj = obj.fget
11231125
if inspect.isfunction(obj) and getattr(obj, '__doc__', None):
11241126
# We don't use `docstring` var here, because `obj` can be changed.
11251127
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
@@ -671,9 +671,11 @@ def basics(): r"""
671671
30 test.doctest_lineno.ClassWithDoctest
672672
None test.doctest_lineno.ClassWithoutDocstring
673673
None test.doctest_lineno.MethodWrapper
674+
53 test.doctest_lineno.MethodWrapper.classmethod_with_doctest
674675
39 test.doctest_lineno.MethodWrapper.method_with_docstring
675676
45 test.doctest_lineno.MethodWrapper.method_with_doctest
676677
None test.doctest_lineno.MethodWrapper.method_without_docstring
678+
61 test.doctest_lineno.MethodWrapper.property_with_doctest
677679
4 test.doctest_lineno.func_with_docstring
678680
12 test.doctest_lineno.func_with_doctest
679681
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)