python / python/mypy

variance okay for literals, but not expressions ([1, 2] okay for list[int | None] but [1] + [2] is error)

Offen
#16,623 1 Kommentar 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

a: list[int | None] = [1] + [2] + [None]
b: list[int | None] = [1] + [2]
c: list[int | None] = [1, 2]

a is okay, b is an error, c is okay

Apologies if this is a repeat / common issue. I couldn't find anything in the docs. This is popping up for me a lot, my interpretation is that mypy allows type invariance on literals that it will not for expressions.

To Reproduce

https://mypy-play.net/?mypy=latest&python=3.11&gist=b999a1bd2e975645b868c0b376c910e8

Expected Behavior

Either b and c should both be errors or (preferably) neither.

Actual Behavior

b is an error but c isn't

Your Environment

  • Mypy version used: 1.7.1 and master
  • Python version used: 3.11

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

Beginne damit, die verlinkte mypy-play-Reproduktion mit mypy 1.7.1 oder master und Python 3.11 auszuführen, und vergleiche die Diagnosen für die Zuweisungen a, b und c. Fertig ist die Aufgabe, wenn sich das Verhalten für [1] + [2] gemäß dem im Issue angegebenen erwarteten Verhalten mit dem Verhalten für [1, 2] deckt.

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

Neue Issues direkt in Ihr Postfach

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