Treat alternative syntax for unions (and other "new-semantics-old-syntax" typing features) in explicit `TypeAlias`es inside `if TYPE_CHECKING` blocks as stringized annotations.
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Python
- Estrellas
- 1.8k
- Forks
- 302
- Merge medio
- 23 h
- PR fusionados (30 d)
- 8
Descripción
The Problem
At runtime, TYPE_CHECKING is always False, only type checkers assume it is True. Therefore, if TYPE_CHECKING blocks are only parsed, but never evaluated by the Python interpreter. Since A = int | str is a valid syntax for all still-supported Python versions at the moment (all versions ever since typing was introduced, actually), it would make sense if type checkers were allowed to accept usage of "new-semantics-old-syntax" features (like the alternative union syntax) in explicitly-annotated TypeAliases inside of if TYPE_CHECKING blocks as long as the Python version specified [project.requires-python] (or it's equivalent in tools) can successfully parse that syntax. This is essentially the same as treating explicitly-annotated TypeAliases as stringized annotations by-default.
Examples
# Run on 3.9
from typing import TYPE_CHECKING
if TYPE_CHECKING:
A = int | str # Alternative syntax for unions requires Python 3.10 or newer # pyright
# Invalid type alias: expression is not a valid type # mypy
# Unsupported left operand type for | ("type[int]") # mypy
# With proposal
from typing import TYPE_CHECKING
from typing_extensions import TypeAlias
if TYPE_CHECKING:
A: TypeAlias = int | str # Treated as a stringized annotation because `: TypeAlias`
# is explicit, therefore valid.
Guía de contribución
No hay ninguna guía de contribución indexada para este repositorio
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 los ejemplos enlazados de pyright-play y mypy-play, centrándote en Python 3.9, TYPE_CHECKING y los TypeAliases anotados explícitamente. No se han identificado archivos del repositorio ni pruebas; el trabajo estaría terminado cuando exista una especificación acordada sobre cómo deben interpretarse estos casos y el correspondiente acuerdo o trabajo de implementación en las herramientas relevantes de comprobación de tipos.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- python
- Área
- devtools
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 28/100