Generic matching error when tuple-unpacking result
Open
Nobody has claimed this yet.
bug
topic-inference
topic-type-variables
topic-union-types
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
Here, in caller_bad, immediately unpacking the result leads to the second type variable getting bound to int | str instead of just int
from typing import TypeVar, Generic
T = TypeVar('T')
U = TypeVar('U')
class Test(Generic[T, U]):
pass
def test(gen: Test[T, U]) -> tuple[T, U | str]:
raise NotImplementedError
def call() -> Test[str, int]:
raise NotImplementedError
def caller_bad() -> None:
x, y = test(call())
def caller_ok() -> None:
res = test(call())
x, y = res
_bug.py:16: error: Argument 1 to "test" has incompatible type "Test[str, int]"; expected "Test[str, Union[int, str]]" [arg-type]
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
Use the supplied _bug.py reproducer as the starting point; then trace mypy's generic inference for direct tuple unpacking versus assigning the result first. Done means caller_bad no longer reports the incompatible Test[str, int] argument while caller_ok remains valid, with a regression test covering both forms.
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
- Clearly specified
- Newbie friendliness
- 42/100