functional_serializers.py 17 KB

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