Type detection broken on attribute override and not-equal-assertion
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
I commonly find myself writing simplified list init parameters. So, if the user provides only a type X, I automatically convert that to list type [X]. In doing so, I found two issue that prevent a proper type detection. See the example code below.
To Reproduce
#!/usr/bin/env python3
from typing import Union, List, Optional
def test(a: Union[List[str], str, None] = None) -> Optional[List[str]]:
if type(a) == str:
a = [a]
assert(type(a) != str)
return a
Expected Behavior
- First, the
type(a) == strcheck and consequent list assignment should remove the str type from the Union choices. - Second, the
assert(!=)check should also remove the str type from the Union options.
Actual Behavior
Both checks do not work and the type is still guessed incorrectly:

Your Environment
- Mypy version used: 0.910
- Mypy command-line flags: pip install
- Mypy configuration options from
mypy.ini(and other config files): - - Python version used: 3.8
- Operating system and version: macOS 10.15
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
Comienza ejecutando el reproductor de Python proporcionado con mypy e inspecciona el comportamiento del estrechamiento de tipos para la asignación type(a) == str y la comprobación assert(type(a) != str). Se considera terminado cuando ambas ramas eliminan str de la Union inferida y la función se acepta con el tipo de retorno esperado Optional[List[str]].
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
- 35/100