python / python/mypy

Variadic `*args` where `args: Union[Tuple[...], Tuple[...], ...]` are _never_ checked [False Negative]

Open
#17,161 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

When args is a Union of two or more fixed-length Tuples, and some_func() is callable, the following call is always accepted without error (regardless of what's in the tuples!):

some_func(*args)

To Reproduce

[mypy-play.net]

from __future__ import annotations

from typing import (
        Tuple,
        Union,
        )

class ClassA: pass
class ClassB: pass
class ClassC: pass

def some_func(value: ClassA, /) -> None:
    pass

u_bc: Union[Tuple[ClassB], Tuple[ClassC]]
some_func(*u_bc)  # expected: [type-arg]; got: no error

Expected Behavior

Mypy should notice that one or more of the Tuples that make up the Union are not appropriate for the callable, and complain about the argument types accordingly.

Put another way, mypy should examine each possibility posed by the Union in turn, and complain if any of them isn't suitable for the variadic call.

Actual Behavior

Mypy always accepts variadic arguments of this form, without any regard to the contents of the Tuples that make up the Union.

Your Environment

  • Mypy version used: 1.9.0 (also checked 1.5, 1.0; this doesn't appear to be a recent regression)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.8, 3.12

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

The mypy-play.net reproducer is the starting point; run the shown Python 3.12 example and inspect mypy's handling of variadic *args from a Union of fixed-length Tuples. Done means the call reports an argument-type error for incompatible tuple alternatives, as described under Expected Behavior.

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.