FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-136914: Use inspect.isroutine() in DocTest's lineno computation by dlax · Pull Request #136930 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (3) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
5 changes: 4 additions & 1 deletion Lib/doctest.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def _test():

import __future__
import difflib
import functools
import inspect
import linecache
import os
Expand Down Expand Up @@ -1141,7 +1142,9 @@ def _find_lineno(self, obj, source_lines):
if inspect.ismethod(obj): obj = obj.__func__
if isinstance(obj, property):
obj = obj.fget
if inspect.isfunction(obj) and getattr(obj, '__doc__', None):
if isinstance(obj, functools.cached_property):
obj = obj.func
if inspect.isroutine(obj) and getattr(obj, '__doc__', None):
# We don't use `docstring` var here, because `obj` can be changed.
obj = inspect.unwrap(obj)
try:
Expand Down
29 changes: 29 additions & 0 deletions Lib/test/test_doctest/doctest_lineno.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,32 @@ def property_with_doctest(self):
@decorator
def func_with_docstring_wrapped():
"""Some unrelated info."""


# https://github.com/python/cpython/issues/136914
import functools


@functools.cache

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I'd suggest using a custom class decorator here instead of functools.cache. We don't want this to depend on the implementation details of functools.cache, which could change in the future.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

I think that this may add unnecessary complexity to the tests. And such test will not guarantee that the code works also for functools.cached, which is a comon case. Other tests just test common cases.

def cached_func_with_doctest(value):
"""
>>> cached_func_with_doctest(1)
-1
"""
return -value


@functools.cache
def cached_func_without_docstring(value):
return value + 1


class ClassWithACachedProperty:

@functools.cached_property
def cached(self):
"""
>>> X().cached
-1
"""
return 0
4 changes: 4 additions & 0 deletions Lib/test/test_doctest/test_doctest.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,8 @@ def basics(): r"""
>>> for t in tests:
... print('%5s %s' % (t.lineno, t.name))
None test.test_doctest.doctest_lineno
None test.test_doctest.doctest_lineno.ClassWithACachedProperty
102 test.test_doctest.doctest_lineno.ClassWithACachedProperty.cached
22 test.test_doctest.doctest_lineno.ClassWithDocstring
30 test.test_doctest.doctest_lineno.ClassWithDoctest
None test.test_doctest.doctest_lineno.ClassWithoutDocstring
Expand All @@ -687,6 +689,8 @@ def basics(): r"""
45 test.test_doctest.doctest_lineno.MethodWrapper.method_with_doctest
None test.test_doctest.doctest_lineno.MethodWrapper.method_without_docstring
61 test.test_doctest.doctest_lineno.MethodWrapper.property_with_doctest
86 test.test_doctest.doctest_lineno.cached_func_with_doctest
None test.test_doctest.doctest_lineno.cached_func_without_docstring
4 test.test_doctest.doctest_lineno.func_with_docstring
77 test.test_doctest.doctest_lineno.func_with_docstring_wrapped
12 test.test_doctest.doctest_lineno.func_with_doctest
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix retrieval of :attr:`doctest.DocTest.lineno` for objects decorated with
:func:`functools.cache` or :class:`functools.cached_property`.
Loading

Back | FazBrowse Home | New Git URL