Generic type not inferred when valid constraint available
Open
Nobody has claimed this yet.
bug
topic-type-variables
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
from typing import reveal_type
def foo[T: (int, str, int | str)](*args: T) -> tuple[T, ...]:
return args
a = foo(0)
b = foo('')
c = foo(0, '')
d = foo('', 0)
reveal_type(a)
reveal_type(b)
reveal_type(c)
reveal_type(d)
$ mypy example.py
example.py:10: error: Value of type variable "T" of "foo" cannot be "object" [type-var]
example.py:11: error: Value of type variable "T" of "foo" cannot be "object" [type-var]
example.py:13: note: Revealed type is "builtins.tuple[builtins.int, ...]"
example.py:14: note: Revealed type is "builtins.tuple[builtins.str, ...]"
example.py:15: note: Revealed type is "builtins.tuple[builtins.object, ...]"
example.py:16: note: Revealed type is "builtins.tuple[builtins.object, ...]"
Found 2 errors in 1 file (checked 1 source file)
I'd expect c and d to be of type tuple[int | str, ...] as allowed by the constraints on T
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 example.py reproduction with mypy and compare the revealed types and errors for all four calls. Trace type-variable constraint inference for the generic foo definition; done when the mixed int and str calls are accepted and reveal tuple[int | str, ...].
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
- 45/100