python / python/mypy

Make it possible to obtain non-type arguments in type analyze plugin hooks

Abierto
#18,508 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

feature topic-plugins
Lenguaje dominante
Python
Estrellas
20.6k
Forks
3.3k
Métricas de merge de PR
Métricas de PR pendientes

Descripción

Feature

It should be possible to obtain value expressions used as generic arguments in an AnalyzeTypeContext
For example, given typing.Annotated[builtins.int, "hello world"], I want to obtain "hello world", which is not a type, but a value.

Pitch

This feature would enable type checking of dependencies in FastAPI. FastAPI dependency injection allows specification of dependencies like this:

def my_dependency() -> SomeType:
    return ...


@app.get("/")
def route_handler(arg: Annotated[SomeType, Depends(my_dependency)]):
    return arg.do_something()

In this situation, MyPy should (with the help of a FastAPI specific plugin) be able to verify that the return type of my_dependency matches the type, which is the first generic argument of Annotated.


Currently, a plugin could theoretically obtain dependencies using get_type_analyze_hook. However, when inspecting the type arguments (ctx.type.args), any non-type argument to Annotated is not accessible within the scope of this hook.


Furthermore, this could potentially enable additional inspections relating to Pydantic fields.

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza por la API get_type_analyze_hook y AnalyzeTypeContext; después, sigue cómo ctx.type.args gestiona los argumentos de typing.Annotated. El trabajo estará completo cuando un plugin pueda obtener expresiones de valores que no sean tipos, como "hello world" o Depends(my_dependency), al analizar argumentos genéricos.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
python
Área
compilers
Tipo de issue
Nueva funcionalidad
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.