tuple element types wrongly mixed as union
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
When a variable conditionally gets one or the other of a set of fixed-length tuples, instead of being inferred as a union of said tuple types, mypy sees an arbibrary-length tuple whose elements are typed with a union of all element types involved. This results false-positive errors being reported.
To Reproduce
https://mypy-play.net/?mypy=latest&python=3.12&gist=4780da0a891d33c46ee7ac0b55a05f24
Expected Behavior
Similar to what pyright reveals, a union of two tuple types with defined length and element types, and no error when assigning :
/home/user/omaha/omaha/uitest/mypy-test.py:9:17 - information: Type of "item" is "tuple[Literal[1], Literal['a'], list[bool]] | tuple[Literal[1], Literal['a']]"
Actual Behavior
The type is instead inferred to be a tuple of arbitrary length, with all elements allowed to have any of the types seen inside the tuple:
main.py:9: note: Revealed type is "builtins.tuple[builtins.int | builtins.str | builtins.list[builtins.bool], ...]"
main.py:10: error: List item 0 has incompatible type "tuple[int | str | list[bool], ...]"; expected "tuple[int, str] | tuple[int, str, Sequence[bool]]" [list-item]
Note that when using just item = (1, "a"), removing the conditional part, the tuple is properly identified with fixed types, and the assignment is validated by mypy:
main.py:9: note: Revealed type is "tuple[builtins.int, builtins.str]"
Your Environment
- Mypy version used: 1.18.2
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.12, 3.14.0rc2
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the linked mypy-play reproduction with mypy 1.18.2 and compare the revealed type and assignment diagnostic with the expected pyright result. Trace the tuple inference behavior involved in the conditional assignment; done means fixed-length tuple alternatives remain a union and the shown assignment no longer produces a false positive.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 42/100