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

Merged
merged 2 commits into from
Jul 25, 2025

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

@dlax
Copy link
Contributor Author

dlax commented Jul 23, 2025

Does it also work for cached_property?

Apparently not; should I make it work in the scope of this PR?

@serhiy-storchaka
Copy link
Member

If this is easy, why not? Otherwise this can be a separate PR.

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.

LGTM. 👍

@dlax
Copy link
Contributor Author

dlax commented Jul 25, 2025

Added a commit to handle functools.cached_property.

@dlax dlax force-pushed the doctest-lineno-isroutine branch from 168503c to 553fb13 Compare July 25, 2025 07:17
@serhiy-storchaka serhiy-storchaka merged commit fece15d into python:main Jul 25, 2025
41 checks passed
@dlax dlax deleted the doctest-lineno-isroutine branch July 25, 2025 12:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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