Typing narrowing fails depending on the order of if clauses
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 42/100
Línea de trabajo
Comienza ejecutando la reproducción en bug.py con mypy 1.4.1 o el ejemplo enlazado de mypy-play, comparando el orden de las cláusulas que fallan y las que funcionan. Traza el estrechamiento de tipos de string, string_a y string_b a través de las instrucciones if. Se considera terminado cuando se eliminan los errores arg-type indicados sin cambiar el comportamiento previsto.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
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
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Merge medio
- 1 d 18 h
- PR fusionados (30 d)
- 54
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Más de python/mypy
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 75/100
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 76/100
-
documentation
Dificultad 2/5 1-3 horas Aptitud para principiantes 72/100
-
bug topic-configuration topic-error-reporting
Dificultad 2/5 1-3 horas Aptitud para principiantes 68/100
Todos los issues de python/mypy
Issues similares
-
bug
Dificultad 2/5 1-3 horas Aptitud para principiantes 86/100
zostera/django-bootstrap4#894 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 78/100
use-agent-os/agent-os#3276 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
zephyrproject-rtos/zephyr#119726 ·
-
area/auth bug comp/agent P3 platform/discord type/security
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
NousResearch/hermes-agent#117848 ·
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 82/100
zilliztech/memsearch#759 ·