pboot-validator 1.0.0__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.
Files changed (112) hide show
  1. pboot_validator/__init__.py +461 -0
  2. pboot_validator/_internal/__init__.py +0 -0
  3. pboot_validator/_internal/_config.py +408 -0
  4. pboot_validator/_internal/_contextlib.py +84 -0
  5. pboot_validator/_internal/_core_metadata.py +97 -0
  6. pboot_validator/_internal/_core_utils.py +156 -0
  7. pboot_validator/_internal/_dataclasses.py +327 -0
  8. pboot_validator/_internal/_decorators.py +906 -0
  9. pboot_validator/_internal/_decorators_v1.py +174 -0
  10. pboot_validator/_internal/_discriminated_union.py +510 -0
  11. pboot_validator/_internal/_docs_extraction.py +128 -0
  12. pboot_validator/_internal/_fields.py +910 -0
  13. pboot_validator/_internal/_forward_ref.py +23 -0
  14. pboot_validator/_internal/_generate_schema.py +3053 -0
  15. pboot_validator/_internal/_generics.py +572 -0
  16. pboot_validator/_internal/_git.py +27 -0
  17. pboot_validator/_internal/_import_utils.py +20 -0
  18. pboot_validator/_internal/_known_annotated_metadata.py +412 -0
  19. pboot_validator/_internal/_mock_val_ser.py +241 -0
  20. pboot_validator/_internal/_model_construction.py +833 -0
  21. pboot_validator/_internal/_namespace_utils.py +300 -0
  22. pboot_validator/_internal/_repr.py +129 -0
  23. pboot_validator/_internal/_schema_gather.py +49 -0
  24. pboot_validator/_internal/_schema_generation_shared.py +126 -0
  25. pboot_validator/_internal/_serializers.py +59 -0
  26. pboot_validator/_internal/_signature.py +193 -0
  27. pboot_validator/_internal/_type_refs.py +128 -0
  28. pboot_validator/_internal/_typing_extra.py +506 -0
  29. pboot_validator/_internal/_utils.py +433 -0
  30. pboot_validator/_internal/_validate_call.py +141 -0
  31. pboot_validator/_internal/_validators.py +541 -0
  32. pboot_validator/_migration.py +317 -0
  33. pboot_validator/alias_generators.py +62 -0
  34. pboot_validator/aliases.py +136 -0
  35. pboot_validator/annotated_handlers.py +123 -0
  36. pboot_validator/class_validators.py +5 -0
  37. pboot_validator/color.py +605 -0
  38. pboot_validator/config.py +1295 -0
  39. pboot_validator/dataclasses.py +385 -0
  40. pboot_validator/datetime_parse.py +5 -0
  41. pboot_validator/decorator.py +5 -0
  42. pboot_validator/deprecated/__init__.py +0 -0
  43. pboot_validator/deprecated/class_validators.py +257 -0
  44. pboot_validator/deprecated/config.py +72 -0
  45. pboot_validator/deprecated/copy_internals.py +224 -0
  46. pboot_validator/deprecated/decorator.py +286 -0
  47. pboot_validator/deprecated/json.py +142 -0
  48. pboot_validator/deprecated/parse.py +81 -0
  49. pboot_validator/deprecated/tools.py +104 -0
  50. pboot_validator/env_settings.py +5 -0
  51. pboot_validator/error_wrappers.py +5 -0
  52. pboot_validator/errors.py +185 -0
  53. pboot_validator/experimental/__init__.py +1 -0
  54. pboot_validator/experimental/arguments_schema.py +44 -0
  55. pboot_validator/experimental/missing_sentinel.py +29 -0
  56. pboot_validator/experimental/pipeline.py +684 -0
  57. pboot_validator/fields.py +2065 -0
  58. pboot_validator/functional_serializers.py +461 -0
  59. pboot_validator/functional_validators.py +920 -0
  60. pboot_validator/generics.py +5 -0
  61. pboot_validator/json.py +5 -0
  62. pboot_validator/json_schema.py +3283 -0
  63. pboot_validator/main.py +1947 -0
  64. pboot_validator/mypy.py +1469 -0
  65. pboot_validator/networks.py +1417 -0
  66. pboot_validator/parse.py +5 -0
  67. pboot_validator/plugin/__init__.py +190 -0
  68. pboot_validator/plugin/_loader.py +56 -0
  69. pboot_validator/plugin/_schema_validator.py +152 -0
  70. pboot_validator/py.typed +0 -0
  71. pboot_validator/root_model.py +189 -0
  72. pboot_validator/schema.py +5 -0
  73. pboot_validator/tools.py +5 -0
  74. pboot_validator/type_adapter.py +780 -0
  75. pboot_validator/types.py +3405 -0
  76. pboot_validator/typing.py +5 -0
  77. pboot_validator/utils.py +5 -0
  78. pboot_validator/v1/__init__.py +131 -0
  79. pboot_validator/v1/_hypothesis_plugin.py +391 -0
  80. pboot_validator/v1/annotated_types.py +72 -0
  81. pboot_validator/v1/class_validators.py +361 -0
  82. pboot_validator/v1/color.py +494 -0
  83. pboot_validator/v1/config.py +191 -0
  84. pboot_validator/v1/dataclasses.py +503 -0
  85. pboot_validator/v1/datetime_parse.py +248 -0
  86. pboot_validator/v1/decorator.py +264 -0
  87. pboot_validator/v1/env_settings.py +350 -0
  88. pboot_validator/v1/error_wrappers.py +161 -0
  89. pboot_validator/v1/errors.py +646 -0
  90. pboot_validator/v1/fields.py +1253 -0
  91. pboot_validator/v1/generics.py +400 -0
  92. pboot_validator/v1/json.py +112 -0
  93. pboot_validator/v1/main.py +1130 -0
  94. pboot_validator/v1/mypy.py +950 -0
  95. pboot_validator/v1/networks.py +747 -0
  96. pboot_validator/v1/parse.py +66 -0
  97. pboot_validator/v1/py.typed +0 -0
  98. pboot_validator/v1/schema.py +1163 -0
  99. pboot_validator/v1/tools.py +92 -0
  100. pboot_validator/v1/types.py +1213 -0
  101. pboot_validator/v1/typing.py +627 -0
  102. pboot_validator/v1/utils.py +807 -0
  103. pboot_validator/v1/validators.py +768 -0
  104. pboot_validator/v1/version.py +38 -0
  105. pboot_validator/validate_call_decorator.py +121 -0
  106. pboot_validator/validators.py +5 -0
  107. pboot_validator/version.py +113 -0
  108. pboot_validator/warnings.py +130 -0
  109. pboot_validator-1.0.0.dist-info/METADATA +93 -0
  110. pboot_validator-1.0.0.dist-info/RECORD +112 -0
  111. pboot_validator-1.0.0.dist-info/WHEEL +4 -0
  112. pboot_validator-1.0.0.dist-info/licenses/LICENSE +21 -0
@@ -0,0 +1,461 @@
1
+ """This module contains related classes and functions for serialization."""
2
+
3
+ from __future__ import annotations
4
+
5
+ import dataclasses
6
+ from collections.abc import Callable
7
+ from functools import partial, partialmethod
8
+ from typing import TYPE_CHECKING, Annotated, Any, Literal, TypeAlias, TypeVar, overload
9
+
10
+ from pydantic_core import PydanticUndefined, core_schema
11
+ from pydantic_core.core_schema import SerializationInfo, SerializerFunctionWrapHandler, WhenUsed
12
+ from typing_extensions import TypeForm
13
+
14
+ from . import PydanticUndefinedAnnotation
15
+ from ._internal import _decorators
16
+ from .annotated_handlers import GetCoreSchemaHandler
17
+ from .errors import PydanticUserError
18
+
19
+
20
+ @dataclasses.dataclass(slots=True, frozen=True)
21
+ class PlainSerializer:
22
+ """Plain serializers use a function to modify the output of serialization.
23
+
24
+ This is particularly helpful when you want to customize the serialization for annotated types.
25
+ Consider an input of `list`, which will be serialized into a space-delimited string.
26
+
27
+ ```python
28
+ from typing import Annotated
29
+
30
+ from pboot_validator import BaseModel, PlainSerializer
31
+
32
+ CustomStr = Annotated[
33
+ list, PlainSerializer(lambda x: ' '.join(x), return_type=str)
34
+ ]
35
+
36
+ class StudentModel(BaseModel):
37
+ courses: CustomStr
38
+
39
+ student = StudentModel(courses=['Math', 'Chemistry', 'English'])
40
+ print(student.model_dump())
41
+ #> {'courses': 'Math Chemistry English'}
42
+ ```
43
+
44
+ Attributes:
45
+ func: The serializer function.
46
+ return_type: The return type for the function. If omitted it will be inferred from the type annotation.
47
+ when_used: Determines when this serializer should be used. Accepts a string with values `'always'`,
48
+ `'unless-none'`, `'json'`, and `'json-unless-none'`. Defaults to 'always'.
49
+ """
50
+
51
+ func: core_schema.SerializerFunction
52
+ return_type: TypeForm[Any] = PydanticUndefined # pyright: ignore[reportAssignmentType]
53
+ when_used: WhenUsed = 'always'
54
+
55
+ def __get_pydantic_core_schema__(self, source_type: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
56
+ """Gets the Pydantic core schema.
57
+
58
+ Args:
59
+ source_type: The source type.
60
+ handler: The `GetCoreSchemaHandler` instance.
61
+
62
+ Returns:
63
+ The Pydantic core schema.
64
+ """
65
+ schema = handler(source_type)
66
+ if self.return_type is not PydanticUndefined:
67
+ return_type = self.return_type
68
+ else:
69
+ try:
70
+ # Do not pass in globals as the function could be defined in a different module.
71
+ # Instead, let `get_callable_return_type` infer the globals to use, but still pass
72
+ # in locals that may contain a parent/rebuild namespace:
73
+ return_type = _decorators.get_callable_return_type(
74
+ self.func,
75
+ localns=handler._get_types_namespace().locals,
76
+ )
77
+ except NameError as e:
78
+ raise PydanticUndefinedAnnotation.from_name_error(e) from e
79
+
80
+ return_schema = None if return_type is PydanticUndefined else handler.generate_schema(return_type)
81
+ schema['serialization'] = core_schema.plain_serializer_function_ser_schema(
82
+ function=self.func,
83
+ info_arg=_decorators.inspect_annotated_serializer(self.func, 'plain'),
84
+ return_schema=return_schema,
85
+ when_used=self.when_used,
86
+ )
87
+ return schema
88
+
89
+
90
+ @dataclasses.dataclass(slots=True, frozen=True)
91
+ class WrapSerializer:
92
+ """Wrap serializers receive the raw inputs along with a handler function that applies the standard serialization
93
+ logic, and can modify the resulting value before returning it as the final output of serialization.
94
+
95
+ For example, here's a scenario in which a wrap serializer transforms timezones to UTC **and** utilizes the existing `datetime` serialization logic.
96
+
97
+ ```python
98
+ from datetime import datetime, timezone
99
+ from typing import Annotated, Any
100
+
101
+ from pboot_validator import (
102
+ BaseModel,
103
+ SerializationInfo,
104
+ SerializerFunctionWrapHandler,
105
+ WrapSerializer,
106
+ )
107
+
108
+ def convert_to_utc(
109
+ value: Any, handler: SerializerFunctionWrapHandler, info: SerializationInfo
110
+ ):
111
+ # Note that `handler` can actually help serialize the `value` for
112
+ # further custom serialization in case it's a subclass.
113
+ serialized = handler(value)
114
+ if info.mode == 'json':
115
+ return datetime.fromisoformat(serialized).astimezone(timezone.utc)
116
+ else:
117
+ return serialized.astimezone(timezone.utc)
118
+
119
+ UTCDatetime = Annotated[datetime, WrapSerializer(convert_to_utc)]
120
+
121
+ class EventModel(BaseModel):
122
+ event_datetime: UTCDatetime
123
+
124
+ event = EventModel(event_datetime='2024-01-01T07:00:00-08:00')
125
+ print(event.model_dump())
126
+ '''
127
+ {
128
+ 'event_datetime': datetime.datetime(
129
+ 2024, 1, 1, 15, 0, tzinfo=datetime.timezone.utc
130
+ )
131
+ }
132
+ '''
133
+
134
+ print(event.model_dump_json())
135
+ #> {"event_datetime":"2024-01-01T15:00:00Z"}
136
+ ```
137
+
138
+ Attributes:
139
+ func: The serializer function to be wrapped.
140
+ return_type: The return type for the function. If omitted it will be inferred from the type annotation.
141
+ when_used: Determines when this serializer should be used. Accepts a string with values `'always'`,
142
+ `'unless-none'`, `'json'`, and `'json-unless-none'`. Defaults to 'always'.
143
+ """
144
+
145
+ func: core_schema.WrapSerializerFunction
146
+ return_type: TypeForm[Any] = PydanticUndefined # pyright: ignore[reportAssignmentType]
147
+ when_used: WhenUsed = 'always'
148
+
149
+ def __get_pydantic_core_schema__(self, source_type: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
150
+ """This method is used to get the Pydantic core schema of the class.
151
+
152
+ Args:
153
+ source_type: Source type.
154
+ handler: Core schema handler.
155
+
156
+ Returns:
157
+ The generated core schema of the class.
158
+ """
159
+ schema = handler(source_type)
160
+ if self.return_type is not PydanticUndefined:
161
+ return_type = self.return_type
162
+ else:
163
+ try:
164
+ # Do not pass in globals as the function could be defined in a different module.
165
+ # Instead, let `get_callable_return_type` infer the globals to use, but still pass
166
+ # in locals that may contain a parent/rebuild namespace:
167
+ return_type = _decorators.get_callable_return_type(
168
+ self.func,
169
+ localns=handler._get_types_namespace().locals,
170
+ )
171
+ except NameError as e:
172
+ raise PydanticUndefinedAnnotation.from_name_error(e) from e
173
+
174
+ return_schema = None if return_type is PydanticUndefined else handler.generate_schema(return_type)
175
+ schema['serialization'] = core_schema.wrap_serializer_function_ser_schema(
176
+ function=self.func,
177
+ info_arg=_decorators.inspect_annotated_serializer(self.func, 'wrap'),
178
+ return_schema=return_schema,
179
+ when_used=self.when_used,
180
+ )
181
+ return schema
182
+
183
+
184
+ if TYPE_CHECKING:
185
+ _Partial: TypeAlias = 'partial[Any] | partialmethod[Any]'
186
+
187
+ FieldPlainSerializer: TypeAlias = 'core_schema.SerializerFunction | _Partial'
188
+ """A field serializer method or function in `plain` mode."""
189
+
190
+ FieldWrapSerializer: TypeAlias = 'core_schema.WrapSerializerFunction | _Partial'
191
+ """A field serializer method or function in `wrap` mode."""
192
+
193
+ FieldSerializer: TypeAlias = 'FieldPlainSerializer | FieldWrapSerializer'
194
+ """A field serializer method or function."""
195
+
196
+ _FieldPlainSerializerT = TypeVar('_FieldPlainSerializerT', bound=FieldPlainSerializer)
197
+ _FieldWrapSerializerT = TypeVar('_FieldWrapSerializerT', bound=FieldWrapSerializer)
198
+
199
+
200
+ @overload
201
+ def field_serializer(
202
+ field: str,
203
+ /,
204
+ *fields: str,
205
+ mode: Literal['wrap'],
206
+ return_type: TypeForm[Any] = ...,
207
+ when_used: WhenUsed = ...,
208
+ check_fields: bool | None = ...,
209
+ ) -> Callable[[_FieldWrapSerializerT], _FieldWrapSerializerT]: ...
210
+
211
+
212
+ @overload
213
+ def field_serializer(
214
+ field: str,
215
+ /,
216
+ *fields: str,
217
+ mode: Literal['plain'] = ...,
218
+ return_type: TypeForm[Any] = ...,
219
+ when_used: WhenUsed = ...,
220
+ check_fields: bool | None = ...,
221
+ ) -> Callable[[_FieldPlainSerializerT], _FieldPlainSerializerT]: ...
222
+
223
+
224
+ def field_serializer( # noqa: D417
225
+ field: str,
226
+ /,
227
+ *fields: str,
228
+ mode: Literal['plain', 'wrap'] = 'plain',
229
+ return_type: TypeForm[Any] = PydanticUndefined, # pyright: ignore[reportArgumentType]
230
+ when_used: WhenUsed = 'always',
231
+ check_fields: bool | None = None,
232
+ ) -> (
233
+ Callable[[_FieldWrapSerializerT], _FieldWrapSerializerT]
234
+ | Callable[[_FieldPlainSerializerT], _FieldPlainSerializerT]
235
+ ):
236
+ """Decorator that enables custom field serialization.
237
+
238
+ In the below example, a field of type `set` is used to mitigate duplication. A `field_serializer` is used to serialize the data as a sorted list.
239
+
240
+ ```python
241
+ from pboot_validator import BaseModel, field_serializer
242
+
243
+ class StudentModel(BaseModel):
244
+ name: str = 'Jane'
245
+ courses: set[str]
246
+
247
+ @field_serializer('courses', when_used='json')
248
+ def serialize_courses_in_order(self, courses: set[str]):
249
+ return sorted(courses)
250
+
251
+ student = StudentModel(courses={'Math', 'Chemistry', 'English'})
252
+ print(student.model_dump_json())
253
+ #> {"name":"Jane","courses":["Chemistry","English","Math"]}
254
+ ```
255
+
256
+ See [the usage documentation](../concepts/serialization.md#serializers) for more information.
257
+
258
+ Four signatures are supported for the decorated serializer:
259
+
260
+ - `(self, value: Any, info: FieldSerializationInfo)`
261
+ - `(self, value: Any, nxt: SerializerFunctionWrapHandler, info: FieldSerializationInfo)`
262
+ - `(value: Any, info: SerializationInfo)`
263
+ - `(value: Any, nxt: SerializerFunctionWrapHandler, info: SerializationInfo)`
264
+
265
+ Args:
266
+ *fields: The field names the serializer should apply to.
267
+ mode: The serialization mode.
268
+
269
+ - `plain` means the function will be called instead of the default serialization logic,
270
+ - `wrap` means the function will be called with an argument to optionally call the
271
+ default serialization logic.
272
+ return_type: Optional return type for the function, if omitted it will be inferred from the type annotation.
273
+ when_used: Determines the serializer will be used for serialization.
274
+ check_fields: Whether to check that the fields actually exist on the model.
275
+
276
+ Raises:
277
+ PydanticUserError:
278
+ - If the decorator is used without any arguments (at least one field name must be provided).
279
+ - If the provided field names are not strings.
280
+ """
281
+ if callable(field) or isinstance(field, classmethod):
282
+ raise PydanticUserError(
283
+ 'The `@field_serializer` decorator cannot be used without arguments, at least one field must be provided. '
284
+ "For example: `@field_serializer('<field_name>', ...)`.",
285
+ code='decorator-missing-arguments',
286
+ )
287
+
288
+ fields = field, *fields
289
+ if not all(isinstance(field, str) for field in fields):
290
+ raise PydanticUserError(
291
+ 'The provided field names to the `@field_serializer` decorator should be strings. '
292
+ "For example: `@field_serializer('<field_name_1>', '<field_name_2>', ...).`",
293
+ code='decorator-invalid-fields',
294
+ )
295
+
296
+ def dec(f: FieldSerializer) -> _decorators.PydanticDescriptorProxy[Any]:
297
+ dec_info = _decorators.FieldSerializerDecoratorInfo(
298
+ fields=fields,
299
+ mode=mode,
300
+ return_type=return_type,
301
+ when_used=when_used,
302
+ check_fields=check_fields,
303
+ )
304
+ return _decorators.PydanticDescriptorProxy(f, dec_info) # pyright: ignore[reportArgumentType]
305
+
306
+ return dec # pyright: ignore[reportReturnType]
307
+
308
+
309
+ if TYPE_CHECKING:
310
+ # The first argument in the following callables represent the `self` type:
311
+
312
+ ModelPlainSerializerWithInfo: TypeAlias = Callable[[Any, SerializationInfo[Any]], Any]
313
+ """A model serializer method with the `info` argument, in `plain` mode."""
314
+
315
+ ModelPlainSerializerWithoutInfo: TypeAlias = Callable[[Any], Any]
316
+ """A model serializer method without the `info` argument, in `plain` mode."""
317
+
318
+ ModelPlainSerializer: TypeAlias = 'ModelPlainSerializerWithInfo | ModelPlainSerializerWithoutInfo'
319
+ """A model serializer method in `plain` mode."""
320
+
321
+ ModelWrapSerializerWithInfo: TypeAlias = Callable[[Any, SerializerFunctionWrapHandler, SerializationInfo[Any]], Any]
322
+ """A model serializer method with the `info` argument, in `wrap` mode."""
323
+
324
+ ModelWrapSerializerWithoutInfo: TypeAlias = Callable[[Any, SerializerFunctionWrapHandler], Any]
325
+ """A model serializer method without the `info` argument, in `wrap` mode."""
326
+
327
+ ModelWrapSerializer: TypeAlias = 'ModelWrapSerializerWithInfo | ModelWrapSerializerWithoutInfo'
328
+ """A model serializer method in `wrap` mode."""
329
+
330
+ ModelSerializer: TypeAlias = 'ModelPlainSerializer | ModelWrapSerializer'
331
+
332
+ _ModelPlainSerializerT = TypeVar('_ModelPlainSerializerT', bound=ModelPlainSerializer)
333
+ _ModelWrapSerializerT = TypeVar('_ModelWrapSerializerT', bound=ModelWrapSerializer)
334
+
335
+
336
+ @overload
337
+ def model_serializer(f: _ModelPlainSerializerT, /) -> _ModelPlainSerializerT: ...
338
+
339
+
340
+ @overload
341
+ def model_serializer(
342
+ *, mode: Literal['wrap'], when_used: WhenUsed = 'always', return_type: TypeForm[Any] = ...
343
+ ) -> Callable[[_ModelWrapSerializerT], _ModelWrapSerializerT]: ...
344
+
345
+
346
+ @overload
347
+ def model_serializer(
348
+ *,
349
+ mode: Literal['plain'] = ...,
350
+ when_used: WhenUsed = 'always',
351
+ return_type: TypeForm[Any] = ...,
352
+ ) -> Callable[[_ModelPlainSerializerT], _ModelPlainSerializerT]: ...
353
+
354
+
355
+ def model_serializer( # pyright: ignore[reportInconsistentOverload]
356
+ f: _ModelPlainSerializerT | _ModelWrapSerializerT | None = None,
357
+ /,
358
+ *,
359
+ mode: Literal['plain', 'wrap'] = 'plain',
360
+ when_used: WhenUsed = 'always',
361
+ return_type: TypeForm[Any] = PydanticUndefined, # pyright: ignore[reportArgumentType]
362
+ ) -> (
363
+ _ModelPlainSerializerT
364
+ | Callable[[_ModelWrapSerializerT], _ModelWrapSerializerT]
365
+ | Callable[[_ModelPlainSerializerT], _ModelPlainSerializerT]
366
+ ):
367
+ """Decorator that enables custom model serialization.
368
+
369
+ This is useful when a model need to be serialized in a customized manner, allowing for flexibility beyond just specific fields.
370
+
371
+ An example would be to serialize temperature to the same temperature scale, such as degrees Celsius.
372
+
373
+ ```python
374
+ from typing import Literal
375
+
376
+ from pboot_validator import BaseModel, model_serializer
377
+
378
+ class TemperatureModel(BaseModel):
379
+ unit: Literal['C', 'F']
380
+ value: int
381
+
382
+ @model_serializer()
383
+ def serialize_model(self):
384
+ if self.unit == 'F':
385
+ return {'unit': 'C', 'value': int((self.value - 32) / 1.8)}
386
+ return {'unit': self.unit, 'value': self.value}
387
+
388
+ temperature = TemperatureModel(unit='F', value=212)
389
+ print(temperature.model_dump())
390
+ #> {'unit': 'C', 'value': 100}
391
+ ```
392
+
393
+ Two signatures are supported for `mode='plain'`, which is the default:
394
+
395
+ - `(self)`
396
+ - `(self, info: SerializationInfo)`
397
+
398
+ And two other signatures for `mode='wrap'`:
399
+
400
+ - `(self, nxt: SerializerFunctionWrapHandler)`
401
+ - `(self, nxt: SerializerFunctionWrapHandler, info: SerializationInfo)`
402
+
403
+ See [the usage documentation](../concepts/serialization.md#serializers) for more information.
404
+
405
+ Args:
406
+ f: The function to be decorated.
407
+ mode: The serialization mode.
408
+
409
+ - `'plain'` means the function will be called instead of the default serialization logic
410
+ - `'wrap'` means the function will be called with an argument to optionally call the default
411
+ serialization logic.
412
+ when_used: Determines when this serializer should be used.
413
+ return_type: The return type for the function. If omitted it will be inferred from the type annotation.
414
+
415
+ Returns:
416
+ The decorator function.
417
+ """
418
+
419
+ def dec(f: ModelSerializer) -> _decorators.PydanticDescriptorProxy[Any]:
420
+ dec_info = _decorators.ModelSerializerDecoratorInfo(mode=mode, return_type=return_type, when_used=when_used)
421
+ return _decorators.PydanticDescriptorProxy(f, dec_info)
422
+
423
+ if f is None:
424
+ return dec # pyright: ignore[reportReturnType]
425
+ else:
426
+ return dec(f) # pyright: ignore[reportReturnType]
427
+
428
+
429
+ AnyType = TypeVar('AnyType')
430
+
431
+
432
+ if TYPE_CHECKING:
433
+ SerializeAsAny = Annotated[AnyType, ...] # SerializeAsAny[list[str]] will be treated by type checkers as list[str]
434
+ """Annotation used to mark a type as having duck-typing serialization behavior.
435
+
436
+ See [usage documentation](../concepts/serialization.md#serializing-with-duck-typing) for more details.
437
+ """
438
+ else:
439
+
440
+ @dataclasses.dataclass(slots=True)
441
+ class SerializeAsAny:
442
+ """Annotation used to mark a type as having duck-typing serialization behavior.
443
+
444
+ See [usage documentation](../concepts/serialization.md#serializing-with-duck-typing) for more details.
445
+ """
446
+
447
+ def __class_getitem__(cls, item: Any) -> Any:
448
+ return Annotated[item, SerializeAsAny()]
449
+
450
+ def __get_pydantic_core_schema__(
451
+ self, source_type: Any, handler: GetCoreSchemaHandler
452
+ ) -> core_schema.CoreSchema:
453
+ schema = handler(source_type)
454
+ schema_to_update = schema
455
+ while schema_to_update['type'] == 'definitions':
456
+ schema_to_update = schema_to_update.copy()
457
+ schema_to_update = schema_to_update['schema']
458
+ schema_to_update['serialization'] = core_schema.simple_ser_schema('any')
459
+ return schema
460
+
461
+ __hash__ = object.__hash__