Skip to content

mypy infers the wrong type for classmethods #3482

Open
@carljm

Description

@carljm

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrongtopic-descriptorsProperties, class vs. instance attributestopic-runtime-semanticsmypy doesn't model runtime semantics correctly

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions