Skip to content

Commit

Permalink
Fix iteration over union (when self type is involved) (python#17976)
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja authored Oct 17, 2024
1 parent 61ad5a4 commit 539513a
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 @@ -3814,6 +3814,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 539513a

Please sign in to comment.