1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
import pytest
from pydantic_core import SchemaSerializer, core_schema
def test_branch_nullable():
s = SchemaSerializer(
core_schema.definitions_schema(
core_schema.definition_reference_schema('Branch'),
[
core_schema.typed_dict_schema(
{
'name': core_schema.typed_dict_field(core_schema.str_schema()),
'sub_branch': core_schema.typed_dict_field(
core_schema.nullable_schema(core_schema.definition_reference_schema('Branch'))
),
},
ref='Branch',
)
],
)
)
assert s.to_python({'name': 'root', 'sub_branch': {'name': 'branch', 'sub_branch': None}}) == {
'name': 'root',
'sub_branch': {'name': 'branch', 'sub_branch': None},
}
assert s.to_python({'name': 'root', 'sub_branch': {'name': 'branch', 'sub_branch': None}}, exclude_none=True) == {
'name': 'root',
'sub_branch': {'name': 'branch'},
}
def test_cyclic_recursion():
s = SchemaSerializer(
core_schema.definitions_schema(
core_schema.definition_reference_schema('Branch'),
[
core_schema.typed_dict_schema(
{
'name': core_schema.typed_dict_field(core_schema.str_schema()),
'sub_branch': core_schema.typed_dict_field(
core_schema.nullable_schema(core_schema.definition_reference_schema('Branch'))
),
},
ref='Branch',
)
],
)
)
v = {'name': 'root'}
v['sub_branch'] = v
with pytest.raises(ValueError, match=r'Circular reference detected \(id repeated\)'):
s.to_python(v)
with pytest.raises(ValueError, match=r'Circular reference detected \(id repeated\)'):
s.to_python(v, mode='json')
with pytest.raises(ValueError, match=r'Circular reference detected \(id repeated\)'):
s.to_json(v)
def test_custom_ser():
s = SchemaSerializer(
core_schema.definitions_schema(
core_schema.definition_reference_schema('Branch'),
[
core_schema.typed_dict_schema(
{
'name': core_schema.typed_dict_field(core_schema.str_schema()),
'sub_branch': core_schema.typed_dict_field(
core_schema.nullable_schema(
core_schema.definition_reference_schema(
'Branch', serialization=core_schema.to_string_ser_schema(when_used='always')
)
)
),
},
ref='Branch',
)
],
)
)
assert s.to_python({'name': 'root', 'sub_branch': {'name': 'branch', 'sub_branch': None}}) == {
'name': 'root',
'sub_branch': "{'name': 'branch', 'sub_branch': None}",
}
def test_recursive_function():
s = SchemaSerializer(
core_schema.definitions_schema(
core_schema.definition_reference_schema('my_ref'),
[
core_schema.typed_dict_schema(
{'root': core_schema.typed_dict_field(core_schema.definition_reference_schema('my_ref'))},
ref='my_ref',
serialization=core_schema.wrap_serializer_function_ser_schema(function=lambda x, _handler: x),
)
],
)
)
assert s.to_python({'root': {'root': {}}}) == {'root': {'root': {}}}
def test_recursive_function_deeper_ref():
s = SchemaSerializer(
core_schema.typed_dict_schema(
{
'a': core_schema.typed_dict_field(
core_schema.definitions_schema(
core_schema.definition_reference_schema('my_ref'),
[
core_schema.typed_dict_schema(
{'b': core_schema.typed_dict_field(core_schema.definition_reference_schema('my_ref'))},
ref='my_ref',
)
],
)
)
},
serialization=core_schema.wrap_serializer_function_ser_schema(
function=lambda x, _handler: x, is_field_serializer=False
),
)
)
assert s.to_python({'a': {'b': {'b': {}}}}) == {'a': {'b': {'b': {}}}}
|