Skip to content

Commit

Permalink
Fix iteration over union (when self type is involved) (#17976)
Browse files Browse the repository at this point in the history
Fixes #17945
  • Loading branch information
hauntsaninja committed Oct 18, 2024
1 parent 2485bed commit 34d8603
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
1 change: 1 addition & 0 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -3805,6 +3805,7 @@ def check_method_call_by_name(
is_operator=True,
msg=self.msg,
original_type=original_type,
self_type=base_type,
chk=self.chk,
in_literal_context=self.is_literal_context(),
)
Expand Down
16 changes: 16 additions & 0 deletions test-data/unit/check-selftype.test
Original file line number Diff line number Diff line change
Expand Up @@ -2160,3 +2160,19 @@ class MyProtocol(Protocol):

def test() -> None: ...
value: MyProtocol = test

[case testSelfTypeUnionIter]
from typing import Self, Iterator, Generic, TypeVar, Union

T = TypeVar("T")

class range(Generic[T]):
def __iter__(self) -> Self: ...
def __next__(self) -> T: ...

class count:
def __iter__(self) -> Iterator[int]: ...

def foo(x: Union[range[int], count]) -> None:
for item in x:
reveal_type(item) # N: Revealed type is "builtins.int"

0 comments on commit 34d8603

Please sign in to comment.