functional_serializers.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. """This module contains related classes and functions for serialization."""
  2. from __future__ import annotations
  3. import dataclasses
  4. from functools import partial, partialmethod
  5. from typing import TYPE_CHECKING, Annotated, Any, Callable, Literal, TypeVar, overload
  6. from pydantic_core import PydanticUndefined, core_schema
  7. from pydantic_core.core_schema import SerializationInfo, SerializerFunctionWrapHandler, WhenUsed
  8. from typing_extensions import TypeAlias
  9. from . import PydanticUndefinedAnnotation
  10. from ._internal import _decorators, _internal_dataclass
  11. from .annotated_handlers import GetCoreSchemaHandler
  12. from .errors import PydanticUserError
  13. @dataclasses.dataclass(**_internal_dataclass.slots_true, frozen=True)
  14. class PlainSerializer:
  15. """Plain serializers use a function to modify the output of serialization.
  16. This is particularly helpful when you want to customize the serialization for annotated types.
  17. Consider an input of `list`, which will be serialized into a space-delimited string.
  18. ```python
  19. from typing import Annotated
  20. from pydantic import BaseModel, PlainSerializer
  21. CustomStr = Annotated[
  22. list, PlainSerializer(lambda x: ' '.join(x), return_type=str)
  23. ]
  24. class StudentModel(BaseModel):
  25. courses: CustomStr
  26. student = StudentModel(courses=['Math', 'Chemistry', 'English'])
  27. print(student.model_dump())
  28. #> {'courses': 'Math Chemistry English'}
  29. ```
  30. Attributes:
  31. func: The serializer function.
  32. return_type: The return type for the function. If omitted it will be inferred from the type annotation.
  33. when_used: Determines when this serializer should be used. Accepts a string with values `'always'`,
  34. `'unless-none'`, `'json'`, and `'json-unless-none'`. Defaults to 'always'.
  35. """
  36. func: core_schema.SerializerFunction
  37. return_type: Any = PydanticUndefined
  38. when_used: WhenUsed = 'always'
  39. def __get_pydantic_core_schema__(self, source_type: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
  40. """Gets the Pydantic core schema.
  41. Args:
  42. source_type: The source type.
  43. handler: The `GetCoreSchemaHandler` instance.
  44. Returns:
  45. The Pydantic core schema.
  46. """
  47. schema = handler(source_type)
  48. if self.return_type is not PydanticUndefined:
  49. return_type = self.return_type
  50. else:
  51. try:
  52. # Do not pass in globals as the function could be defined in a different module.
  53. # Instead, let `get_callable_return_type` infer the globals to use, but still pass
  54. # in locals that may contain a parent/rebuild namespace:
  55. return_type = _decorators.get_callable_return_type(
  56. self.func,
  57. localns=handler._get_types_namespace().locals,
  58. )
  59. except NameError as e:
  60. raise PydanticUndefinedAnnotation.from_name_error(e) from e
  61. return_schema = None if return_type is PydanticUndefined else handler.generate_schema(return_type)
  62. schema['serialization'] = core_schema.plain_serializer_function_ser_schema(
  63. function=self.func,
  64. info_arg=_decorators.inspect_annotated_serializer(self.func, 'plain'),
  65. return_schema=return_schema,
  66. when_used=self.when_used,
  67. )
  68. return schema
  69. @dataclasses.dataclass(**_internal_dataclass.slots_true, frozen=True)
  70. class WrapSerializer:
  71. """Wrap serializers receive the raw inputs along with a handler function that applies the standard serialization
  72. logic, and can modify the resulting value before returning it as the final output of serialization.
  73. For example, here's a scenario in which a wrap serializer transforms timezones to UTC **and** utilizes the existing `datetime` serialization logic.
  74. ```python
  75. from datetime import datetime, timezone
  76. from typing import Annotated, Any
  77. from pydantic import BaseModel, WrapSerializer
  78. class EventDatetime(BaseModel):
  79. start: datetime
  80. end: datetime
  81. def convert_to_utc(value: Any, handler, info) -> dict[str, datetime]:
  82. # Note that `handler` can actually help serialize the `value` for
  83. # further custom serialization in case it's a subclass.
  84. partial_result = handler(value, info)
  85. if info.mode == 'json':
  86. return {
  87. k: datetime.fromisoformat(v).astimezone(timezone.utc)
  88. for k, v in partial_result.items()
  89. }
  90. return {k: v.astimezone(timezone.utc) for k, v in partial_result.items()}
  91. UTCEventDatetime = Annotated[EventDatetime, WrapSerializer(convert_to_utc)]
  92. class EventModel(BaseModel):
  93. event_datetime: UTCEventDatetime
  94. dt = EventDatetime(
  95. start='2024-01-01T07:00:00-08:00', end='2024-01-03T20:00:00+06:00'
  96. )
  97. event = EventModel(event_datetime=dt)
  98. print(event.model_dump())
  99. '''
  100. {
  101. 'event_datetime': {
  102. 'start': datetime.datetime(
  103. 2024, 1, 1, 15, 0, tzinfo=datetime.timezone.utc
  104. ),
  105. 'end': datetime.datetime(
  106. 2024, 1, 3, 14, 0, tzinfo=datetime.timezone.utc
  107. ),
  108. }
  109. }
  110. '''
  111. print(event.model_dump_json())
  112. '''
  113. {"event_datetime":{"start":"2024-01-01T15:00:00Z","end":"2024-01-03T14:00:00Z"}}
  114. '''
  115. ```
  116. Attributes:
  117. func: The serializer function to be wrapped.
  118. return_type: The return type for the function. If omitted it will be inferred from the type annotation.
  119. when_used: Determines when this serializer should be used. Accepts a string with values `'always'`,
  120. `'unless-none'`, `'json'`, and `'json-unless-none'`. Defaults to 'always'.
  121. """
  122. func: core_schema.WrapSerializerFunction
  123. return_type: Any = PydanticUndefined
  124. when_used: WhenUsed = 'always'
  125. def __get_pydantic_core_schema__(self, source_type: Any, handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
  126. """This method is used to get the Pydantic core schema of the class.
  127. Args:
  128. source_type: Source type.
  129. handler: Core schema handler.
  130. Returns:
  131. The generated core schema of the class.
  132. """
  133. schema = handler(source_type)
  134. if self.return_type is not PydanticUndefined:
  135. return_type = self.return_type
  136. else:
  137. try:
  138. # Do not pass in globals as the function could be defined in a different module.
  139. # Instead, let `get_callable_return_type` infer the globals to use, but still pass
  140. # in locals that may contain a parent/rebuild namespace:
  141. return_type = _decorators.get_callable_return_type(
  142. self.func,
  143. localns=handler._get_types_namespace().locals,
  144. )
  145. except NameError as e:
  146. raise PydanticUndefinedAnnotation.from_name_error(e) from e
  147. return_schema = None if return_type is PydanticUndefined else handler.generate_schema(return_type)
  148. schema['serialization'] = core_schema.wrap_serializer_function_ser_schema(
  149. function=self.func,
  150. info_arg=_decorators.inspect_annotated_serializer(self.func, 'wrap'),
  151. return_schema=return_schema,
  152. when_used=self.when_used,
  153. )
  154. return schema
  155. if TYPE_CHECKING:
  156. _Partial: TypeAlias = 'partial[Any] | partialmethod[Any]'
  157. FieldPlainSerializer: TypeAlias = 'core_schema.SerializerFunction | _Partial'
  158. """A field serializer method or function in `plain` mode."""
  159. FieldWrapSerializer: TypeAlias = 'core_schema.WrapSerializerFunction | _Partial'
  160. """A field serializer method or function in `wrap` mode."""
  161. FieldSerializer: TypeAlias = 'FieldPlainSerializer | FieldWrapSerializer'
  162. """A field serializer method or function."""
  163. _FieldPlainSerializerT = TypeVar('_FieldPlainSerializerT', bound=FieldPlainSerializer)
  164. _FieldWrapSerializerT = TypeVar('_FieldWrapSerializerT', bound=FieldWrapSerializer)
  165. @overload
  166. def field_serializer(
  167. field: str,
  168. /,
  169. *fields: str,
  170. mode: Literal['wrap'],
  171. return_type: Any = ...,
  172. when_used: WhenUsed = ...,
  173. check_fields: bool | None = ...,
  174. ) -> Callable[[_FieldWrapSerializerT], _FieldWrapSerializerT]: ...
  175. @overload
  176. def field_serializer(
  177. field: str,
  178. /,
  179. *fields: str,
  180. mode: Literal['plain'] = ...,
  181. return_type: Any = ...,
  182. when_used: WhenUsed = ...,
  183. check_fields: bool | None = ...,
  184. ) -> Callable[[_FieldPlainSerializerT], _FieldPlainSerializerT]: ...
  185. def field_serializer( # noqa: D417
  186. field: str,
  187. /,
  188. *fields: str,
  189. mode: Literal['plain', 'wrap'] = 'plain',
  190. # TODO PEP 747 (grep for 'return_type' on the whole code base):
  191. return_type: Any = PydanticUndefined,
  192. when_used: WhenUsed = 'always',
  193. check_fields: bool | None = None,
  194. ) -> (
  195. Callable[[_FieldWrapSerializerT], _FieldWrapSerializerT]
  196. | Callable[[_FieldPlainSerializerT], _FieldPlainSerializerT]
  197. ):
  198. """Decorator that enables custom field serialization.
  199. 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.
  200. ```python
  201. from pydantic import BaseModel, field_serializer
  202. class StudentModel(BaseModel):
  203. name: str = 'Jane'
  204. courses: set[str]
  205. @field_serializer('courses', when_used='json')
  206. def serialize_courses_in_order(self, courses: set[str]):
  207. return sorted(courses)
  208. student = StudentModel(courses={'Math', 'Chemistry', 'English'})
  209. print(student.model_dump_json())
  210. #> {"name":"Jane","courses":["Chemistry","English","Math"]}
  211. ```
  212. See [the usage documentation](../concepts/serialization.md#serializers) for more information.
  213. Four signatures are supported for the decorated serializer:
  214. - `(self, value: Any, info: FieldSerializationInfo)`
  215. - `(self, value: Any, nxt: SerializerFunctionWrapHandler, info: FieldSerializationInfo)`
  216. - `(value: Any, info: SerializationInfo)`
  217. - `(value: Any, nxt: SerializerFunctionWrapHandler, info: SerializationInfo)`
  218. Args:
  219. *fields: The field names the serializer should apply to.
  220. mode: The serialization mode.
  221. - `plain` means the function will be called instead of the default serialization logic,
  222. - `wrap` means the function will be called with an argument to optionally call the
  223. default serialization logic.
  224. return_type: Optional return type for the function, if omitted it will be inferred from the type annotation.
  225. when_used: Determines the serializer will be used for serialization.
  226. check_fields: Whether to check that the fields actually exist on the model.
  227. Raises:
  228. PydanticUserError:
  229. - If the decorator is used without any arguments (at least one field name must be provided).
  230. - If the provided field names are not strings.
  231. """
  232. if callable(field) or isinstance(field, classmethod):
  233. raise PydanticUserError(
  234. 'The `@field_serializer` decorator cannot be used without arguments, at least one field must be provided. '
  235. "For example: `@field_serializer('<field_name>', ...)`.",
  236. code='decorator-missing-arguments',
  237. )
  238. fields = field, *fields
  239. if not all(isinstance(field, str) for field in fields):
  240. raise PydanticUserError(
  241. 'The provided field names to the `@field_serializer` decorator should be strings. '
  242. "For example: `@field_serializer('<field_name_1>', '<field_name_2>', ...).`",
  243. code='decorator-invalid-fields',
  244. )
  245. def dec(f: FieldSerializer) -> _decorators.PydanticDescriptorProxy[Any]:
  246. dec_info = _decorators.FieldSerializerDecoratorInfo(
  247. fields=fields,
  248. mode=mode,
  249. return_type=return_type,
  250. when_used=when_used,
  251. check_fields=check_fields,
  252. )
  253. return _decorators.PydanticDescriptorProxy(f, dec_info) # pyright: ignore[reportArgumentType]
  254. return dec # pyright: ignore[reportReturnType]
  255. if TYPE_CHECKING:
  256. # The first argument in the following callables represent the `self` type:
  257. ModelPlainSerializerWithInfo: TypeAlias = Callable[[Any, SerializationInfo[Any]], Any]
  258. """A model serializer method with the `info` argument, in `plain` mode."""
  259. ModelPlainSerializerWithoutInfo: TypeAlias = Callable[[Any], Any]
  260. """A model serializer method without the `info` argument, in `plain` mode."""
  261. ModelPlainSerializer: TypeAlias = 'ModelPlainSerializerWithInfo | ModelPlainSerializerWithoutInfo'
  262. """A model serializer method in `plain` mode."""
  263. ModelWrapSerializerWithInfo: TypeAlias = Callable[[Any, SerializerFunctionWrapHandler, SerializationInfo[Any]], Any]
  264. """A model serializer method with the `info` argument, in `wrap` mode."""
  265. ModelWrapSerializerWithoutInfo: TypeAlias = Callable[[Any, SerializerFunctionWrapHandler], Any]
  266. """A model serializer method without the `info` argument, in `wrap` mode."""
  267. ModelWrapSerializer: TypeAlias = 'ModelWrapSerializerWithInfo | ModelWrapSerializerWithoutInfo'
  268. """A model serializer method in `wrap` mode."""
  269. ModelSerializer: TypeAlias = 'ModelPlainSerializer | ModelWrapSerializer'
  270. _ModelPlainSerializerT = TypeVar('_ModelPlainSerializerT', bound=ModelPlainSerializer)
  271. _ModelWrapSerializerT = TypeVar('_ModelWrapSerializerT', bound=ModelWrapSerializer)
  272. @overload
  273. def model_serializer(f: _ModelPlainSerializerT, /) -> _ModelPlainSerializerT: ...
  274. @overload
  275. def model_serializer(
  276. *, mode: Literal['wrap'], when_used: WhenUsed = 'always', return_type: Any = ...
  277. ) -> Callable[[_ModelWrapSerializerT], _ModelWrapSerializerT]: ...
  278. @overload
  279. def model_serializer(
  280. *,
  281. mode: Literal['plain'] = ...,
  282. when_used: WhenUsed = 'always',
  283. return_type: Any = ...,
  284. ) -> Callable[[_ModelPlainSerializerT], _ModelPlainSerializerT]: ...
  285. def model_serializer(
  286. f: _ModelPlainSerializerT | _ModelWrapSerializerT | None = None,
  287. /,
  288. *,
  289. mode: Literal['plain', 'wrap'] = 'plain',
  290. when_used: WhenUsed = 'always',
  291. return_type: Any = PydanticUndefined,
  292. ) -> (
  293. _ModelPlainSerializerT
  294. | Callable[[_ModelWrapSerializerT], _ModelWrapSerializerT]
  295. | Callable[[_ModelPlainSerializerT], _ModelPlainSerializerT]
  296. ):
  297. """Decorator that enables custom model serialization.
  298. This is useful when a model need to be serialized in a customized manner, allowing for flexibility beyond just specific fields.
  299. An example would be to serialize temperature to the same temperature scale, such as degrees Celsius.
  300. ```python
  301. from typing import Literal
  302. from pydantic import BaseModel, model_serializer
  303. class TemperatureModel(BaseModel):
  304. unit: Literal['C', 'F']
  305. value: int
  306. @model_serializer()
  307. def serialize_model(self):
  308. if self.unit == 'F':
  309. return {'unit': 'C', 'value': int((self.value - 32) / 1.8)}
  310. return {'unit': self.unit, 'value': self.value}
  311. temperature = TemperatureModel(unit='F', value=212)
  312. print(temperature.model_dump())
  313. #> {'unit': 'C', 'value': 100}
  314. ```
  315. Two signatures are supported for `mode='plain'`, which is the default:
  316. - `(self)`
  317. - `(self, info: SerializationInfo)`
  318. And two other signatures for `mode='wrap'`:
  319. - `(self, nxt: SerializerFunctionWrapHandler)`
  320. - `(self, nxt: SerializerFunctionWrapHandler, info: SerializationInfo)`
  321. See [the usage documentation](../concepts/serialization.md#serializers) for more information.
  322. Args:
  323. f: The function to be decorated.
  324. mode: The serialization mode.
  325. - `'plain'` means the function will be called instead of the default serialization logic
  326. - `'wrap'` means the function will be called with an argument to optionally call the default
  327. serialization logic.
  328. when_used: Determines when this serializer should be used.
  329. return_type: The return type for the function. If omitted it will be inferred from the type annotation.
  330. Returns:
  331. The decorator function.
  332. """
  333. def dec(f: ModelSerializer) -> _decorators.PydanticDescriptorProxy[Any]:
  334. dec_info = _decorators.ModelSerializerDecoratorInfo(mode=mode, return_type=return_type, when_used=when_used)
  335. return _decorators.PydanticDescriptorProxy(f, dec_info)
  336. if f is None:
  337. return dec # pyright: ignore[reportReturnType]
  338. else:
  339. return dec(f) # pyright: ignore[reportReturnType]
  340. AnyType = TypeVar('AnyType')
  341. if TYPE_CHECKING:
  342. SerializeAsAny = Annotated[AnyType, ...] # SerializeAsAny[list[str]] will be treated by type checkers as list[str]
  343. """Annotation used to mark a type as having duck-typing serialization behavior.
  344. See [usage documentation](../concepts/serialization.md#serializing-with-duck-typing) for more details.
  345. """
  346. else:
  347. @dataclasses.dataclass(**_internal_dataclass.slots_true)
  348. class SerializeAsAny:
  349. """Annotation used to mark a type as having duck-typing serialization behavior.
  350. See [usage documentation](../concepts/serialization.md#serializing-with-duck-typing) for more details.
  351. """
  352. def __class_getitem__(cls, item: Any) -> Any:
  353. return Annotated[item, SerializeAsAny()]
  354. def __get_pydantic_core_schema__(
  355. self, source_type: Any, handler: GetCoreSchemaHandler
  356. ) -> core_schema.CoreSchema:
  357. schema = handler(source_type)
  358. schema_to_update = schema
  359. while schema_to_update['type'] == 'definitions':
  360. schema_to_update = schema_to_update.copy()
  361. schema_to_update = schema_to_update['schema']
  362. schema_to_update['serialization'] = core_schema.simple_ser_schema('any')
  363. return schema
  364. __hash__ = object.__hash__