python / python/mypy

Can't assign result of list addition to list variable covariantly

Offen
#16,773 0 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Bug Report

I cannot assign a list[int] to a list[int | None] if I make it using addition. However, it works fine if I don't use addition. See the tiny example below.

In this example, of course, I can avoid the problem using the list literal [1, 2], but in the application code where I'm facing this issue I actually need to add two comprehensions and store the result as a variable. It shows the same unexpected behavior.

To Reproduce

a: list[int | None] = [1]         # allowed
b: list[int | None] = [1] + [2]   # not allowed

https://mypy-play.net/?mypy=latest&python=3.12&gist=800cfe12c8689a108440e71e641ad240

Expected Behavior

I expect mypy to behave in one of two ways:

  • Both lines are allowed 👈 this would be very convenient
  • Both lines are not allowed 👈 this would be correct
    • How can the first line be allowed if list is invariant?
    • In this case I would need a way to type-hint a list comprehension

Note:

I expect both lines to be treated the same way. This expectation is reinforced because reveal_type([1]) reports the same type as reveal_type([1] + [2]). If that was not the case the different treatment would be easier to understand.

Actual Behavior

main.py:2: error: Incompatible types in assignment (expression has type "list[int]", variable has type "list[int | None]")  [assignment]
main.py:2: note: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance
main.py:2: note: Consider using "Sequence" instead, which is covarian

Your Environment

  • Mypy version used: 1.8.0 and 1.5.1 (latest and version in use on my project)
  • Mypy command-line flags: none
  • Mypy configuration options from mypy.ini (and other config files): none
  • Python version used: 3.12

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 mit der minimalen Reproduktion in main.py und führe sie mit dem verknüpften mypy-play-Beispiel unter den angegebenen Python- und mypy-Versionen aus. Verfolge, wie die Ausdrücke für Listenliterale und Listenaddition inferiert werden, und sorge dann für ein konsistentes Zuweisungsverhalten, wobei die Invarianz von Listen erhalten bleibt; füge einen Regressionstest für beide Formen hinzu oder aktualisiere ihn.

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.