Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(chore) fastapi: test typing #3782

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
),
]
)
Loading