Skip to content

Commit

Permalink
(chore) fastapi: test typing (#3782)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-pelykh authored Feb 13, 2025
1 parent 4c4e07b commit 03d8aca
Showing 1 changed file with 119 additions and 0 deletions.
119 changes: 119 additions & 0 deletions tests/typecheckers/test_fastapi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
from inline_snapshot import snapshot

from .utils.marks import requires_mypy, requires_pyright, skip_on_windows
from .utils.typecheck import Result, typecheck

pytestmark = [skip_on_windows, requires_pyright, requires_mypy]

CODE_ROUTER_WITH_CONTEXT = """
import strawberry
from strawberry.fastapi import GraphQLRouter, BaseContext
@strawberry.type
class Query:
@strawberry.field
def hello(self) -> str:
return "Hello World"
class Context(BaseContext):
pass
def get_context() -> Context:
return Context()
router = GraphQLRouter(
strawberry.Schema(
query=Query,
),
context_getter=get_context,
)
reveal_type(router)
"""


def test_router_with_context():
results = typecheck(CODE_ROUTER_WITH_CONTEXT)

assert results.pyright == snapshot(
[
Result(
type="information",
message='Type of "router" is "GraphQLRouter[Context, None]"',
line=29,
column=13,
),
]
)
assert results.mypy == snapshot(
[
Result(
type="note",
message='Revealed type is "strawberry.fastapi.router.GraphQLRouter[mypy_test.Context, None]"',
line=29,
column=13,
),
]
)


CODE_ROUTER_WITH_ASYNC_CONTEXT = """
import strawberry
from strawberry.fastapi import GraphQLRouter, BaseContext
@strawberry.type
class Query:
@strawberry.field
def hello(self) -> str:
return "Hello World"
class Context(BaseContext):
pass
async def get_context() -> Context:
return Context()
router = GraphQLRouter[Context](
strawberry.Schema(
query=Query,
),
context_getter=get_context,
)
reveal_type(router)
"""


def test_router_with_async_context():
results = typecheck(CODE_ROUTER_WITH_ASYNC_CONTEXT)

assert results.pyright == snapshot(
[
Result(
type="information",
message='Type of "router" is "GraphQLRouter[Context, None]"',
line=29,
column=13,
),
]
)
assert results.mypy == snapshot(
[
Result(
type="note",
message='Revealed type is "strawberry.fastapi.router.GraphQLRouter[mypy_test.Context, None]"',
line=29,
column=13,
),
]
)

0 comments on commit 03d8aca

Please sign in to comment.