Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mypy v1.10.0 errors out when functools wraps a method #17166

Open
davidbrochart opened this issue Apr 24, 2024 · 4 comments
Open

Mypy v1.10.0 errors out when functools wraps a method #17166

davidbrochart opened this issue Apr 24, 2024 · 4 comments
Labels
bug mypy got something wrong

Comments

@davidbrochart
Copy link

Bug Report

mypy v1.10.0 gives the following error, while it was working with v1.9.0:

test.py:13: error: Missing positional argument "self" in call to "__call__" of "_Wrapped"  [call-arg]

To Reproduce

# test.py

from functools import wraps

class Foo:
    def f(self):
        pass

class Bar:
    @wraps(Foo.f)
    def f(self):
        pass

bar = Bar()
bar.f()
mypy test.py

Expected Behavior

There should be no error.

@davidbrochart davidbrochart added the bug mypy got something wrong label Apr 24, 2024
@AlexWaygood
Copy link
Member

almost certainly due to #16942

@Hnasar
Copy link
Contributor

Hnasar commented Apr 24, 2024

I also hit this in a few places. Here's the relevant upstream bug python/typeshed#10653

Workaround I used:

from typing import TYPE_CHECKING

class Bar:
    def f(self):
        pass

    if not TYPE_CHECKING:
        f = wraps(Foo.f)(f)

@troiganto
Copy link

I've also run into this. My workaround was to use update_wrapper() (for which wraps() is a convenience wrapper) instead. This is, understandably, completely invisible to Mypy.

from functools import update_wrapper

class Foo:
    def f(self):
        pass

class Bar:
    def f(self):
        pass
    update_wrapper(f, Foo.f)

bar = Bar()
bar.f()

@pp-mo
Copy link

pp-mo commented Jul 23, 2024

We hit this too.
See: SciTools/iris#6033

I guess this is the same problem, but it's not our only usage of "wraps", so I'm not really clear why other uses seem to be OK.

FWIW my workaround is to include an own-brew replacement for "wraps", based on the above hints :

# Use custom version of "wraps", to workaround MyPy v1.10 problem
# See : https://github.com/python/mypy/issues/17166
# TODO: remove when MyPy fixed
def wraps(x):
    def _inner(f):
        return update_wrapper(f, x)

    return _inner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong
Projects
None yet
Development

No branches or pull requests

5 participants