python / python/mypy

Function rejects tuple argument when argument type context is a union involving a tuple-bound typevar

Open
#21,520 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug
Dominant language
Python
Stars
20.6k
Forks
3.3k
PR merge metrics
PR metrics pending

Description

Bug Report, To Reproduce, and Actual Behaviour

Mypy rejects this example (see the line containing reveal_type(f(b)))

# mypy: disable-error-code=empty-body

type VarTuple[T] = tuple[T, ...]

class MyTuple(tuple[int, str]): ...

def f[T: str | MyTuple](arg: T | VarTuple[T], /) -> T: ...
def f2[T: MyTuple](arg: T | VarTuple[T], /) -> T: ...

b = MyTuple((1, ""))
reveal_type(f(b))  # E: Argument 1 to "f" has incompatible type "MyTuple"; expected "tuple[Never, ...]"  [arg-type] \
                   # N: Revealed type is "Never"
reveal_type(f2(b))  # N: Revealed type is "tuple[int, str, fallback=__main__.MyTuple]"

Expected Behavior

No errors and revealed type for f(b) should be same as for f2(b).

Your Environment

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the provided example in the linked mypy playground and compare the results for f and f2. Trace the type inference and argument-checking path for a union involving a tuple-bound type variable, then add a regression test for the example; done means f(b) is accepted and reveals the same type as f2(b).

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
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.