Inconsistent narrowing for `builtins.type` and `typing.Type`
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 following difference is due to Mypy handling typing.Type via TypeType, for which it does not try to create an intersection, and builtins.type as an Instance, for which it does:
# flags: --python-version 3.9 --warn-unreachable
from typing import Type
t1: Type
t2: type
class C: ...
if isinstance(t1, C):
reveal_type(t1) # E: Statement is unreachable
if isinstance(t2, C):
reveal_type(t2) # N: Revealed type is "__main__.<subclass of "type" and "C">"
I encountered this problem when working on #16330, which affects type checking a sphinx function.
Interestingly, things are consistent when annotating explicitly with Any:
# flags: --python-version 3.9 --warn-unreachable
from typing import Any, Type
t1: Type[Any]
t2: type[Any]
class C: ...
if isinstance(t1, C):
reveal_type(t1) # E: Statement is unreachable
if isinstance(t2, C):
reveal_type(t2) # E: Statement is unreachable
So, Mypy maybe misunderstands t2: type?
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 reproducer del issue usando la configuración indicada de Python 3.9 y --warn-unreachable. Rastrea cómo Mypy estrecha typing.Type mediante TypeType frente a builtins.type mediante Instance, y después compara el comportamiento con los ejemplos explícitos de Any. Se considera terminado cuando se haya resuelto el estrechamiento inconsistente y los casos mostrados produzcan diagnósticos y tipos revelados coherentes.
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
- 30/100