Skip to content

classmethod of a generic class can't be called through an instance #1386

Closed
@rwbarton

Description

@rwbarton

I came across this while looking at #1337, but this is a separate issue.

from typing import TypeVar, Generic
T = TypeVar('T')
class A(Generic[T]):
    @classmethod
    def foo(cls, x: int) -> int: pass
    @staticmethod
    def bar(x: int) -> int: pass

a = A()  # type: A[str]
a.foo(2) # E: Invalid class method type
a.bar(2) # ok

If the class A is not generic, then both calls are accepted. So I think this is a bug.

The error arises in check_method_type when checking that the type of the instance (a) matches the return type of the argument cls as a Callable. But the latter type here is A[T-1], while the type of aisA[str]`.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugmypy got something wrong

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions