Typing narrowing fails depending on the order of if clauses
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 42/100
Direzione di ricerca
Inizia eseguendo la riproduzione in bug.py con mypy 1.4.1 o l'esempio mypy-play collegato, confrontando l'ordine delle clausole che falliscono e di quelle che funzionano. Traccia il restringimento del tipo di string, string_a e string_b attraverso le istruzioni if. Il lavoro è completato quando gli errori arg-type segnalati vengono eliminati senza modificare il comportamento previsto.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
Bug Report
Type narrowing fails in the code below.
To Reproduce
def takes_one_str(string: str) -> str:
return string
# just the problematic case -> failing
def takes_two_optional_str0a(string: str | None, whatever: bool) -> str:
if not (whatever and (string is None)) and whatever:
return takes_one_str(string)
return ""
# reformulated by reversing the order of if clauses -> working
def takes_two_optional_str0b(string: str | None, whatever: bool) -> str:
if whatever and not (whatever and (string is None)):
return takes_one_str(string)
return ""
# what was I actually trying to achieve -> failing
def takes_two_optional_str1(string_a: str | None, string_b: str | None) -> str:
if (string_a is None) and (string_b is None):
return takes_one_str("")
if string_a is None:
# false-positive arg-type (same without return, with elif)
# string_b cannot be None
return takes_one_str(string_b)
if string_b is None:
return takes_one_str(string_a)
return takes_one_str(string_a + string_b)
# workaround variant -> working
def takes_two_optional_str2(string_a: str | None, string_b: str | None) -> str:
if (string_a is not None) and (string_b is not None):
return takes_one_str(string_a + string_b)
if string_a is not None:
# false-positive arg-type (same without return, with elif)
return takes_one_str(string_a)
if string_b is not None:
return takes_one_str(string_b)
return takes_one_str("")
https://mypy-play.net/?mypy=latest&python=3.11&gist=3f3ce6b3021c1a71a4ecf31d35f62a98
Expected Behavior
No errors
Actual Behavior
bug.py:8: error: Argument 1 to "takes_one_str" has incompatible type "str | None"; expected "str" [arg-type]
bug.py:26: error: Argument 1 to "takes_one_str" has incompatible type "str | None"; expected "str" [arg-type]
Found 2 errors in 1 file (checked 1 source file)
Your Environment
- Mypy version used: mypy 1.4.1 (compiled: yes)
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.11.4
- Lingua principale
- Python
- Stelle
- 20.6k
- Fork
- 3.3k
- Merge medio
- 1g 18h
- PR unite (30g)
- 54
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.
Altre issue di python/mypy
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 75/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 76/100
-
documentation
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
bug topic-configuration topic-error-reporting
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
Issue simili
-
🐛 Bug 🔔 Pending processing
Difficoltà 2/5 1-3 ore Idoneità per principianti 84/100
jumpserver/jumpserver#17584 ·
-
link-check link-check:sphinx-theme
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
qgis/QGIS-Documentation#11275 ·
-
bug priority:normal ready-for-dev
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
OpenHands/extensions#626 · 1 commento ·
-
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 90/100
CSCfi/sd-search-api#39 ·