Skip to content

functools.wraps does not work on class methods #10653

Description

@jakkdl

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions