Regression: Type of instance variable narrowed incorrectly after `is not` expression
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
Type of a.foo in the example below is narrowed to what looks like a function type which results in a strange error on the call to append later.
Issue started with https://github.com/python/mypy/commit/ad30be89f146adaae398bd854146952158a53cad @hauntsaninja
To Reproduce
from typing import Any
def f(a: Any, x: Any) -> None:
if x is not list:
x = []
reveal_type(x) # note: Revealed type is "Any"
if a.foo is not list:
a.foo = []
reveal_type(a.foo) # note: Revealed type is "Any | Overload(def [_T] () -> builtins.list[_T`1], def [_T] (typing.Iterable[_T`1]) -> builtins.list[_T`1])"
a.foo.append(x) # error: Too few arguments for "append" of "list" [call-arg]
Expected Behavior
a.foo should probably be typed as Any.
Actual Behavior
/Users/psawicki/test.py:6: note: Revealed type is "Any"
/Users/psawicki/test.py:9: note: Revealed type is "Any | Overload(def [_T] () -> builtins.list[_T`1], def [_T] (typing.Iterable[_T`1]) -> builtins.list[_T`1])"
/Users/psawicki/test.py:10: error: Too few arguments for "append" of "list" [call-arg]
Your Environment
- Mypy version used: mypy 1.20.0+dev.ad30be89f146adaae398bd854146952158a53cad and up
- Mypy command-line flags:
mypy ~/test.py - Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.13.5
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 eseguendo il riproduttore fornito con mypy e confronta i tipi rivelati per x e a.foo. Traccia il percorso di restringimento del tipo per l’espressione a.foo is not list. Il lavoro è completato quando a.foo rimane tipizzato come Any e la chiamata successiva a append(x) non segnala più l’errore di argomento mostrato.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 38/100