Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pydantic/_internal/_serializers.py: 38%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

21 statements  

1from __future__ import annotations 

2 

3import collections 

4import collections.abc 

5import typing 

6from typing import Any 

7 

8from pydantic_core import PydanticOmit, core_schema 

9 

10SEQUENCE_ORIGIN_MAP: dict[Any, Any] = { 

11 typing.Deque: collections.deque, # noqa: UP006 

12 collections.deque: collections.deque, 

13 list: list, 

14 typing.List: list, # noqa: UP006 

15 tuple: tuple, 

16 typing.Tuple: tuple, # noqa: UP006 

17 set: set, 

18 typing.AbstractSet: set, 

19 typing.Set: set, # noqa: UP006 

20 frozenset: frozenset, 

21 typing.FrozenSet: frozenset, # noqa: UP006 

22 typing.Sequence: list, 

23 typing.MutableSequence: list, 

24 typing.MutableSet: set, 

25 # this doesn't handle subclasses of these 

26 # parametrized typing.Set creates one of these 

27 collections.abc.MutableSet: set, 

28 collections.abc.Set: frozenset, 

29} 

30 

31 

32def serialize_sequence_via_list( 

33 v: Any, handler: core_schema.SerializerFunctionWrapHandler, info: core_schema.SerializationInfo 

34) -> Any: 

35 items: list[Any] = [] 

36 

37 mapped_origin = SEQUENCE_ORIGIN_MAP.get(type(v), None) 

38 if mapped_origin is None: 

39 # we shouldn't hit this branch, should probably add a serialization error or something 

40 return v 

41 

42 for index, item in enumerate(v): 

43 try: 

44 v = handler(item, index) 

45 except PydanticOmit: # noqa: PERF203 

46 pass 

47 else: 

48 items.append(v) 

49 

50 if info.mode_is_json(): 

51 return items 

52 else: 

53 return mapped_origin(items)