-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Description
Bug Report
mypy infers an incorrectly broad type in some cases where collections are constructed from a collection whose type is a union. In particular, if the input to a collection constructor is a union of collections with homogeneous element types, the type that mypy infers ignores the fact that the inputs are homogeneous. The type mypy infers is not wrong (in that the actual type will be covered by it), but it is unnecessarily broad (it matches types which are obviously not possible -- i.e., a collection with heterogeneous element types).
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=04f25887cd105eefebfdcbdbd3c03665
https://gist.github.com/mypy-play/04f25887cd105eefebfdcbdbd3c03665
from typing import Generator
from typing_extensions import reveal_type
list_union: list[int] | list[None]
set_union: set[int] | set[None]
tuple_union: tuple[int, ...] | tuple[None, ...]
generator_union: Generator[int, None, None] | Generator[None, None, None]
# Expected: "Union[builtins.list[builtins.int], builtins.list[None]]"
# Actual: "builtins.list[Union[builtins.int, None]]"
reveal_type(list(list_union))
reveal_type(list(set_union))
reveal_type(list(tuple_union))
reveal_type([x for x in generator_union])
# Expected: "Union[builtins.set[builtins.int], builtins.set[None]]"
# Actual: "builtins.set[Union[builtins.int, None]]"
reveal_type(set(list_union))
reveal_type(set(set_union))
reveal_type(set(tuple_union))
reveal_type({x for x in generator_union})
# Expected: "Union[builtins.tuple[builtins.int, ...], builtins.tuple[None, ...]]"
# Actual: "builtins.tuple[Union[builtins.int, None], ...]"
reveal_type(tuple(list_union))
reveal_type(tuple(set_union))
reveal_type(tuple(tuple_union))
reveal_type(tuple(x for x in generator_union))
Expected Behavior
Because mypy knows that the argument to the collection constructor is a collection whose elements are all of the same type, I assumed it would thus realize that it would result in a collection whose elements are all of the same type.
Actual Behavior
mypy incorrectly reports that the resulting collection may have heterogeneous element types.
Your Environment
- Mypy version used: 1.15.0
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini
(and other config files): None - Python version used: 3.12