File tree 2 files changed +19
-1
lines changed
2 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -582,7 +582,10 @@ def is_implicit_any(t: Type) -> bool:
582
582
elif isinstance (arg_type , TypeVarType ):
583
583
# Refuse covariant parameter type variables
584
584
# TODO: check recursively for inner type variables
585
- if arg_type .variance == COVARIANT :
585
+ if (
586
+ arg_type .variance == COVARIANT and
587
+ defn .name () not in ('__init__' , '__new__' )
588
+ ):
586
589
self .fail (messages .FUNCTION_PARAMETER_CANNOT_BE_COVARIANT , arg_type )
587
590
if typ .arg_kinds [i ] == nodes .ARG_STAR :
588
591
# builtins.tuple[T] is typing.Tuple[T, ...]
Original file line number Diff line number Diff line change @@ -1942,6 +1942,21 @@ def new_pro_user(user_class: Type[ProUser]):
1942
1942
new_user(user_class)
1943
1943
[out]
1944
1944
1945
+ [case testAllowCovariantArgsInConstructor]
1946
+ from typing import Generic, TypeVar
1947
+
1948
+ T_co = TypeVar('T_co', covariant=True)
1949
+
1950
+ class C(Generic[T_co]):
1951
+ def __init__(self, x: T_co) -> None: # This should be allowed
1952
+ self.x = x
1953
+ def meth(self) -> None:
1954
+ reveal_type(self.x) # E: Revealed type is 'T_co`1'
1955
+
1956
+ reveal_type(C(1).x) # E: Revealed type is 'builtins.int*'
1957
+ [builtins fixtures/property.pyi]
1958
+ [out]
1959
+
1945
1960
[case testTypeUsingTypeCErrorCovariance]
1946
1961
from typing import Type, TypeVar
1947
1962
class User: pass
You can’t perform that action at this time.
0 commit comments