Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix iteration over union (when self type is involved) #17976

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
Loading