python / python/mypy

Wrong "Too many arguments" errors with tuple unpacking

Open
#9,710 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug Report

I'm getting wrong or misleading "Too many arguments" errors when using tuple unpacking in function calls.

To Reproduce

from typing import Tuple

def x(x: int, y: int) -> None:
    print(x, y)

def foo1(t: tuple) -> None:
    x(*t[:1], 1)

def foo2(t: tuple) -> None:
    x(*t[:2])

def foo3(t: Tuple[int]) -> None:
    x(*t[:1], 1)

mypy reports:

$ mypy sandbox.py 
sandbox.py:7: error: Too many arguments for "x"
Found 1 error in 1 file (checked 1 source file)

(Line 7 is the call to x in foo1)

Expected Behavior

The call to x in foo1 will never have too many arguments -- it has either 1 (if t is empty) or 2 (if t is not empty). I would have expected an error regarding the possibility of t being empty (i.e. "Potentially not enough arguments").

Also I think that foo1 and foo2 share the same issues regarding their calls of x, so I would have expected to get the same errors for both of them.

Actual Behavior

The call to x in foo1 is reported, but the call to x in foo2 isn't. Interestingly, fixing the tuple length (as in foo3) seems to fix the issue.

Your Environment

  • Mypy version used: 0.790
  • Mypy command-line flags: None
  • Mypy configuration options from mypy.ini (and other config files): None
  • Python version used: 3.8.5
  • Operating system and version: Ubuntu 18.04.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

Reproduce the examples in sandbox.py using mypy with the reported Python 3.8.5 environment and compare diagnostics for foo1, foo2, and foo3. Trace tuple-unpacking argument checking from the failing call, then add regression coverage for the expected handling of each example and verify the diagnostics are consistent.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.