-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Issues with unions of sequences #13112
Comments
This is a classic of the "join versus union" genre of mypy bugs. It's similar to #8586, #12327, #11440, #10135, etc. #12056 is the meta-issue for this genre of bugs. This is a valid bug report, but I think we have enough bug reports in this genre open already, and we know what the problem is here, so I'm closing this. |
Thanks for looking into it, indeed (1a) and (1b) above fall under the join versus union issue. For (2b) though |
I think 2b) is still a union-vs-join issue. The issue is that an object of type @overload
def iter(__iterable: SupportsIter[_SupportsNextT]) -> _SupportsNextT: ...
@overload
def iter(__iterable: _GetItemIterable[_T]) -> Iterator[_T]: ...
@overload
def iter(__function: Callable[[], _T | None], __sentinel: None) -> Iterator[_T]: ...
@overload
def iter(__function: Callable[[], _T], __sentinel: object) -> Iterator[_T]: ... In order to infer the return type, therefore, I think mypy then performs a |
Observe the difference between these two snippets: from typing import Sequence, Union
import random
x: Sequence[int] = [1, 2, 3]
y: Sequence[str] = ["a", "b", "c"]
z: Union[Sequence[int], Sequence[str]] = x if random.random() > 0.5 else y
a: Sequence[Union[int, str]] = x if random.random() > 0.5 else y # type: ignore
reveal_type(iter(z)) # object
reveal_type(iter(a)) # Union[int, str] https://mypy-play.net/?mypy=latest&python=3.10&gist=41cd61709ac31f3c53096035db8eee3d |
Ok I see. I subscribed to the meta-issue, when there's a resolution I'll try this again and reopen the issue if needed. |
Consider the following example:
❌ 1a. Without an explicit annotation for
z
, its inferred type isSequence[object]
instead ofUnion[Sequence[int], Sequence[str]]
:❌ 1b. A
map
over an overloaded function seems to take into account only the first overload:✔️ 2a. Annotating
z
explicitly fixes some issues by preserving the Union:❌ 2b. But not all of them;
iter
andnext
fall back toobject
:The text was updated successfully, but these errors were encountered: