Skip to content

gh-136914: Use inspect.isroutine() in DocTest's lineno computation #136930

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

dlax
Copy link
Contributor

@dlax dlax commented Jul 21, 2025

Previously, DocTest's lineno of functools.cache()-decorated functions was not properly returned (None was returned) because the underlying computation, in DocTest._find_lineno(), relied on inspect.isfunction() which does not consider the decorated result as a function.

We now use the more generic inspect.isroutine(), as elsewhere in doctest's logic, thus fixing lineno computation for functools.cache()-decorated functions.

Previously, DocTest's lineno of `functools.cache()`-decorated functions
was not properly returned (None was returned) because the underlying
computation, in `DocTest._find_lineno()`, relied on
`inspect.isfunction()` which does not consider the decorated result as a
function.

We now use the more generic `inspect.isroutine()`, as elsewhere
in doctest's logic, thus fixing lineno computation for
functools.cache()-decorated functions.
@dlax dlax force-pushed the doctest-lineno-isroutine branch from b1e3185 to ddf3ab1 Compare July 21, 2025 13:59
@serhiy-storchaka serhiy-storchaka self-requested a review July 23, 2025 12:06
@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Jul 23, 2025
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Does it also work for cached_property?

import functools


@functools.cache
Copy link
Member

Choose a reason for hiding this comment

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

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
Member

Choose a reason for hiding this comment

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

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.

@@ -0,0 +1 @@
Fix retrieval of :attr:`doctest.DocTest.lineno` for :func:`functools.cache`-decorated functions.
Copy link
Member

Choose a reason for hiding this comment

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

Likewise, I'd suggest wording this more generally -- this affects any class decorator that uses functools.update_wrapper, not just functools.cache

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants