Inconsistent handling of unsolved TypeVars
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
PEP 484 doesn't provide much clarity about the situation where a TypeVar is provided with no value in a generic function call, so I'm not sure if there is a "correct" behavior. However, I'd expect that the behavior would at least be consistent for a type checker.
from typing import Optional, TypeVar
T1 = TypeVar("T1")
def func1(suffix: Optional[T1] = ...) -> T1:
...
T2 = TypeVar("T2", bound=str)
def func2(suffix: Optional[T2] = ...) -> T2:
...
T3 = TypeVar("T3", str, bytes)
def func3(suffix: Optional[T3] = ...) -> T3:
...
T4 = TypeVar("T4", bytes, str)
def func4(suffix: Optional[T4] = ...) -> T4:
...
reveal_type(func1()) # <nothing>
reveal_type(func2()) # <nothing>
reveal_type(func3()) # str
reveal_type(func4()) # bytes
Note that mypy's results depend on whether the TypeVar is bound or constrained. If it is constrained, mypy appears to (arbitrarily?) choose the first constrained type.
Note that this situation arises in some common situations like the following:
from tempfile import mkstemp
_, temp_path = mkstemp()
reveal_type(temp_path) # str?
Like I said, I'm not sure what the "correct" or intended behavior is in this case, but the current behavior strikes me as inconsistent.
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
No se nombran archivos ni pruebas. Empieza reproduciendo los cuatro ejemplos de funciones genéricas y el caso de mkstemp(), y después compara los tipos inferidos para TypeVars no restringidos, acotados y restringidos. La tarea se considerará completada cuando haya un comportamiento acordado y coherente, seguido de la implementación y la cobertura 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
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Necesita aclaración
- Aptitud para principiantes
- 25/100