Inference for Literal arguments not handled
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Métricas de merge de PR
- Métricas de PR pendientes
Descripción
Bug Report
If you have a function with Literal arguments, and a variable that is set to one of the options, an error is reported.
To Reproduce
from typing import Literal
def myfun(a: Literal["a", "b", "c"]) -> str:
return a
s = "a"
myfun(s)
def myfun2(a: Literal[1, 2]) -> int:
return a
b = 2
myfun2(b)
c = 1 if s == "a" else 2
myfun2(c)
reports
literalinf.py:10: error: Argument 1 to "myfun" has incompatible type "str"; expected "Union[Literal['a'], Literal['b'], Literal['c']]"
literalinf.py:18: error: Argument 1 to "myfun2" has incompatible type "int"; expected "Union[Literal[1], Literal[2]]"
literalinf.py:21: error: Argument 1 to "myfun2" has incompatible type "int"; expected "Union[Literal[1], Literal[2]]"
Expected Behavior
No error. The values can be inferred to be of the Literal value
Actual Behavior
See above.
Your Environment
- Mypy version used: 0.910
- Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files): None - Python version used: 3.8.5
- Operating system and version: Windows 10 21H1
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.
Línea de trabajo
Empieza ejecutando el reproductor de Python proporcionado con mypy y rastrea cómo se infieren los valores asignados y las expresiones condicionales al comprobar argumentos Literal. Añade cobertura para las tres llamadas; el trabajo estará completo cuando ya no produzcan los errores de tipo incompatibles indicados.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- devtools
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 48/100