strawberry-graphql / strawberry

A GraphQL library for Python that leverages type annotations 🍓
https://strawberry.rocks
MIT License
3.98k stars 531 forks source link

Fields with resolvers raise Type Errors when using Generic Subclasses #1899

Open invokermain opened 2 years ago

invokermain commented 2 years ago

Describe the Bug

Following on from: https://github.com/strawberry-graphql/strawberry/pull/1891

The case where a field's resolver has a subclassed generic return type still raises a Type Error. The isinstance check is too weak.

See this gist for examples: https://gist.github.com/invokermain/31c783c76dd26e1da862cbe5218d2500

System Information

Additional Context

There is a wider discussion about resolver return type hint behaviour here: https://github.com/strawberry-graphql/strawberry/issues/1896

Upvote & Fund

Fund with Polar

invokermain commented 2 years ago

Behavior table:

Let T = TypeVar("T") ... the extended table might like:

field type | resolver return type | current behaviour | expected behaviour | current type | expected type -- | -- | -- | -- | -- | -- int | T | no error | no error [1] | int | int int | GenericClass[T] | runtime error | no errors, no warnings | int | int int | Optional[T] | runtime error | no errors, no warnings | int | int

[1] recently fixed here: https://github.com/strawberry-graphql/strawberry/pull/1891