python / python/mypy

Wrong "Too many arguments" errors with tuple unpacking

Offen
#9,710 4 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-calls
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Reproduziere die Beispiele in sandbox.py mit mypy in der gemeldeten Python-3.8.5-Umgebung und vergleiche die Diagnosen für foo1, foo2 und foo3. Verfolge die Prüfung von Argumenten beim Entpacken von Tupeln ausgehend vom fehlschlagenden Aufruf und füge anschließend Regressionstests für die erwartete Behandlung jedes Beispiels hinzu und überprüfe, dass die Diagnosen konsistent sind.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
devtools
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.