strawberry
strawberry copied to clipboard
mypy: Need type annotation for "graphql_app" [var-annotated]
Since strawberry-graphql==0.169.0
mypy gives the following error on graphql_app = GraphQLRouter(schema)
:
app.py:16: error: Need type annotation for "graphql_app" [var-annotated]
Found 1 error in 1 file (checked 1 source file)
Describe the Bug
pip install in your virtualenv:
pip install mypy fastapi strawberry-graphql==0.169.0
Take the example web application:
import strawberry
from fastapi import FastAPI
from strawberry.fastapi import GraphQLRouter
@strawberry.type
class Query:
@strawberry.field
def hello(self) -> str:
return "Hello World"
schema = strawberry.Schema(Query)
graphql_app = GraphQLRouter(schema)
app = FastAPI()
app.include_router(graphql_app, prefix="/graphql")
Then run mypy:
$ mypy app.py
app.py:16: error: Need type annotation for "graphql_app" [var-annotated]
Found 1 error in 1 file (checked 1 source file)
System Information
- Operating system: Linux
- Strawberry version (if applicable): 0.171.1
Additional Context
The problem does not happen in version 0.168.2:
$ pip install strawberry-graphql==0.168.2
$ mypy app.py
Success: no issues found in 1 source file
A workaround is to just annotate the graphql_app variable manually:
graphql_app: GraphQLRouter = GraphQLRouter(schema)
Of course it would be nicer if it can get fixed.
Upvote & Fund
- We're using Polar.sh so you can upvote and help fund this issue.
- We receive the funding once the issue is completed & confirmed by you.
- Thank you in advance for helping prioritize & fund our backlog.
hi @yunzheng!
I think this is because I made GraphQLRouter (and all the other views) generic, can you try with:
graphql_app = GraphQLRouter[object, object](schema)
?
(first type is the context type, second is the root value)
This works!, however it's unclear to me that I should define it like that. Also the examples are not updated to use this.
hi @yunzheng!
I think this is because I made GraphQLRouter (and all the other views) generic, can you try with:
graphql_app = GraphQLRouter[object, object](schema)
?(first type is the context type, second is the root value)
Is this still the best way to do this?