Make it possible to obtain non-type arguments in type analyze plugin hooks
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 20.6k
- Forks
- 3.3k
- PR merge metrics
- PR metrics pending
Description
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.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the get_type_analyze_hook API and AnalyzeTypeContext, then trace how ctx.type.args handles typing.Annotated arguments. The work is complete when a plugin can obtain non-type value expressions such as "hello world" or Depends(my_dependency) while analyzing generic arguments.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- compilers
- Issue type
- Feature
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100