Skip to content

Commit 34d8603

Browse files
committed
Fix iteration over union (when self type is involved) (#17976)
Fixes #17945
1 parent 2485bed commit 34d8603

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

mypy/checkexpr.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3805,6 +3805,7 @@ def check_method_call_by_name(
38053805
is_operator=True,
38063806
msg=self.msg,
38073807
original_type=original_type,
3808+
self_type=base_type,
38083809
chk=self.chk,
38093810
in_literal_context=self.is_literal_context(),
38103811
)

test-data/unit/check-selftype.test

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2160,3 +2160,19 @@ class MyProtocol(Protocol):
21602160

21612161
def test() -> None: ...
21622162
value: MyProtocol = test
2163+
2164+
[case testSelfTypeUnionIter]
2165+
from typing import Self, Iterator, Generic, TypeVar, Union
2166+
2167+
T = TypeVar("T")
2168+
2169+
class range(Generic[T]):
2170+
def __iter__(self) -> Self: ...
2171+
def __next__(self) -> T: ...
2172+
2173+
class count:
2174+
def __iter__(self) -> Iterator[int]: ...
2175+
2176+
def foo(x: Union[range[int], count]) -> None:
2177+
for item in x:
2178+
reveal_type(item) # N: Revealed type is "builtins.int"

0 commit comments

Comments
 (0)