Assigning a variable from either of two optional variables
Nadie ha tomado este issue todavía.
Evaluación
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Aptitud para principiantes
- 35/100
Línea de trabajo
Usa el reproductor de Python proporcionado como primer punto de entrada y ejecuta mypy sobre la asignación x or y después del retorno anticipado. Traza cómo se analiza este flujo de control; se considera terminado cuando el ejemplo acepta other como str sin debilitar el comportamiento de Optional informado en otras partes.
Escrito por el modelo de indexación a partir del texto del issue.
Descripción
Bug Report
consider the following example:
from typing import Optional
def test_something(x: Optional[str] = None, y: Optional[str] = None) -> str:
if not (x or y):
return "early"
other: str = x or y # <<< mypy complains that other should be Optional[str]
return other
am i not able to assume that other would be of type str given that if neither x nor y is given then the function would return early and the assignment of other would never happen? in other words, at the assignment of other, surely one or both of x or y would be of type str, therefore other would be of type str?
to further the argument, if i modify the example like so:
#!/usr/bin/env python3
from typing import Optional
def test_something(x: Optional[str] = None, y: Optional[str] = None) -> str:
if not (x or y):
return "early"
other: str = x or y
print(f"x = {x}")
print(f"y = {y}")
print(f"other class: {type(other)}")
print()
return other
test_something()
test_something(x="hello")
test_something(y="goodbye")
test_something(x="hello", y="goodbye")
and run it i get:
x = hello
y = None
other class: <class 'str'>
x = None
y = goodbye
other class: <class 'str'>
x = hello
y = goodbye
other class: <class 'str'>
To Reproduce
run mypy on the example above
Expected Behavior
mypy would not complain about typing other as str
Actual Behavior
running mypy on the above example yields:
.../test.py: note: In function "test_something":
.../test.py:8:18: error: Incompatible types in assignment (expression has type "Optional[str]", variable has type "str") [assignment]
other: str = x or y
^
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used: 0.910
- Mypy command-line flags: none
- Mypy configuration options from
mypy.ini(and other config files):
strict = true
pretty = true
show_column_numbers = true
show_error_codes = true
show_error_context = true
- Python version used: 3.7.9
- Operating system and version: MacOSX 10.15.7 Catalina
- 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
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
bancolombia/sentinel#23 ·
-
test md AbiertoCI
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
-
integration:quickjs org:external priority:backlog topic:code-interpreter topic:middleware type:feature
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100
langchain-ai/deepagents#6450 ·
-
bug client
Dificultad 2/5 1-3 horas Aptitud para principiantes 88/100
-
Dificultad 2/5 1-3 horas Aptitud para principiantes 74/100