@@ -93,6 +93,12 @@ def __repr__(self) -> str:
93
93
...
94
94
95
95
96
+ class FieldSerializationInfo (SerializationInfo , Protocol ):
97
+ @property
98
+ def field_name (self ) -> str :
99
+ ...
100
+
101
+
96
102
class ValidationInfo (Protocol ):
97
103
"""
98
104
Argument passed to validation functions.
@@ -109,7 +115,7 @@ def config(self) -> CoreConfig | None:
109
115
...
110
116
111
117
112
- class ModelFieldValidationInfo (ValidationInfo , Protocol ):
118
+ class FieldValidationInfo (ValidationInfo , Protocol ):
113
119
"""
114
120
Argument passed to model field validation functions.
115
121
"""
@@ -166,11 +172,26 @@ def simple_ser_schema(type: ExpectedSerializationTypes) -> SimpleSerSchema:
166
172
return SimpleSerSchema (type = type )
167
173
168
174
169
- class SerializePlainFunction (Protocol ): # pragma: no cover
175
+ class GeneralSerializePlainFunction (Protocol ): # pragma: no cover
170
176
def __call__ (self , __input_value : Any , __info : SerializationInfo ) -> Any :
171
177
...
172
178
173
179
180
+ class FieldSerializePlainFunction (Protocol ): # pragma: no cover
181
+ def __call__ (self , __model : Any , __input_value : Any , __info : FieldSerializationInfo ) -> Any :
182
+ ...
183
+
184
+
185
+ class GeneralSerializePlainFunctionSchema (TypedDict ):
186
+ type : Literal ['general' ]
187
+ function : GeneralSerializePlainFunction
188
+
189
+
190
+ class FieldSerializePlainFunctionSchema (TypedDict ):
191
+ type : Literal ['field' ]
192
+ function : FieldSerializePlainFunction
193
+
194
+
174
195
# must match `src/serializers/ob_type.rs::ObType`
175
196
JsonReturnTypes = Literal [
176
197
'int' ,
@@ -212,13 +233,41 @@ def __call__(self, __input_value: Any, __info: SerializationInfo) -> Any:
212
233
213
234
class FunctionPlainSerSchema (TypedDict , total = False ):
214
235
type : Required [Literal ['function-plain' ]]
215
- function : Required [SerializePlainFunction ]
236
+ function : Required [Union [ GeneralSerializePlainFunctionSchema , FieldSerializePlainFunctionSchema ] ]
216
237
json_return_type : JsonReturnTypes
217
238
when_used : WhenUsed # default: 'always'
218
239
219
240
220
- def function_plain_ser_schema (
221
- function : SerializePlainFunction , * , json_return_type : JsonReturnTypes | None = None , when_used : WhenUsed = 'always'
241
+ def general_function_plain_ser_schema (
242
+ function : GeneralSerializePlainFunction ,
243
+ * ,
244
+ json_return_type : JsonReturnTypes | None = None ,
245
+ when_used : WhenUsed = 'always' ,
246
+ ) -> FunctionPlainSerSchema :
247
+ """
248
+ Returns a schema for serialization with a function.
249
+
250
+ Args:
251
+ function: The function to use for serialization
252
+ json_return_type: The type that the function returns if `mode='json'`
253
+ when_used: When the function should be called
254
+ """
255
+ if when_used == 'always' :
256
+ # just to avoid extra elements in schema, and to use the actual default defined in rust
257
+ when_used = None # type: ignore
258
+ return dict_not_none (
259
+ type = 'function-plain' ,
260
+ function = {'type' : 'general' , 'function' : function },
261
+ json_return_type = json_return_type ,
262
+ when_used = when_used ,
263
+ )
264
+
265
+
266
+ def field_function_plain_ser_schema (
267
+ function : FieldSerializePlainFunction ,
268
+ * ,
269
+ json_return_type : JsonReturnTypes | None = None ,
270
+ when_used : WhenUsed = 'always' ,
222
271
) -> FunctionPlainSerSchema :
223
272
"""
224
273
Returns a schema for serialization with a function.
@@ -232,7 +281,10 @@ def function_plain_ser_schema(
232
281
# just to avoid extra elements in schema, and to use the actual default defined in rust
233
282
when_used = None # type: ignore
234
283
return dict_not_none (
235
- type = 'function-plain' , function = function , json_return_type = json_return_type , when_used = when_used
284
+ type = 'function-plain' ,
285
+ function = {'type' : 'field' , 'function' : function },
286
+ json_return_type = json_return_type ,
287
+ when_used = when_used ,
236
288
)
237
289
238
290
@@ -241,21 +293,38 @@ def __call__(self, __input_value: Any, __index_key: int | str | None = None) ->
241
293
...
242
294
243
295
244
- class SerializeWrapFunction (Protocol ): # pragma: no cover
296
+ class GeneralSerializeWrapFunction (Protocol ): # pragma: no cover
245
297
def __call__ (self , __input_value : Any , __serializer : SerializeWrapHandler , __info : SerializationInfo ) -> Any :
246
298
...
247
299
248
300
301
+ class FieldSerializeWrapFunction (Protocol ): # pragma: no cover
302
+ def __call__ (
303
+ self , __model : Any , __input_value : Any , __serializer : SerializeWrapHandler , __info : FieldSerializationInfo
304
+ ) -> Any :
305
+ ...
306
+
307
+
308
+ class GeneralSerializeWrapFunctionSchema (TypedDict ):
309
+ type : Literal ['general' ]
310
+ function : GeneralSerializeWrapFunction
311
+
312
+
313
+ class FieldSerializeWrapFunctionSchema (TypedDict ):
314
+ type : Literal ['field' ]
315
+ function : FieldSerializeWrapFunction
316
+
317
+
249
318
class FunctionWrapSerSchema (TypedDict , total = False ):
250
319
type : Required [Literal ['function-wrap' ]]
251
- function : Required [SerializeWrapFunction ]
320
+ function : Required [Union [ GeneralSerializeWrapFunctionSchema , FieldSerializeWrapFunctionSchema ] ]
252
321
schema : Required [CoreSchema ]
253
322
json_return_type : JsonReturnTypes
254
323
when_used : WhenUsed # default: 'always'
255
324
256
325
257
- def function_wrap_ser_schema (
258
- function : SerializeWrapFunction ,
326
+ def general_function_wrap_ser_schema (
327
+ function : GeneralSerializeWrapFunction ,
259
328
schema : CoreSchema ,
260
329
* ,
261
330
json_return_type : JsonReturnTypes | None = None ,
@@ -274,7 +343,39 @@ def function_wrap_ser_schema(
274
343
# just to avoid extra elements in schema, and to use the actual default defined in rust
275
344
when_used = None # type: ignore
276
345
return dict_not_none (
277
- type = 'function-wrap' , schema = schema , function = function , json_return_type = json_return_type , when_used = when_used
346
+ type = 'function-wrap' ,
347
+ schema = schema ,
348
+ function = {'type' : 'general' , 'function' : function },
349
+ json_return_type = json_return_type ,
350
+ when_used = when_used ,
351
+ )
352
+
353
+
354
+ def field_function_wrap_ser_schema (
355
+ function : FieldSerializeWrapFunction ,
356
+ schema : CoreSchema ,
357
+ * ,
358
+ json_return_type : JsonReturnTypes | None = None ,
359
+ when_used : WhenUsed = 'always' ,
360
+ ) -> FunctionWrapSerSchema :
361
+ """
362
+ Returns a schema for serialization with a function for a model field.
363
+
364
+ Args:
365
+ function: The function to use for serialization
366
+ schema: The schema to use for the inner serialization
367
+ json_return_type: The type that the function returns if `mode='json'`
368
+ when_used: When the function should be called
369
+ """
370
+ if when_used == 'always' :
371
+ # just to avoid extra elements in schema, and to use the actual default defined in rust
372
+ when_used = None # type: ignore
373
+ return dict_not_none (
374
+ type = 'function-wrap' ,
375
+ schema = schema ,
376
+ function = {'type' : 'field' , 'function' : function },
377
+ json_return_type = json_return_type ,
378
+ when_used = when_used ,
278
379
)
279
380
280
381
@@ -290,7 +391,7 @@ def format_ser_schema(formatting_string: str, *, when_used: WhenUsed = 'json-unl
290
391
291
392
Args:
292
393
formatting_string: String defining the format to use
293
- when_used: Same meaning as for [function_plain_ser_schema ], but with a different default
394
+ when_used: Same meaning as for [general_function_plain_ser_schema ], but with a different default
294
395
"""
295
396
if when_used == 'json-unless-none' :
296
397
# just to avoid extra elements in schema, and to use the actual default defined in rust
@@ -308,7 +409,7 @@ def to_string_ser_schema(*, when_used: WhenUsed = 'json-unless-none') -> ToStrin
308
409
Returns a schema for serialization using python's `str()` / `__str__` method.
309
410
310
411
Args:
311
- when_used: Same meaning as for [function_plain_ser_schema ], but with a different default
412
+ when_used: Same meaning as for [general_function_plain_ser_schema ], but with a different default
312
413
"""
313
414
s = dict (type = 'to-string' )
314
415
if when_used != 'json-unless-none' :
@@ -1491,7 +1592,7 @@ def __call__(self, __input_value: Any, __info: ValidationInfo) -> Any: # pragma
1491
1592
1492
1593
1493
1594
class FieldValidatorFunction (Protocol ):
1494
- def __call__ (self , __input_value : Any , __info : ModelFieldValidationInfo ) -> Any : # pragma: no cover
1595
+ def __call__ (self , __input_value : Any , __info : FieldValidationInfo ) -> Any : # pragma: no cover
1495
1596
...
1496
1597
1497
1598
@@ -1532,7 +1633,7 @@ def field_before_validation_function(
1532
1633
```py
1533
1634
from pydantic_core import SchemaValidator, core_schema
1534
1635
1535
- def fn(v: bytes, info: core_schema.ModelFieldValidationInfo ) -> str:
1636
+ def fn(v: bytes, info: core_schema.FieldValidationInfo ) -> str:
1536
1637
assert info.data is not None
1537
1638
assert info.field_name is not None
1538
1639
return v.decode() + 'world'
@@ -1624,7 +1725,7 @@ def field_after_validation_function(
1624
1725
```py
1625
1726
from pydantic_core import SchemaValidator, core_schema
1626
1727
1627
- def fn(v: str, info: core_schema.ModelFieldValidationInfo ) -> str:
1728
+ def fn(v: str, info: core_schema.FieldValidationInfo ) -> str:
1628
1729
assert info.data is not None
1629
1730
assert info.field_name is not None
1630
1731
return v + 'world'
@@ -1709,7 +1810,7 @@ def __call__(
1709
1810
1710
1811
class FieldWrapValidatorFunction (Protocol ):
1711
1812
def __call__ (
1712
- self , __input_value : Any , __validator : CallableValidator , __info : ModelFieldValidationInfo
1813
+ self , __input_value : Any , __validator : CallableValidator , __info : FieldValidationInfo
1713
1814
) -> Any : # pragma: no cover
1714
1815
...
1715
1816
@@ -1791,7 +1892,7 @@ def field_wrap_validation_function(
1791
1892
```py
1792
1893
from pydantic_core import SchemaValidator, core_schema
1793
1894
1794
- def fn(v: bytes, validator: core_schema.CallableValidator, info: core_schema.ModelFieldValidationInfo ) -> str:
1895
+ def fn(v: bytes, validator: core_schema.CallableValidator, info: core_schema.FieldValidationInfo ) -> str:
1795
1896
assert info.data is not None
1796
1897
assert info.field_name is not None
1797
1898
return validator(v) + 'world'
@@ -1881,7 +1982,7 @@ def field_plain_validation_function(
1881
1982
from typing import Any
1882
1983
from pydantic_core import SchemaValidator, core_schema
1883
1984
1884
- def fn(v: Any, info: core_schema.ModelFieldValidationInfo ) -> str:
1985
+ def fn(v: Any, info: core_schema.FieldValidationInfo ) -> str:
1885
1986
assert info.data is not None
1886
1987
assert info.field_name is not None
1887
1988
return str(v) + 'world'
0 commit comments