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
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.
changed the title [-]mypy doesn't know that classmethods have a __func__ attribute[/-][+]mypy infers the wrong type for classmethods[/+]on Jun 23, 2017
Activity
refi64 commentedon 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 commentedon Jun 1, 2017
Yeah, mypy needs to be much more careful with these things.
[-]mypy doesn't know that classmethods have a __func__ attribute[/-][+]mypy infers the wrong type for classmethods[/+]ilevkivskyi commentedon Dec 20, 2019
Note that this also applies to static methods, see for example #8181