Make it possible to obtain non-type arguments in type analyze plugin hooks
Dieses Issue hat noch niemand übernommen.
- 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
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Ö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