Repository navigation
DocTests for functools.cache()-decorated functions have no line number #136914
Description
Activity
- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jul 21, 2025 - addedstdlibStandard Library Python modules in the Lib/ directoryStandard Library Python modules in the Lib/ directory
on Jul 21, 2025 inspect.isfunctionhas very strict constraints and after being decorated byfunctools.cache, the object is no longer a function at runtime. So I don't think we should change anything ininspect. However we should change something indoctest.EDIT: Also, it might not necessarily be the case that the decorator is called at the same place where the function has been defined (sometimes, people write it in two steps).
Reacted by Brian SchubertFor context, the was foreseen as being a possible issue in #115440 (review), but at the time it wasn't known whether a class wrapper using
functools.wraps/functools.update_wrapperwas a realistic use case. Evidently it is :-)inspect.isroutine()is already used in some places ofdoctest. It should be used instead ofinspect.isfunction()in other places as well.Reacted by Denis Laxalde- changed the title
[-]functools.cache decorated functions not handled by inspect.isfunction()[/-][+]DocTests for functools.cache()-decorated functions have no line number[/+]on Jul 22, 2025 - added a commit that references this issue
on Jul 25, 2025 - added a commit that references this issue
on Aug 10, 2025 - added a commit that references this issue
on Oct 7, 2025
Bug report
Bug description:
(edited to reflect discussion comments)
doctests of functions decorated with
functools.cache(orlru_cache()) do not get their line number retrieved, e.g.:where we can see that the
File "..."line is missing the line number of the example.Also:
This is because
DocTest._find_lineno()relies oninspect.isfunction()to possibly inspect function's code and get line numbers; butinspect.isfunction()returnsFalsefor a function decorated withfunctools.cachebecause only plaintypes.FunctionTypeis considered.Original question: Should such cached functions be considered as well in
inspect.isfunction()?I would be happy to work on a fix in case this change is acceptable.
CPython versions tested on:
CPython main branch, 3.13
Operating systems tested on:
No response
Linked PRs