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
functools.wraps does not work on class methods #10653
We recently hit an error using @wraps on a class method in Trio: python-trio/trio#2775 (comment) when checked with pyright. It works fine on mypy, but I'm guessing they might be special-casing wraps usage while pyright doesn't.
Minimal repro (python 3.11):
fromfunctoolsimportwrapsfromtypingimportassert_typedeffoo(param: int) ->str:
"""docstring"""return""@wraps(foo)defmy_function(blah: int) ->str:
return'foo'# works fineassert_type(my_function(5), str)
classMyOtherClass:
defbar(self, param: int) ->str:
"""docstring"""return""classMyClass:
@wraps(MyOtherClass.bar)defmy_method(self, blah: int) ->str:
return'foo'instance=MyClass()
# works in mypy, but not pyrightassert_type(instance.my_method(5), str)
mypy 1.5.1 works without any issue, pyright 1.1.325 gives
./test.py
./test.py:26:13 - error: Argument missing for parameter "blah" (reportGeneralTypeIssues)
./test.py:26:13 - error: "assert_type" mismatch: expected "str" but received "Unknown" (reportGeneralTypeIssues)
2 errors, 0 warnings, 0 informations
This smells a lot like the problem with getting functools.cache to work both with methods and functions, see e.g. #6347
@erictraut's PR #6670 changed functools.wraps to use ParamSpec. However, we found that this change only caused regressions for mypy users (likely at least in part because mypy at the time had much less complete support for ParamSpec than pyright did). As a result, mypy currently reverts that change to functools.wraps as part of every typeshed sync that it does. See the fifth commit in python/mypy#16009, as an example.
Oh wait, does that mean that it's fixed for both type checkers, @jakkdl? Or that both mypy and pyright now have the bug, whereas previously it was only pyright?
Both have the bug now, in that mypy no longer deviates from typeshed - which doesn't handle class methods. I think I confused myself at some point too in the mypy issue.
I don't remember enough details from #6347 to figure out if this has the same issue and can't be resolved without Intersection, or if it's a simpler case.
Activity
AlexWaygood commentedon Sep 2, 2023
@erictraut's PR #6670 changed
functools.wrapsto useParamSpec. However, we found that this change only caused regressions for mypy users (likely at least in part because mypy at the time had much less complete support forParamSpecthan pyright did). As a result, mypy currently reverts that change tofunctools.wrapsas part of every typeshed sync that it does. See the fifth commit in python/mypy#16009, as an example.tamird commentedon Feb 23, 2024
@AlexWaygood do you think it's appropriate to stop reverting that change on an ongoing basis now?
AlexWaygood commentedon Feb 23, 2024
Last I checked it still caused a lot of false positives for mypy users, but it might be worth checking again
tamird commentedon Feb 23, 2024
How can I help?
AlexWaygood commentedon Feb 23, 2024
You could open an experimental draft PR against mypy reverting python/mypy@0dd4b6f and see what mypy_primer says
jakkdl commentedon Mar 18, 2024
With python/mypy#16942 being merged the behavior is now similar between mypy (if installing directly from
master/) and pyright:AlexWaygood commentedon Mar 18, 2024
Oh wait, does that mean that it's fixed for both type checkers, @jakkdl? Or that both mypy and pyright now have the bug, whereas previously it was only pyright?
jakkdl commentedon Mar 19, 2024
Both have the bug now, in that
mypyno longer deviates from typeshed - which doesn't handle class methods. I think I confused myself at some point too in the mypy issue.I don't remember enough details from #6347 to figure out if this has the same issue and can't be resolved without
Intersection, or if it's a simpler case.Add tests to functools.wraps
Add tests to functools.wraps (#12173)
srittau commentedon Jun 24, 2024
Also discussed on discuss.python.org: https://discuss.python.org/t/making-functions-subscriptable-at-runtime/26463/31
Add tests to functools.wraps (python#12173)
Document and hint logger.py (#4305)
womp womp wraps is broken
womp womp wraps is broken
womp womp wraps is broken