8000 [3.11] gh-61648: Detect line numbers of properties in doctests (GH-113161) by miss-islington · Pull Request #113165 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

[3.11] gh-61648: Detect line numbers of properties in doctests (GH-113161) #113165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Lib/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -1114,6 +1114,8 @@ def _find_lineno(self, obj, source_lines):

# Find the line number for functions & methods.
if inspect.ismethod(obj): obj = obj.__func__
if isinstance(obj, property):
obj = obj.fget
if inspect.isfunction(obj) and getattr(obj, '__doc__', None):
# We don't use `docstring` var here, because `obj` can be changed.
obj = obj.__code__
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/doctest_lineno.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,21 @@ def method_with_doctest(self):
'method_with_doctest'
"""

@classmethod
def classmethod_with_doctest(cls):
"""
This has a doctest!
>>> MethodWrapper.classmethod_with_doctest.__name__
'classmethod_with_doctest'
"""

@property
def property_with_doctest(self):
"""
This has a doctest!
>>> MethodWrapper.property_with_doctest.__name__
'property_with_doctest'
"""

# https://github.com/python/cpython/issues/99433
str_wrapper = object().__str__
2 changes: 2 additions & 0 deletions Lib/test/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,9 +673,11 @@ def basics(): r"""
30 test.doctest_lineno.ClassWithDoctest
None test.doctest_lineno.ClassWithoutDocstring
None test.doctest_lineno.MethodWrapper
53 test.doctest_lineno.MethodWrapper.classmethod_with_doctest
39 test.doctest_lineno.MethodWrapper.method_with_docstring
45 test.doctest_lineno.MethodWrapper.method_with_doctest
None test.doctest_lineno.MethodWrapper.method_without_docstring
61 test.doctest_lineno.MethodWrapper.property_with_doctest
4 test.doctest_lineno.func_with_docstring
12 test.doctest_lineno.func_with_doctest
None test.doctest_lineno.func_without_docstring
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Detect line numbers of properties in doctests.
0