strawberry-graphql 0.262.6__py3-none-any.whl → 0.262.7.dev1743345593__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
strawberry/__init__.py CHANGED
@@ -23,7 +23,7 @@ from .types.object_type import asdict, input, interface, type # noqa: A004
23
23
  from .types.private import Private
24
24
  from .types.scalar import scalar
25
25
  from .types.union import union
26
- from .types.unset import UNSET
26
+ from .types.unset import UNSET, Maybe, exists
27
27
 
28
28
  __all__ = [
29
29
  "ID",
@@ -31,6 +31,7 @@ __all__ = [
31
31
  "BasePermission",
32
32
  "Info",
33
33
  "LazyType",
34
+ "Maybe",
34
35
  "Parent",
35
36
  "Private",
36
37
  "Schema",
@@ -42,6 +43,7 @@ __all__ = [
42
43
  "directive_field",
43
44
  "enum",
44
45
  "enum_value",
46
+ "exists",
45
47
  "experimental",
46
48
  "federation",
47
49
  "field",
strawberry/annotation.py CHANGED
@@ -30,7 +30,7 @@ from strawberry.types.enum import enum as strawberry_enum
30
30
  from strawberry.types.lazy_type import LazyType
31
31
  from strawberry.types.private import is_private
32
32
  from strawberry.types.scalar import ScalarDefinition
33
- from strawberry.types.unset import UNSET
33
+ from strawberry.types.unset import UNSET, _annot_is_maybe
34
34
  from strawberry.utils.typing import eval_type, is_generic, is_type_var
35
35
 
36
36
  if TYPE_CHECKING:
@@ -143,6 +143,11 @@ class StrawberryAnnotation:
143
143
  return evaled_type
144
144
  if self._is_list(evaled_type):
145
145
  return self.create_list(evaled_type)
146
+ if type_of := self._get_maybe_type(evaled_type):
147
+ return StrawberryAnnotation(
148
+ annotation=Union[type_of, None],
149
+ namespace=self.namespace,
150
+ ).resolve()
146
151
 
147
152
  if self._is_graphql_generic(evaled_type):
148
153
  if any(is_type_var(type_) for type_ in get_args(evaled_type)):
@@ -310,6 +315,10 @@ class StrawberryAnnotation:
310
315
  or is_list
311
316
  )
312
317
 
318
+ @classmethod
319
+ def _get_maybe_type(cls, annotation: Any) -> type | None:
320
+ return get_args(annotation)[0] if _annot_is_maybe(annotation) else None
321
+
313
322
  @classmethod
314
323
  def _is_strawberry_type(cls, evaled_type: Any) -> bool:
315
324
  # Prevent import cycles
@@ -20,6 +20,7 @@ from strawberry.exceptions import (
20
20
  ObjectIsNotClassError,
21
21
  )
22
22
  from strawberry.types.base import get_object_definition
23
+ from strawberry.types.unset import UNSET, _annot_is_maybe
23
24
  from strawberry.utils.deprecations import DEPRECATION_MESSAGES, DeprecatedDescriptor
24
25
  from strawberry.utils.str_converters import to_camel_case
25
26
 
@@ -122,6 +123,15 @@ def _wrap_dataclass(cls: builtins.type[T]) -> builtins.type[T]:
122
123
  return dclass
123
124
 
124
125
 
126
+ def _inject_default_for_maybe_annotations(
127
+ cls: builtins.type[T], annotations: dict[str, Any]
128
+ ) -> None:
129
+ """Inject `= UNSET` for fields with `Maybe` annotations and no default value."""
130
+ for name, annot in annotations.copy().items():
131
+ if _annot_is_maybe(annot) and not getattr(cls, name, None):
132
+ setattr(cls, name, UNSET)
133
+
134
+
125
135
  def _process_type(
126
136
  cls: T,
127
137
  *,
@@ -286,7 +296,8 @@ def type(
286
296
 
287
297
  if field and isinstance(field, StrawberryField) and field.type_annotation:
288
298
  original_type_annotations[field_name] = field.type_annotation.annotation
289
-
299
+ if is_input:
300
+ _inject_default_for_maybe_annotations(cls, annotations)
290
301
  wrapped = _wrap_dataclass(cls)
291
302
 
292
303
  return _process_type( # type: ignore
strawberry/types/unset.py CHANGED
@@ -1,5 +1,7 @@
1
+ import typing
1
2
  import warnings
2
- from typing import Any, Optional
3
+ from typing import TYPE_CHECKING, Any, Generic, Optional, TypeVar, Union
4
+ from typing_extensions import TypeAlias, TypeGuard
3
5
 
4
6
  DEPRECATED_NAMES: dict[str, str] = {
5
7
  "is_unset": "`is_unset` is deprecated use `value is UNSET` instead",
@@ -61,6 +63,26 @@ def __getattr__(name: str) -> Any:
61
63
  raise AttributeError(f"module {__name__} has no attribute {name}")
62
64
 
63
65
 
66
+ NOTHING = UnsetType()
67
+ """A special value that can be used to represent an unset value in a field or argument."""
68
+
69
+ T = TypeVar("T")
70
+
71
+ if TYPE_CHECKING:
72
+ Maybe: TypeAlias = Union[T, UnsetType, None]
73
+ else:
74
+ # we do this trick so we can inspect that at runtime
75
+ class Maybe(Generic[T]): ...
76
+
77
+
78
+ def _annot_is_maybe(annotation: Any) -> bool:
79
+ return (orig := typing.get_origin(annotation)) and orig is Maybe
80
+
81
+
82
+ def exists(value: Union[T, UnsetType, None]) -> TypeGuard[Union[T, None]]:
83
+ return value is not UNSET or not isinstance(value, UnsetType)
84
+
85
+
64
86
  __all__ = [
65
87
  "UNSET",
66
88
  ]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: strawberry-graphql
3
- Version: 0.262.6
3
+ Version: 0.262.7.dev1743345593
4
4
  Summary: A library for creating GraphQL APIs
5
5
  License: MIT
6
6
  Keywords: graphql,api,rest,starlette,async
@@ -1,10 +1,10 @@
1
- strawberry/__init__.py,sha256=VcqNwegcJS_YhCOrZyveD_wBcd-hh8KKrXcIlTc7knE,1429
1
+ strawberry/__init__.py,sha256=oWrH2u03CPC4o3_Q3MVtOv5bMr4n1l5AIoQi0xk3D2Q,1471
2
2
  strawberry/__main__.py,sha256=3U77Eu21mJ-LY27RG-JEnpbh6Z63wGOom4i-EoLtUcY,59
3
3
  strawberry/aiohttp/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
4
  strawberry/aiohttp/test/__init__.py,sha256=4xxdUZtIISSOwjrcnmox7AvT4WWjowCm5bUuPdQneMg,71
5
5
  strawberry/aiohttp/test/client.py,sha256=8FKZTnvawxYpgEICOri-34O3wHRHLhRpjH_Ktp2EupQ,1801
6
6
  strawberry/aiohttp/views.py,sha256=JXYd_qO_LD4QbRJ7iAnZbEsezwvPOmTujTGNAb75bRU,7885
7
- strawberry/annotation.py,sha256=FYoUNz857yt3Hewm8bYFmMLpG7dTdKpc4j9nBGsv40k,13038
7
+ strawberry/annotation.py,sha256=Bt1pSylGy8eYJYpXhJb_34MmQQLv5cqk08xMgGaYVuM,13428
8
8
  strawberry/asgi/__init__.py,sha256=55tsJmqIPlQgeScDUQuADalLlN5ymdHOeFHWzyII7aY,8167
9
9
  strawberry/asgi/test/__init__.py,sha256=4xxdUZtIISSOwjrcnmox7AvT4WWjowCm5bUuPdQneMg,71
10
10
  strawberry/asgi/test/client.py,sha256=kp2O5znHWuAB5VVYO8p4XPSTEDDXBSjNz5WHqW0r6GM,1473
@@ -209,12 +209,12 @@ strawberry/types/info.py,sha256=bPP7XTQQScmskJcmVv36iqLAWpdGmF2nhYjI1pJ-csI,4709
209
209
  strawberry/types/lazy_type.py,sha256=ILLDSbFpsg6u1FBztrI1OumRFCJ2182-7FR9oZeJgks,5077
210
210
  strawberry/types/mutation.py,sha256=cg-_O2WWnZ-GSwOIv0toSdxlGeY2lhBBxZ24AifJuSM,11978
211
211
  strawberry/types/nodes.py,sha256=RwZB43OT9BS3Cqjgq4AazqOfyq_y0GD2ysC86EDBv5U,5134
212
- strawberry/types/object_type.py,sha256=CW-detiYjGmk4kHJP-vUK9sBkBuDic4nswDub4zUyvc,15075
212
+ strawberry/types/object_type.py,sha256=tIKz_jYLzB8kmkgs2R71O4un_DRERrItkLS4oRrjxyo,15574
213
213
  strawberry/types/private.py,sha256=DhJs50XVGtOXlxWZFkRpMxQ5_6oki0-x_WQsV1bGUxk,518
214
214
  strawberry/types/scalar.py,sha256=CM24Ixg4DKxxI1C6hTNGYVitohJibs8BpGtntRZvMXw,6284
215
215
  strawberry/types/type_resolver.py,sha256=4jaRh5MntX7wcBY8ksK-ZE3849K46Mwsswc4ebFCQWI,6545
216
216
  strawberry/types/union.py,sha256=3Wk0O9ocMLgiv4fhC9OcS7em3sZP9UaFypmRbz3Ekgg,9960
217
- strawberry/types/unset.py,sha256=7DVK-WWxVLo41agvavTvIbphE42BmY8UpGolXfasIvw,1682
217
+ strawberry/types/unset.py,sha256=YiGWz2gFdEtRQ7E369p3oebmmbCDuOyx_Vg-OQUFBe4,2344
218
218
  strawberry/utils/__init__.py,sha256=wuuNvKjcMfE0l4lqrlC-cc0_SR4hV19gNBJ3Mcn7l3A,141
219
219
  strawberry/utils/aio.py,sha256=Y78UoqWJE_mKJBVSLW1EUM43zV5MWt7yXqlvoktKkoA,1696
220
220
  strawberry/utils/await_maybe.py,sha256=YdjfuzjDVjph0VH0WkwvU4ezsjl_fELnGrLC1_bvb_U,449
@@ -228,8 +228,8 @@ strawberry/utils/logging.py,sha256=U1cseHGquN09YFhFmRkiphfASKCyK0HUZREImPgVb0c,7
228
228
  strawberry/utils/operation.py,sha256=SSXxN-vMqdHO6W2OZtip-1z7y4_A-eTVFdhDvhKeLCk,1193
229
229
  strawberry/utils/str_converters.py,sha256=-eH1Cl16IO_wrBlsGM-km4IY0IKsjhjnSNGRGOwQjVM,897
230
230
  strawberry/utils/typing.py,sha256=Ux0Hl46lhuXvOKK-C5hj6nlz3zDn8P4CUGH2nUVD2vU,13373
231
- strawberry_graphql-0.262.6.dist-info/LICENSE,sha256=m-XnIVUKqlG_AWnfi9NReh9JfKhYOB-gJfKE45WM1W8,1072
232
- strawberry_graphql-0.262.6.dist-info/METADATA,sha256=7nqosxgnwdquqIsxQb_9MR_f6c_Tsbt21wdMmZTzQyc,7679
233
- strawberry_graphql-0.262.6.dist-info/WHEEL,sha256=XbeZDeTWKc1w7CSIyre5aMDU_-PohRwTQceYnisIYYY,88
234
- strawberry_graphql-0.262.6.dist-info/entry_points.txt,sha256=Nk7-aT3_uEwCgyqtHESV9H6Mc31cK-VAvhnQNTzTb4k,49
235
- strawberry_graphql-0.262.6.dist-info/RECORD,,
231
+ strawberry_graphql-0.262.7.dev1743345593.dist-info/LICENSE,sha256=m-XnIVUKqlG_AWnfi9NReh9JfKhYOB-gJfKE45WM1W8,1072
232
+ strawberry_graphql-0.262.7.dev1743345593.dist-info/METADATA,sha256=j6KQ3Yc5_EUY2TWqhygqiu6hCMza2GYKT2p5AaQx77g,7693
233
+ strawberry_graphql-0.262.7.dev1743345593.dist-info/WHEEL,sha256=fGIA9gx4Qxk2KDKeNJCbOEwSrmLtjWCwzBz351GyrPQ,88
234
+ strawberry_graphql-0.262.7.dev1743345593.dist-info/entry_points.txt,sha256=Nk7-aT3_uEwCgyqtHESV9H6Mc31cK-VAvhnQNTzTb4k,49
235
+ strawberry_graphql-0.262.7.dev1743345593.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: poetry-core 2.1.1
2
+ Generator: poetry-core 2.1.2
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any