-
Notifications
You must be signed in to change notification settings - Fork 247
/
test_schema_functions.py
362 lines (326 loc) · 13.6 KB
/
test_schema_functions.py
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
import dataclasses
import re
from datetime import date
from enum import Enum
from typing import Any
import pytest
from pydantic_core import SchemaError, SchemaSerializer, SchemaValidator, ValidationError, core_schema
def val_function(x, *args: Any):
return x
def make_5():
return 5
class MyModel:
__slots__ = '__dict__', '__pydantic_fields_set__', '__pydantic_extra__', '__pydantic_private__'
@dataclasses.dataclass
class MyDataclass:
x: int
y: str
class MyEnum(int, Enum):
a = 1
b = 2
def ids_function(val):
if callable(val):
return val.__name__
elif isinstance(val, tuple) and len(val) == 2:
return '({})'.format(', '.join([repr(a) for a in val[0]] + [f'{k}={v!r}' for k, v in val[1].items()]))
else:
return repr(val)
def args(*args, **kwargs):
return args, kwargs
all_schema_functions = [
(core_schema.any_schema, args(), {'type': 'any'}),
(core_schema.any_schema, args(metadata=['foot', 'spa']), {'type': 'any', 'metadata': ['foot', 'spa']}),
(core_schema.none_schema, args(), {'type': 'none'}),
(core_schema.bool_schema, args(), {'type': 'bool'}),
(core_schema.bool_schema, args(strict=True), {'type': 'bool', 'strict': True}),
(core_schema.int_schema, args(), {'type': 'int'}),
(core_schema.int_schema, args(metadata={'fred'}), {'type': 'int', 'metadata': {'fred'}}),
(core_schema.int_schema, args(multiple_of=5, gt=10, lt=20), {'type': 'int', 'multiple_of': 5, 'gt': 10, 'lt': 20}),
(core_schema.float_schema, args(), {'type': 'float'}),
(core_schema.float_schema, args(multiple_of=5, gt=1.2), {'type': 'float', 'multiple_of': 5, 'gt': 1.2}),
(core_schema.str_schema, args(), {'type': 'str'}),
(core_schema.str_schema, args(min_length=5, max_length=10), {'type': 'str', 'min_length': 5, 'max_length': 10}),
(core_schema.bytes_schema, args(), {'type': 'bytes'}),
(core_schema.bytes_schema, args(min_length=5, ref='xx'), {'type': 'bytes', 'min_length': 5, 'ref': 'xx'}),
(core_schema.date_schema, args(), {'type': 'date'}),
(core_schema.date_schema, args(gt=date(2020, 1, 1)), {'type': 'date', 'gt': date(2020, 1, 1)}),
(core_schema.time_schema, args(), {'type': 'time', 'microseconds_precision': 'truncate'}),
(core_schema.datetime_schema, args(), {'type': 'datetime', 'microseconds_precision': 'truncate'}),
(core_schema.timedelta_schema, args(), {'type': 'timedelta', 'microseconds_precision': 'truncate'}),
(
core_schema.time_schema,
args(microseconds_precision='error'),
{'type': 'time', 'microseconds_precision': 'error'},
),
(
core_schema.datetime_schema,
args(microseconds_precision='error'),
{'type': 'datetime', 'microseconds_precision': 'error'},
),
(
core_schema.timedelta_schema,
args(microseconds_precision='error'),
{'type': 'timedelta', 'microseconds_precision': 'error'},
),
(core_schema.literal_schema, args(['a', 'b']), {'type': 'literal', 'expected': ['a', 'b']}),
(
core_schema.enum_schema,
args(MyEnum, list(MyEnum.__members__.values())),
{'type': 'enum', 'cls': MyEnum, 'members': [MyEnum.a, MyEnum.b]},
),
(core_schema.is_instance_schema, args(int), {'type': 'is-instance', 'cls': int}),
(core_schema.callable_schema, args(), {'type': 'callable'}),
(core_schema.list_schema, args(), {'type': 'list'}),
(core_schema.list_schema, args({'type': 'int'}), {'type': 'list', 'items_schema': {'type': 'int'}}),
(core_schema.tuple_schema, args([]), {'type': 'tuple', 'items_schema': []}),
(
core_schema.set_schema,
args({'type': 'int'}, min_length=4),
{'type': 'set', 'items_schema': {'type': 'int'}, 'min_length': 4},
),
(
core_schema.frozenset_schema,
args({'type': 'int'}, max_length=5),
{'type': 'frozenset', 'items_schema': {'type': 'int'}, 'max_length': 5},
),
(core_schema.generator_schema, args({'type': 'int'}), {'type': 'generator', 'items_schema': {'type': 'int'}}),
(core_schema.dict_schema, args(), {'type': 'dict'}),
(
core_schema.dict_schema,
args({'type': 'str'}, {'type': 'int'}),
{'type': 'dict', 'keys_schema': {'type': 'str'}, 'values_schema': {'type': 'int'}},
),
(
core_schema.with_info_before_validator_function,
args(val_function, {'type': 'int'}),
{
'type': 'function-before',
'function': {'type': 'with-info', 'function': val_function},
'schema': {'type': 'int'},
},
),
(
core_schema.with_info_after_validator_function,
args(val_function, {'type': 'int'}),
{
'type': 'function-after',
'function': {'type': 'with-info', 'function': val_function},
'schema': {'type': 'int'},
},
),
(
core_schema.with_info_wrap_validator_function,
args(val_function, {'type': 'int'}),
{
'type': 'function-wrap',
'function': {'type': 'with-info', 'function': val_function},
'schema': {'type': 'int'},
},
),
(
core_schema.with_info_plain_validator_function,
args(val_function),
core_schema.with_info_plain_validator_function(val_function),
),
(
core_schema.with_default_schema,
args({'type': 'int'}, default=5),
{'type': 'default', 'schema': {'type': 'int'}, 'default': 5},
),
(
core_schema.with_default_schema,
args({'type': 'int'}, default=None),
{'type': 'default', 'schema': {'type': 'int'}, 'default': None},
),
(
core_schema.with_default_schema,
args({'type': 'int'}, default_factory=make_5),
{'type': 'default', 'schema': {'type': 'int'}, 'default_factory': make_5},
),
(core_schema.nullable_schema, args({'type': 'int'}), {'type': 'nullable', 'schema': {'type': 'int'}}),
(
core_schema.union_schema,
args([{'type': 'int'}, {'type': 'str'}]),
{'type': 'union', 'choices': [{'type': 'int'}, {'type': 'str'}]},
),
(
core_schema.union_schema,
args([{'type': 'int'}, {'type': 'str'}], custom_error_type='foobar', custom_error_message='This is Foobar'),
{
'type': 'union',
'choices': [{'type': 'int'}, {'type': 'str'}],
'custom_error_type': 'foobar',
'custom_error_message': 'This is Foobar',
},
),
(
core_schema.tagged_union_schema,
args({'foo': {'type': 'int'}, 'bar': {'type': 'str'}}, 'foo'),
{'type': 'tagged-union', 'choices': {'foo': {'type': 'int'}, 'bar': {'type': 'str'}}, 'discriminator': 'foo'},
),
(
core_schema.chain_schema,
args([{'type': 'int'}, {'type': 'str'}]),
{'type': 'chain', 'steps': [{'type': 'int'}, {'type': 'str'}]},
),
(
core_schema.typed_dict_field,
args({'type': 'int'}, required=True),
{'type': 'typed-dict-field', 'schema': {'type': 'int'}, 'required': True},
),
(
core_schema.typed_dict_schema,
args({'foo': core_schema.typed_dict_field({'type': 'int'})}),
{'type': 'typed-dict', 'fields': {'foo': {'type': 'typed-dict-field', 'schema': {'type': 'int'}}}},
),
(
core_schema.model_field,
args({'type': 'int'}, validation_alias='foobar'),
{'type': 'model-field', 'schema': {'type': 'int'}, 'validation_alias': 'foobar'},
),
(
core_schema.model_fields_schema,
args({'foo': core_schema.model_field({'type': 'int'})}),
{'type': 'model-fields', 'fields': {'foo': {'type': 'model-field', 'schema': {'type': 'int'}}}},
),
(
core_schema.model_schema,
args(MyModel, {'type': 'int'}),
{'type': 'model', 'cls': MyModel, 'schema': {'type': 'int'}},
),
(core_schema.arguments_parameter, args('foo', {'type': 'int'}), {'name': 'foo', 'schema': {'type': 'int'}}),
(
core_schema.arguments_schema,
args(
[
core_schema.arguments_parameter('foo', {'type': 'int'}),
core_schema.arguments_parameter('bar', {'type': 'str'}),
],
serialization=core_schema.format_ser_schema('d'),
),
{
'type': 'arguments',
'arguments_schema': [
{'name': 'foo', 'schema': {'type': 'int'}},
{'name': 'bar', 'schema': {'type': 'str'}},
],
'serialization': {'type': 'format', 'formatting_string': 'd'},
},
),
(
core_schema.call_schema,
args(core_schema.arguments_schema([core_schema.arguments_parameter('foo', {'type': 'int'})]), val_function),
{
'type': 'call',
'function': val_function,
'arguments_schema': {'type': 'arguments', 'arguments_schema': [{'name': 'foo', 'schema': {'type': 'int'}}]},
},
),
(
core_schema.custom_error_schema,
args(core_schema.int_schema(), 'foobar', custom_error_message='Hello'),
{
'type': 'custom-error',
'schema': {'type': 'int'},
'custom_error_type': 'foobar',
'custom_error_message': 'Hello',
},
),
(core_schema.json_schema, args({'type': 'int'}), {'type': 'json', 'schema': {'type': 'int'}}),
(core_schema.url_schema, args(), {'type': 'url'}),
(core_schema.multi_host_url_schema, args(), {'type': 'multi-host-url'}),
(
core_schema.lax_or_strict_schema,
args({'type': 'int'}, {'type': 'int'}),
{'type': 'lax-or-strict', 'lax_schema': {'type': 'int'}, 'strict_schema': {'type': 'int'}},
),
(
core_schema.json_or_python_schema,
args({'type': 'int'}, {'type': 'str'}),
{'type': 'json-or-python', 'json_schema': {'type': 'int'}, 'python_schema': {'type': 'str'}},
),
(core_schema.is_subclass_schema, args(MyModel), {'type': 'is-subclass', 'cls': MyModel}),
(
core_schema.definitions_schema,
args({'type': 'definition-ref', 'schema_ref': 'an-int'}, [{'type': 'int', 'ref': 'an-int'}]),
{
'type': 'definitions',
'schema': {'type': 'definition-ref', 'schema_ref': 'an-int'},
'definitions': [{'type': 'int', 'ref': 'an-int'}],
},
),
(core_schema.definition_reference_schema, args('foo'), {'type': 'definition-ref', 'schema_ref': 'foo'}),
(
core_schema.dataclass_args_schema,
args('Foo', [{'name': 'foo', 'type': 'dataclass-field', 'schema': {'type': 'int'}}]),
{
'type': 'dataclass-args',
'dataclass_name': 'Foo',
'fields': [{'name': 'foo', 'type': 'dataclass-field', 'schema': {'type': 'int'}}],
},
),
(
core_schema.dataclass_schema,
args(MyDataclass, {'type': 'int'}, ['foobar']),
{'type': 'dataclass', 'schema': {'type': 'int'}, 'fields': ['foobar'], 'cls': MyDataclass},
),
(
core_schema.dataclass_schema,
args(MyDataclass, {'type': 'int'}, ['foobar'], slots=True),
{'type': 'dataclass', 'schema': {'type': 'int'}, 'fields': ['foobar'], 'cls': MyDataclass, 'slots': True},
),
(core_schema.uuid_schema, args(), {'type': 'uuid'}),
(core_schema.decimal_schema, args(), {'type': 'decimal'}),
(core_schema.decimal_schema, args(multiple_of=5, gt=1.2), {'type': 'decimal', 'multiple_of': 5, 'gt': 1.2}),
(core_schema.complex_schema, args(), {'type': 'complex'}),
(core_schema.invalid_schema, args(), {'type': 'invalid'}),
]
@pytest.mark.parametrize('function,args_kwargs,expected_schema', all_schema_functions, ids=ids_function)
def test_schema_functions(function, args_kwargs, expected_schema):
args, kwargs = args_kwargs
schema = function(*args, **kwargs)
assert schema == expected_schema
if schema.get('type') in {None, 'definition-ref', 'typed-dict-field', 'model-field', 'invalid'}:
return
v = SchemaValidator(schema)
try:
v.validate_python('foobar')
except ValidationError:
pass
# also build the serializer, just to check it doesn't raise an error
SchemaSerializer(schema)
def test_all_schema_functions_used():
all_types = {
re.sub(r".+'(.+?)'.+", r'\1', s.__annotations__['type'].__forward_arg__)
for s in core_schema.CoreSchema.__args__
}
types_used = {args['type'] for _, _, args in all_schema_functions if 'type' in args}
# isn't a CoreSchema type
types_used.remove('typed-dict-field')
types_used.remove('model-field')
assert all_types == types_used
def test_invalid_custom_error():
s = core_schema.union_schema([{'type': 'int'}, {'type': 'str'}], custom_error_type='foobar')
with pytest.raises(SchemaError, match=r"KeyError: 'custom_error_message'"):
SchemaValidator(s)
def test_invalid_custom_error_type():
s = core_schema.union_schema(
[{'type': 'int'}, {'type': 'str'}], custom_error_type='finite_number', custom_error_message='x'
)
msg = "custom_error.message should not be provided if 'custom_error_type' matches a known error"
with pytest.raises(SchemaError, match=msg):
SchemaValidator(s)
def repr_function(value, _info):
return repr(value)
@pytest.mark.parametrize('return_schema', [core_schema.str_schema(), core_schema.int_schema()])
def test_expected_serialization_types(return_schema):
SchemaSerializer(
core_schema.any_schema(
serialization=core_schema.plain_serializer_function_ser_schema(
repr_function, info_arg=True, return_schema=return_schema
)
)
)
def test_err_on_invalid() -> None:
with pytest.raises(SchemaError, match='Cannot construct schema with `InvalidSchema` member.'):
SchemaValidator(core_schema.invalid_schema())