Can't assign result of list addition to list variable covariantly
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
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
listis invariant? - In this case I would need a way to type-hint a list comprehension
- How can the first line be allowed if
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.0and1.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
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con la riproduzione minima in main.py ed eseguila con l’esempio mypy-play collegato usando le versioni di Python e mypy indicate. Traccia il modo in cui vengono inferite le espressioni di letterale di lista e di somma di liste, quindi rendi coerente il loro comportamento di assegnazione preservando l’invarianza delle liste; aggiungi o aggiorna un test di regressione per entrambe le forme.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- devtools
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100