Regression: Type of instance variable narrowed incorrectly after `is not` expression
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- PR-Merge-Kennzahlen
- PR-Kennzahlen ausstehend
Beschreibung
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
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Führe zunächst den bereitgestellten Reproducer mit mypy aus und vergleiche die angezeigten Typen für x und a.foo. Verfolge den Type-Narrowing-Pfad für den Ausdruck a.foo is not list. Als erledigt gilt, wenn a.foo weiterhin als Any typisiert bleibt und der spätere Aufruf append(x) den gezeigten Argumentfehler nicht mehr meldet.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- python
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 3/5
- Geschätzter Aufwand
- 1-2 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 38/100