`tuple` of different `type`s narrows to `object`
Open
Nobody has claimed this yet.
bug
topic-join-v-union
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Bug Report
mypy fails to infer the type variable in a arbitrary length tuple.
To Reproduce
from typing import TypeVar, reveal_type
T = TypeVar('T')
def get_type_hint(val_type: tuple[type[T]] | tuple[type[T], ...]) -> T:
return None # type: ignore
a: tuple[type[int | str], ...] = (str, int)
reveal_type(a)
reveal_type(get_type_hint((str,)))
reveal_type(get_type_hint((str, int)))
reveal_type(get_type_hint(a))
Expected Behavior
The reveal_type for calling (str, int) and a should be int | str.
Actual Behavior
Type is inferred to narrow type as object.
tuple_infer.py:10: note: Revealed type is "builtins.tuple[Union[Type[builtins.int], Type[builtins.str]], ...]"
tuple_infer.py:11: note: Revealed type is "builtins.str"
tuple_infer.py:12: note: Revealed type is "builtins.object"
tuple_infer.py:13: note: Revealed type is "builtins.object"
Your Environment
- Mypy version used: 1.1.1
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.11.1
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
Reproduce the example in tuple_infer.py with mypy 1.1.1 and inspect the reveal_type output for the heterogeneous tuple calls. Trace the type inference behavior behind get_type_hint; done when the calls with (str, int) and a reveal int | str rather than object.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100