python / python/mypy

tuple element types wrongly mixed as union

Offen
#20,188 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

bug topic-join-v-union
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug Report

When a variable conditionally gets one or the other of a set of fixed-length tuples, instead of being inferred as a union of said tuple types, mypy sees an arbibrary-length tuple whose elements are typed with a union of all element types involved. This results false-positive errors being reported.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.12&gist=4780da0a891d33c46ee7ac0b55a05f24

Expected Behavior

Similar to what pyright reveals, a union of two tuple types with defined length and element types, and no error when assigning :

  /home/user/omaha/omaha/uitest/mypy-test.py:9:17 - information: Type of "item" is "tuple[Literal[1], Literal['a'], list[bool]] | tuple[Literal[1], Literal['a']]"

Actual Behavior

The type is instead inferred to be a tuple of arbitrary length, with all elements allowed to have any of the types seen inside the tuple:

main.py:9: note: Revealed type is "builtins.tuple[builtins.int | builtins.str | builtins.list[builtins.bool], ...]"
main.py:10: error: List item 0 has incompatible type "tuple[int | str | list[bool], ...]"; expected "tuple[int, str] | tuple[int, str, Sequence[bool]]"  [list-item]

Note that when using just item = (1, "a"), removing the conditional part, the tuple is properly identified with fixed types, and the assignment is validated by mypy:

main.py:9: note: Revealed type is "tuple[builtins.int, builtins.str]"

Your Environment

  • Mypy version used: 1.18.2
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12, 3.14.0rc2

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

Führe zunächst die verlinkte mypy-play-Reproduktion mit mypy 1.18.2 aus und vergleiche den angezeigten Typ und die Zuweisungsdiagnose mit dem erwarteten pyright-Ergebnis. Verfolge das Verhalten der Tupel-Inferenz, das an der bedingten Zuweisung beteiligt ist; abgeschlossen ist die Aufgabe, wenn Alternativen von Tupeln fester Länge eine Union bleiben und die gezeigte Zuweisung keinen False Positive mehr erzeugt.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

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

Neue Issues direkt in Ihr Postfach

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