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 infers the wrong type for classmethods #3482

Open
carljm opened this issue Jun 1, 2017 · 3 comments
Open

mypy infers the wrong type for classmethods #3482

carljm opened this issue Jun 1, 2017 · 3 comments
Labels
bug mypy got something wrong topic-descriptors Properties, class vs. instance attributes topic-runtime-semantics mypy doesn't model runtime semantics correctly

Comments

@carljm
Copy link
Member

carljm commented Jun 1, 2017

We have some code that simplifies to this:

class A:
    @classmethod
    def hi(cls):
        pass

    bye = classmethod(hi.__func__)

(The real code is wrapping an async classmethod with a sync version; there's another async-to-sync wrapper in there.)

Mypy doesn't like this code because it doesn't know that classmethods have a __func__ attribute. The typeshed annotation for classmethod includes __func__, but mypy doesn't infer the real classmethod type for hi (which is the actual type at runtime), it just infers a general callable type.

This may not be easily fixed until we have protocol support and can more easily have various callable subtypes?

I'm also OK if this is closed as "just use cast() if you're accessing private attributes of a classmethod"; it's certainly not high priority.

@refi64
Copy link
Contributor

refi64 commented Jun 1, 2017

I think the root cause of this is roughly the same as #2267: descriptors are treated as callables inside the class definition.

@gvanrossum
Copy link
Member

Yeah, mypy needs to be much more careful with these things.

@carljm carljm changed the title mypy doesn't know that classmethods have a __func__ attribute mypy infers the wrong type for classmethods Jun 23, 2017
@ilevkivskyi
Copy link
Member

Note that this also applies to static methods, see for example #8181

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug mypy got something wrong topic-descriptors Properties, class vs. instance attributes topic-runtime-semantics mypy doesn't model runtime semantics correctly
Projects
None yet
Development

No branches or pull requests

5 participants