Mypy reports invalid type when a value is a parameterized generic
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
When I attempt to use a parameterized generic as a value to build a signature at runtime, mypy complains about the parameter as being invalid, when the parameter comes from a variable.
To Reproduce
from inspect import Parameter
def signature_parameter(name: str, item_type: type) -> Parameter:
return Parameter(name=name, kind=Parameter.KEYWORD_ONLY, annotation=list[item_type])
Expected Behavior
I would expect no error in this case (when the parameterized type is used as a value).
I know a variable cannot be used as a type parameter in a typing context (i.e. when used as a type annotation).
I understand that the following code would be wrong because variables cannot be used in type specifications:
var: type = int
lst: list[var] = [1, 2, 3] # this is wrong
But in my case above, list[item_type] is not used as a type specification, but as a value, that is passed to the runtime to build a signature. I do not think mypy should flag such use of values. It should simply consider the value to be of type types.GenericAlias.
Actual Behavior
typing-8.py:6: error: Variable "item_type" is not valid as a type [valid-type]
typing-8.py:6: note: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases
Found 1 error in 1 file (checked 1 source file)
Your Environment
- Mypy version used:
mypy 1.12.0+dev.6a0657e5959ba1777c4d427f8f355d499035d145 (compiled: no) - Mypy command-line flags: None
- Mypy configuration options from
mypy.ini(and other config files):
[tool.mypy]
enable_incomplete_feature = "NewGenericSyntax"
disable_bytearray_promotion = true
disable_memoryview_promotion = true
check_untyped_defs = true
warn_unreachable = true
warn_redundant_casts = true
warn_unused_ignores = true
- Python version used: 3.12
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 typing-8.py y ejecuta mypy bajo Python 3.12 para confirmar el diagnóstico [valid-type] para list[item_type]. Sigue la ruta de comprobación de tipos responsable de ese diagnóstico y añade una prueba de regresión que muestre que este valor de anotación en tiempo de ejecución se acepta; el trabajo estará terminado cuando la prueba pase sin suprimir el error.
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
- 45/100