@@ -3657,3 +3657,43 @@ t2.foo = [B()]
36573657t2.foo = [C()]
36583658t2.foo = [1] # E: Value of type variable "T" of "foo" of "Test" cannot be "int"
36593659[builtins fixtures/property.pyi]
3660+
3661+ [case testContextFreeConcatInvariantType]
3662+ from typing import Iterable, Iterator, TypeVar, Generic, Union
3663+
3664+ T = TypeVar("T")
3665+ S = TypeVar("S")
3666+
3667+ class Vec(Generic[T]):
3668+ def getitem(self, i: int) -> T: ... # ensure invariance of T
3669+ def setitem(self, i: int, v: T) -> None: ... # ensure invariance of T
3670+ def __iter__(self) -> Iterator[T]: ...
3671+ def __add__(self, other: "Vec[S]") -> "Vec[Union[T, S]]": ...
3672+
3673+ mix: Vec[Union[int, str]]
3674+ strings: Vec[str]
3675+ mix = mix + strings
3676+ mix = strings + mix
3677+ reveal_type(mix + strings) # N: Revealed type is "__main__.Vec[Union[builtins.int, builtins.str]]"
3678+ reveal_type(strings + mix) # N: Revealed type is "__main__.Vec[Union[builtins.str, builtins.int]]"
3679+ [builtins fixtures/list.pyi]
3680+
3681+
3682+ [case testInContextConcatInvariantType]
3683+ # https://github.com/python/mypy/issues/3933#issuecomment-2272804302
3684+ from typing import Iterable, Iterator, TypeVar, Generic, Union
3685+
3686+ T = TypeVar("T")
3687+ S = TypeVar("S")
3688+
3689+ class Vec(Generic[T]):
3690+ def getitem(self, i: int) -> T: ... # ensure invariance of T
3691+ def setitem(self, i: int, v: T) -> None: ... # ensure invariance of T
3692+ def __iter__(self) -> Iterator[T]: ...
3693+ def __add__(self, other: "Vec[S]") -> "Vec[Union[T, S]]": ...
3694+
3695+ def identity_on_iterable(arg: Iterable[T]) -> Iterable[T]: return arg
3696+ x: Vec[str]
3697+ y: Vec[None]
3698+ reveal_type( identity_on_iterable(y + x) ) # N: Revealed type is "typing.Iterable[Union[None, builtins.str]]"
3699+ reveal_type( identity_on_iterable(x + y) ) # N: Revealed type is "typing.Iterable[Union[builtins.str, None]]"
0 commit comments