From 07429d6d3ddcdf79b13ba5ced25919285c2da0c6 Mon Sep 17 00:00:00 2001 From: Alexey Pelykh Date: Sun, 2 Feb 2025 21:13:13 +0100 Subject: [PATCH 1/2] (imp) support type[strawberry.UNSET] --- RELEASE.md | 3 +++ strawberry/annotation.py | 4 +++- tests/types/resolving/test_optionals.py | 11 +++++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 RELEASE.md diff --git a/RELEASE.md b/RELEASE.md new file mode 100644 index 0000000000..173de4d0a8 --- /dev/null +++ b/RELEASE.md @@ -0,0 +1,3 @@ +Release type: minor + +Support for `type[strawberry.UNSET]` in addition to `strawberry.types.unset.UnsetType` for annotations. diff --git a/strawberry/annotation.py b/strawberry/annotation.py index 29ffa6ee85..a84dae52e1 100644 --- a/strawberry/annotation.py +++ b/strawberry/annotation.py @@ -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, ) ) diff --git a/tests/types/resolving/test_optionals.py b/tests/types/resolving/test_optionals.py index f01f8b74d4..1c2784a03a 100644 --- a/tests/types/resolving/test_optionals.py +++ b/tests/types/resolving/test_optionals.py @@ -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() From 8039fa4cc171f3fac3fe8c0acf96ea6dee04370f Mon Sep 17 00:00:00 2001 From: Patrick Arminio Date: Thu, 27 Feb 2025 12:21:03 +0000 Subject: [PATCH 2/2] Update RELEASE.md --- RELEASE.md | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/RELEASE.md b/RELEASE.md index 173de4d0a8..005e9e3a03 100644 --- a/RELEASE.md +++ b/RELEASE.md @@ -1,3 +1,11 @@ Release type: minor -Support for `type[strawberry.UNSET]` in addition to `strawberry.types.unset.UnsetType` for annotations. +This release adds support for `type[strawberry.UNSET]` in addition to `strawberry.types.unset.UnsetType` for annotations. + + +```python +@strawberry.type +class User: + name: str | None = UNSET + age: int | None | type[strawberry.UNSET] = UNSET +```