Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 34k
Description
Bug report
Bug description:
(edited to reflect discussion comments)
doctests of functions decorated with functools.cache (or lru_cache()) do not get their line number retrieved, e.g.:
# file /tmp/t.pyimportfunctools@functools.cachedeff(x): """cached >>> f(1) -2 """return-x$ 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:
>>>importdoctest>>>importt>>>dt, =doctest.DocTestFinder().find(t) >>>dt<DocTestt.ffrom/home/denis/src/cpython/t.py:None (1example)>>>>print(dt.lineno) NoneThis 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.
CPython versions tested on:
CPython main branch, 3.13
Operating systems tested on:
No response
Linked PRs
- gh-136914: Use inspect.isroutine() in DocTest's lineno computation #136930
- [3.13] gh-136914: Fix support of cached functions and properties in DocTest's lineno computation (GH-136930) #137615
- [3.14] gh-136914: Fix support of cached functions and properties in DocTest's lineno computation (GH-136930) #137616