You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DocTests for functools.cache()-decorated functions have no line number #136914
$ python -m doctest /tmp/t.py**********************************************************************File "/tmp/t.py", line ?, in t.fFailed example: f(1)Expected: -2Got: -1**********************************************************************1 item had failures: 1 of 1 in t.f***Test Failed*** 1 failure.
where we can see that the File "..." line is missing the line number of the example.
Also:
This is because DocTest._find_lineno() relies on inspect.isfunction() to possibly inspect function's code and get line numbers; but inspect.isfunction() returns False for a function decorated with functools.cache because only plain types.FunctionType is 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.
inspect.isfunction has very strict constraints and after being decorated by functools.cache, the object is no longer a function at runtime. So I don't think we should change anything in inspect. However we should change something in doctest.
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).
For 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_wrapper was a realistic use case. Evidently it is :-)
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
Activity
picnixz commentedon 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).
cc @serhiy-storchaka
brianschubert commentedon Jul 21, 2025
For 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 :-)serhiy-storchaka commentedon Jul 21, 2025
inspect.isroutine()is already used in some places ofdoctest. It should be used instead ofinspect.isfunction()in other places as well.[-]functools.cache decorated functions not handled by inspect.isfunction()[/-][+]DocTests for functools.cache()-decorated functions have no line number[/+]gh-136914: Fix support of cached functions and properties in DocTest'…
pythongh-136914: Fix support of cached functions and properties in Do…
pythongh-136914: Fix support of cached functions and properties in Do…
pythongh-136914: Fix support of cached functions and properties in Do…
[3.13] gh-136914: Fix support of cached functions and properties in D…
pythongh-136914: Fix support of cached functions and properties in Do…
[3.14] gh-136914: Fix support of cached functions and properties in D…