Closed
Description
Mypy associates class type variables to generic class methods in some contexts, but this is inconsistent, which results in type unsafety. Here is an example that mypy is okay with but that is actually unsafe:
from typing import TypeVar, Generic
T = TypeVar('T')
T2 = TypeVar('T2')
class C(Generic[T]):
@classmethod
def f(cls, x: T) -> T:
return x
@classmethod
def g(cls, x: T2) -> T2:
cls.f(x) # This is accepted
return x
class D(C[int]):
@classmethod
def f(cls, x: int) -> int: # This override is accepted
return x + 1
D.g('') # Runtime error!
If the methods are instance methods, T2
is not treated as compatible with T
, however.