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
Contravariant type args in method return is correctly identified as type errors; but covariant type args in method arguments does not create an error:
T_co = TypeVar('T_co', covariant=True)
class Covariant(Generic[T_co]):
def f(x: T_co) -> T_co: # Need an error here
return x
T_contra = TypeVar('T_contra', contravariant=True)
class Contravariant(Generic[T_contra]):
def f(x: T_contra) -> T_contra: # E: Cannot use a contravariant type variable as return type
return x
It works correctly for normal functions though (not methods).
The text was updated successfully, but these errors were encountered:
This is because x in your example is interpreted as self. The self in methods is indeed special (it actually varies covariantly in some sense). If I change your example as following:
Contravariant type args in method return is correctly identified as type errors; but covariant type args in method arguments does not create an error:
It works correctly for normal functions though (not methods).
The text was updated successfully, but these errors were encountered: