Skip to content

Commit

Permalink
(imp) support type[strawberry.UNSET]
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-pelykh committed Feb 2, 2025
1 parent 7ba5928 commit f8ab2de
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
3 changes: 3 additions & 0 deletions RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Release type: minor

(imp) Support for `type[strawberry.UNSET]` in addition to `strawberry.types.unset.UnsetType`
2 changes: 1 addition & 1 deletion docs/types/input-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ from typing import Optional
class Point2D:
x: float
y: float
label: Optional[str] = strawberry.UNSET
label: Optional[str] | type[strawberry.UNSET] = strawberry.UNSET
```

```graphql
Expand Down
4 changes: 3 additions & 1 deletion strawberry/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ def create_optional(self, evaled_type: Any) -> StrawberryOptional:
types = get_args(evaled_type)
non_optional_types = tuple(
filter(
lambda x: x is not type(None) and x is not type(UNSET),
lambda x: x is not type(None)
and x is not type(UNSET)
and x != type[UNSET],
types,
)
)
Expand Down
11 changes: 11 additions & 0 deletions tests/types/resolving/test_optionals.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,17 @@ def test_optional_with_unset():
assert resolved == Optional[str]


def test_optional_with_type_of_unset():
annotation = StrawberryAnnotation(Union[type[strawberry.UNSET], Optional[str]])
resolved = annotation.resolve()

assert isinstance(resolved, StrawberryOptional)
assert resolved.of_type is str

assert resolved == StrawberryOptional(of_type=str)
assert resolved == Optional[str]


def test_optional_with_unset_as_union():
annotation = StrawberryAnnotation(Union[UnsetType, None, str])
resolved = annotation.resolve()
Expand Down

0 comments on commit f8ab2de

Please sign in to comment.