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

context_getter typing is broken after v0.254.1 #3768

Open
YaraslauZhylko opened this issue Feb 5, 2025 · 1 comment
Open

context_getter typing is broken after v0.254.1 #3768

YaraslauZhylko opened this issue Feb 5, 2025 · 1 comment
Labels
bug Something isn't working

Comments

@YaraslauZhylko
Copy link

YaraslauZhylko commented Feb 5, 2025

Unexpected mypy error for context_getter after v0.254.1.

Describe the Bug

I have a working configuration (slimmed down):

import strawberry
from fastapi import Depends
from strawberry.fastapi import BaseContext, GraphQLRouter


class MyContext(BaseContext):
    def __init__(self) -> None:
        self.test = "test"


@strawberry.type
class Query:
    noop: str = strawberry.field(resolver=lambda: "noop")


class MyGraphQLRouter(GraphQLRouter):
    pass  # some unrelated customizations here


def get_context(context: MyContext = Depends(MyContext)) -> MyContext:
    return context


schema = strawberry.Schema(Query)


router: GraphQLRouter = MyGraphQLRouter(schema, context_getter=get_context)  # error here

Before v0.254.1, MyPy used to correctly check this config:

Success: no issues found in 1 source file

But after v0.254.1 (PR #3732), I get a weird error like this:

test.py:27: error: Argument "context_getter" to "MyGraphQLRouter" has incompatible type "Callable[[MyContext], MyContext]"; expected "Callable[..., None] | None"  [arg-type]
Found 1 error in 1 file (checked 1 source file)

Meanwhile, context_getter is defined like this:

context_getter: Optional[Callable[..., Optional[Context]]] = None,

Seems quite compatible with what I have.

I susspect, this this behaviour might be caused by this definition:

Context = TypeVar("Context", default=None)

System Information

  • Operating system: Linux
  • Python version: 3.12.8
  • Strawberry version: 0.254.1 or higher
  • FastAPI version: 0.115.6
  • MyPy version: 1.14.0 and 1.14.1
@YaraslauZhylko YaraslauZhylko added the bug Something isn't working label Feb 5, 2025
@YaraslauZhylko
Copy link
Author

cc: @patrick91

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant