diff --git a/mypy/subtypes.py b/mypy/subtypes.py index cb630c68b62a..49689db7ea41 100644 --- a/mypy/subtypes.py +++ b/mypy/subtypes.py @@ -547,8 +547,7 @@ def f(self) -> A: ... return False assuming = right.type.assuming_proper if proper_subtype else right.type.assuming for (l, r) in reversed(assuming): - if (mypy.sametypes.is_same_type(l, left) - and mypy.sametypes.is_same_type(r, right)): + if l == left and r == right: return True with pop_on_exit(assuming, left, right): for member in right.type.protocol_members: diff --git a/mypy/test/testtypes.py b/mypy/test/testtypes.py index e9e169ccc84e..0600c333944d 100644 --- a/mypy/test/testtypes.py +++ b/mypy/test/testtypes.py @@ -1055,6 +1055,7 @@ def setUp(self) -> None: self.fx = TypeFixture() def test_literal_type(self) -> None: + a = self.fx.a b = self.fx.b # Reminder: b is a subclass of a lit1 = self.fx.lit1 @@ -1064,6 +1065,7 @@ def test_literal_type(self) -> None: self.assert_same(lit1, lit1) self.assert_same(UnionType([lit1, lit2]), UnionType([lit1, lit2])) self.assert_same(UnionType([lit1, lit2]), UnionType([lit2, lit1])) + self.assert_same(UnionType([a, b]), UnionType([b, a])) self.assert_not_same(lit1, b) self.assert_not_same(lit1, lit2) self.assert_not_same(lit1, lit3) diff --git a/test-data/unit/check-unions.test b/test-data/unit/check-unions.test index e8921f30cdcf..08c183cb3ed1 100644 --- a/test-data/unit/check-unions.test +++ b/test-data/unit/check-unions.test @@ -1059,3 +1059,100 @@ x: Union[None, Any] y: Union[int, None] reveal_type(f(x, y)) # N: Revealed type is "Union[None, Any, builtins.int]" reveal_type(f(y, x)) # N: Revealed type is "Union[builtins.int, None, Any]" + +[case testNestedProtocolUnions] +from typing import Union, Iterator, Iterable +def foo( + values: Union[ + Iterator[Union[ + Iterator[Union[Iterator[int], Iterable[int]]], + Iterable[Union[Iterator[int], Iterable[int]]], + ]], + Iterable[Union[ + Iterator[Union[Iterator[int], Iterable[int]]], + Iterable[Union[Iterator[int], Iterable[int]]], + ]], + ] +) -> Iterator[int]: + for i in values: + for j in i: + for k in j: + yield k +foo([[[1]]]) +[builtins fixtures/list.pyi] + +[case testNestedProtocolGenericUnions] +from typing import Union, Iterator, List +def foo( + values: Union[ + Iterator[Union[ + Iterator[Union[Iterator[int], List[int]]], + List[Union[Iterator[int], List[int]]], + ]], + List[Union[ + Iterator[Union[Iterator[int], List[int]]], + List[Union[Iterator[int], List[int]]], + ]], + ] +) -> Iterator[int]: + for i in values: + for j in i: + for k in j: + yield k +foo([[[1]]]) +[builtins fixtures/list.pyi] + +[case testNestedProtocolGenericUnionsDeep] +from typing import TypeVar, Union, Iterator, List +T = TypeVar("T") +Iter = Union[Iterator[T], List[T]] +def foo( + values: Iter[Iter[Iter[Iter[Iter[int]]]]], +) -> Iterator[int]: + for i in values: + for j in i: + for k in j: + for l in k: + for m in l: + yield m +foo([[[[[1]]]]]) +[builtins fixtures/list.pyi] + +[case testNestedInstanceUnsimplifiedUnion] +from typing import TypeVar, Union, Iterator, List, Any +T = TypeVar("T") + +Iter = Union[Iterator[T], List[T]] +def foo( + values: Iter[Union[Any, Any]], +) -> Iterator[Any]: + for i in values: + yield i +foo([1]) +[builtins fixtures/list.pyi] + +[case testNestedInstanceTypeAlias] +from typing import TypeVar, Union, Iterator, List, Any +T = TypeVar("T") + +Iter = Union[Iterator[T], List[T]] +def foo( + values: Iter["Any"], +) -> Iterator[Any]: + for i in values: + yield i +foo([1]) +[builtins fixtures/list.pyi] + +[case testNestedInstanceTypeAliasUnsimplifiedUnion] +from typing import TypeVar, Union, Iterator, List, Any +T = TypeVar("T") + +Iter = Union[Iterator[T], List[T]] +def foo( + values: Iter["Union[Any, Any]"], +) -> Iterator[Any]: + for i in values: + yield i +foo([1]) +[builtins fixtures/list.pyi]