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

Aberta
#18,508 0 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

Avaliação

Dificuldade
4/5
Tempo estimado
3-5 dias
Facilidade para iniciantes
35/100
Tipo de issue
Funcionalidade
Clareza
Razoavelmente clara
Status de atividade
Estagnada
Stack de tecnologia
python
Domínio
compilers

Direção de pesquisa

Comece pela API get_type_analyze_hook e por AnalyzeTypeContext; em seguida, rastreie como ctx.type.args lida com os argumentos de typing.Annotated. O trabalho estará concluído quando um plugin puder obter expressões de valores que não sejam tipos, como "hello world" ou Depends(my_dependency), ao analisar argumentos genéricos.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

feature topic-plugins

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.

Linguagem predominante
Python
Estrelas
20.6k
Forks
3.3k
Merge médio
1d 18h
PRs com merge (30d)
54

Guia de contribuição

Abrir o guia de contribuição

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de python/mypy

Todas as issues de python/mypy

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.