Optional types not fully narrowed with multiple conditionals
Dieses Issue hat noch niemand übernommen.
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 35/100
Rechercherichtung
Reproduziere das Beispiel in sandbox.py mit mypy 1.7.0, Python 3.12 und ohne Konfigurationsflags und verfolge dann das Verhalten der Typverengung für die mehreren Bedingungen. Die Arbeit ist abgeschlossen, wenn y als int erkannt wird und die Rückgabe ohne den gemeldeten Fehler wegen eines nicht unterstützten Operanden durchläuft.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Bug Report
Mypy does not fully narrow optional types when multiple conditionals are involved. I suspect this is because it is not able to make an inference step when performing type narrowing. e.g., below, you have facts ~ (x is None and y is None) and x is None on line 6, which allows you to narrow y: int | None to y: int. y: None can't happen, because y is None -> ~ x is None -> contradiction given the above facts.
To Reproduce
def f(x: int | None, y: int | None) -> int:
if x is None and y is None:
return 0
if x is None:
# y guaranteed non-null
reveal_type(y) # int | None, but should be int
return y + 1
Expected Behavior
mypy should narrow y to int.
Actual Behavior
mypy is not able to narrow y from int | None, and gives a false positive error:
sandbox.py:5: note: Revealed type is "Union[builtins.int, None]"
sandbox.py:6: error: Unsupported operand types for + ("None" and "int") [operator]
sandbox.py:6: note: Left operand is of type "int | None"
Your Environment
- Mypy version used: 1.7.0
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files): none - Python version used: 3.12.0
- Vorherrschende Sprache
- Python
- Sterne
- 20.6k
- Forks
- 3.3k
- Ø Merge
- 1 T. 18 Std.
- Gemergte PRs (30 T.)
- 54
Beitragsleitfaden
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus python/mypy
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 75/100
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
-
bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 76/100
-
documentation
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
-
bug topic-configuration topic-error-reporting
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 68/100
Ähnliche Issues
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
bancolombia/sentinel#23 ·
-
test md OffenCI
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 88/100
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 74/100