python / python/mypy

Unpacking unions of tuples skips type checking

Open
#15,969 1 comment 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

mypy applies type checks when passing an unpacked tuple to a function, but it seems to skip the checks when passing a variable that is a union of tuple types.

To Reproduce

def inner(_name: str, _num: int) -> None:
    ...

# mypy passes as expected
def fun1(name_and_num: tuple[str, int]) -> None:
    inner(*name_and_num)

# mypy errors as expected
def fun2(name_and_num: tuple[str, float]) -> None:
    inner(*name_and_num)

# mypy passes *but should error*
def fun3(name_and_num: tuple[str, int] | tuple[str, float]) -> None:
    inner(*name_and_num)

https://gist.github.com/mypy-play/257f0a56dd44b405b2853e2854333cc2

Expected Behavior

arg-type error on line 14

Actual Behavior

No error on line 14

Your Environment

  • Mypy version used: 1.5.1
  • Python version used: 3.11.5

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 behavior with the provided fun1, fun2, and fun3 example on Python 3.11.5 using mypy 1.5.1. Trace the unpacked union-of-tuples type-checking path and verify that the call in fun3 reports the expected arg-type error on line 14.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.