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

Strawberry cannot resolve type by inheriting a generic type with union type applied to it. #3289

Open
HrMathematiker opened this issue Dec 13, 2023 · 1 comment · May be fixed by #3291
Open
Labels
bug Something isn't working

Comments

@HrMathematiker
Copy link

HrMathematiker commented Dec 13, 2023

Hello! I tried to create a type inheriting a generic type with a union type applied and caught a TypeError: Response fields cannot be resolved.

Describe the Bug

There's a code fragment the bug can be reproduced with:

from typing import Annotated, Generic, TypeVar, Union
import strawberry

T = TypeVar("T")

@strawberry.type
class User:
    name: str
    age: int


@strawberry.type
class ProUser:
    name: str
    age: float


@strawberry.type
class GenType(Generic[T]):
    data: T


GeneralUser = Annotated[Union[User, ProUser], strawberry.union("GeneralUser")]


@strawberry.type
class Response(GenType[GeneralUser]):
    ...


@strawberry.type
class Query:
    @strawberry.field
    def user(self) -> Response:
        return Response(data=User(age=1, name="John"))

schema = strawberry.Schema(query=Query)

This code raises the following exception: TypeError: Response fields cannot be resolved. Unexpected type 'typing.Annotated[typing.Union[__main__.User, __main__.ProUser], <strawberry.union.StrawberryUnion object at 0x14f8a90>]'

But if you replace GeneralUser = Annotated[Union[User, ProUser], strawberry.union("GeneralUser")] with GeneralUser = strawberry.union("GeneralUser", (User, ProUser)) the code works as expected and doesn't raise any exception.

It looks like union types cannot be applied to generic types if they're declared with Annotated, because this bug isn't reproducible in case the old-style approach with strawberry.union is used.

System Information

  • Strawberry version (if applicable): 0.216.1
  • Python version: 3.9

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.
Fund with Polar
@HrMathematiker HrMathematiker added the bug Something isn't working label Dec 13, 2023
@patrick91
Copy link
Member

oh, well spotted! we should fix this, I'll take a look at it soon 😊

thanks for the great reproduction!

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

Successfully merging a pull request may close this issue.

2 participants