fix: handle StrawberryUnion in is_implemented_by for generic TypeVar resolution#4302
fix: handle StrawberryUnion in is_implemented_by for generic TypeVar resolution#4302bellini666 wants to merge 2 commits intomainfrom
Conversation
…resolution When a generic type like Collection[A | B] is used in a union with another type, the TypeVar maps to a StrawberryUnion. The issubclass check skipped it since StrawberryUnion isn't a type, causing UnallowedReturnTypeForUnion. Now checks if the runtime type is a member of the union. Fixes #4288
Reviewer's GuideExtends File-Level Changes
Assessment against linked issues
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Hey - I've left some high level feedback:
- The runtime import of
StrawberryUnioninsideis_implemented_bymay introduce hidden circular dependencies; consider either moving this import to the module level or adding a brief comment explaining why it must stay local. - Instead of directly accessing
expected_concrete_type.typesand using theinoperator, consider delegating union membership checks to a dedicated helper or method onStrawberryUnion(if available) so the implementation detail of how members are stored remains encapsulated.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The runtime import of `StrawberryUnion` inside `is_implemented_by` may introduce hidden circular dependencies; consider either moving this import to the module level or adding a brief comment explaining why it must stay local.
- Instead of directly accessing `expected_concrete_type.types` and using the `in` operator, consider delegating union membership checks to a dedicated helper or method on `StrawberryUnion` (if available) so the implementation detail of how members are stored remains encapsulated.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
Thanks for adding the Here's a preview of the changelog: Fix Here's the tweet text: |
Greptile SummaryThis PR fixes a regression where returning a generic type parameterised with a union TypeVar (e.g. The fix adds a single, well-placed guard in Key changes:
Confidence Score: 4/5
Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A["is_implemented_by(root) called"] --> B{type_definition is self?}
B -- Yes --> C[return True]
B -- No --> D{type_definition is concrete_of?}
D -- No --> E[return False]
D -- Yes --> F[Iterate fields with TypeVar]
F --> G[Get value from root, unwrap StrawberryList]
G --> H[Get expected_concrete_type from type_var_map]
H --> I{isinstance expected_concrete_type, type AND issubclass?}
I -- Yes --> C
I -- No --> J{isinstance expected_concrete_type, StrawberryUnion?}
J -- Yes --> K{real_concrete_type in union.types?}
K -- Yes --> C
K -- No --> L{real_concrete_type is not expected_concrete_type?}
J -- No --> L
L -- True --> E
L -- False --> F
F -- All fields matched --> C
Last reviewed commit: b6a4e6d |
There was a problem hiding this comment.
Pull request overview
Fixes union type resolution for generics where a TypeVar resolves to a union (e.g. Collection[A | B]) and that generic is then used inside an outer union, avoiding UnallowedReturnTypeForUnion during schema execution.
Changes:
- Extend
StrawberryObjectDefinition.is_implemented_byto treatStrawberryUnionas a valid expected concrete type by checking runtime member types. - Add a regression test for
Collection[Animal | Plant] | Errorcovering both success and error branches.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
strawberry/types/base.py |
Updates generic type matching to accept StrawberryUnion as an expected concrete type during is_implemented_by checks. |
tests/schema/test_generics.py |
Adds regression test reproducing the reported error scenario with a union TypeVar inside an outer union. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Summary
Collection[A | B]is used in a union (Collection[A | B] | Error), resolving the return type raisedUnallowedReturnTypeForUnionis_implemented_bymethod now handlesStrawberryUnionas an expected concrete type by checking if the runtime type is a member of the unionFixes #4288
Summary by Sourcery
Handle generic fields whose type variables are unions when used inside outer unions, ensuring correct runtime resolution for union return types.
Bug Fixes:
Tests: