Type narrowing on object attribute with match statement doesn't apply
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 20.6k
- Forks
- 3.3k
- Merge medio
- 1 d 18 h
- PR fusionados (30 d)
- 54
Descripción
The bug
It seems that doing a "type checking" with the object UI in pattern matching seems to not work properly.
Attribute match is not considered.
Code
class Test:
def __init__(self, a: int | str):
self.a: int | str = a
test = Test(5)
match test:
case Test(a = int()):
print(test.a + 3)
case Test(a = str()):
print(test.a + "3")
Expected behavior
By doing a match with Test(a = int()), python will check test is of type Test AND test.a is of type int.
But mypy "only assert" that test if of type Test. It keep the type int | str for test.a.
Reproduce :
On mypy-playground :
https://mypy-play.net/?mypy=latest&python=3.11&gist=6f05124abd1ef68032d59651a7d5c6c3
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 con el reproductor de mypy-playground enlazado en el issue y sigue cómo la sentencia match gestiona los patrones Test(a=int()) y Test(a=str()). Compara el tipo inferido de test.a en cada rama. Se considera terminado cuando el atributo se restringe a int o str respectivamente, con el comportamiento cubierto por una prueba de regresión.
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
- 42/100