python / python/mypy

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

Offen
#18,508 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

feature topic-plugins
Vorherrschende Sprache
Python
Sterne
20.6k
Forks
3.3k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne bei der get_type_analyze_hook API und AnalyzeTypeContext und verfolge dann, wie ctx.type.args Argumente von typing.Annotated verarbeitet. Die Arbeit ist abgeschlossen, wenn ein Plugin beim Analysieren generischer Argumente Wertausdrücke erhalten kann, die keine Typen sind, etwa "hello world" oder Depends(my_dependency).

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.