/
test_root_model.py
563 lines (401 loc) · 14.8 KB
/
test_root_model.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
import pickle
from typing import Any, Dict, List, Optional, Union
import pytest
from pydantic_core import CoreSchema
from pydantic_core.core_schema import SerializerFunctionWrapHandler
from typing_extensions import Annotated, Literal
from pydantic import (
Base64Str,
BaseModel,
ConfigDict,
Field,
PrivateAttr,
PydanticDeprecatedSince20,
PydanticUserError,
RootModel,
ValidationError,
field_serializer,
model_validator,
)
def parametrize_root_model():
class InnerModel(BaseModel):
int_field: int
str_field: str
return pytest.mark.parametrize(
('root_type', 'root_value', 'dump_value'),
[
pytest.param(int, 42, 42, id='int'),
pytest.param(str, 'forty two', 'forty two', id='str'),
pytest.param(Dict[int, bool], {1: True, 2: False}, {1: True, 2: False}, id='dict[int, bool]'),
pytest.param(List[int], [4, 2, -1], [4, 2, -1], id='list[int]'),
pytest.param(
InnerModel,
InnerModel(int_field=42, str_field='forty two'),
{'int_field': 42, 'str_field': 'forty two'},
id='InnerModel',
),
],
)
def check_schema(schema: CoreSchema) -> None:
# we assume the shape of the core schema here, which is not a guarantee
# pydantic makes to it's users but is useful to check here to make sure
# we are doing the right thing internally
assert schema['type'] == 'definitions'
inner = schema['schema']
assert inner['type'] == 'definition-ref'
ref = inner['schema_ref'] # type: ignore
schema = next(s for s in schema['definitions'] if s['ref'] == ref) # type: ignore
assert schema['type'] == 'model'
assert schema['root_model'] is True
assert schema['custom_init'] is False
@parametrize_root_model()
def test_root_model_specialized(root_type, root_value, dump_value):
Model = RootModel[root_type]
check_schema(Model.__pydantic_core_schema__)
m = Model(root_value)
assert m.model_dump() == dump_value
assert dict(m) == {'root': m.root}
assert m.__pydantic_fields_set__ == {'root'}
@parametrize_root_model()
def test_root_model_inherited(root_type, root_value, dump_value):
class Model(RootModel[root_type]):
pass
check_schema(Model.__pydantic_core_schema__)
m = Model(root_value)
assert m.model_dump() == dump_value
assert dict(m) == {'root': m.root}
assert m.__pydantic_fields_set__ == {'root'}
def test_root_model_validation_error():
Model = RootModel[int]
with pytest.raises(ValidationError) as e:
Model('forty two')
assert e.value.errors(include_url=False) == [
{
'input': 'forty two',
'loc': (),
'msg': 'Input should be a valid integer, unable to parse string as an ' 'integer',
'type': 'int_parsing',
},
]
def test_root_model_repr():
SpecializedRootModel = RootModel[int]
class SubRootModel(RootModel):
pass
class SpecializedSubRootModel(RootModel[int]):
pass
assert repr(SpecializedRootModel(1)) == 'RootModel[int](root=1)'
assert repr(SubRootModel(1)) == 'SubRootModel(root=1)'
assert repr(SpecializedSubRootModel(1)) == 'SpecializedSubRootModel(root=1)'
def test_root_model_recursive():
class A(RootModel[List['B']]):
def my_a_method(self):
pass
class B(RootModel[Dict[str, Optional[A]]]):
def my_b_method(self):
pass
assert repr(A.model_validate([{}])) == 'A(root=[B(root={})])'
def test_root_model_nested():
calls = []
class B(RootModel[int]):
def my_b_method(self):
calls.append(('my_b_method', self.root))
class A(RootModel[B]):
def my_a_method(self):
calls.append(('my_a_method', self.root.root))
m1 = A.model_validate(1)
m1.my_a_method()
m1.root.my_b_method()
assert calls == [('my_a_method', 1), ('my_b_method', 1)]
calls.clear()
m2 = A.model_validate_json('2')
m2.my_a_method()
m2.root.my_b_method()
assert calls == [('my_a_method', 2), ('my_b_method', 2)]
def test_root_model_as_field():
class MyRootModel(RootModel[int]):
pass
class MyModel(BaseModel):
root_model: MyRootModel
m = MyModel.model_validate({'root_model': 1})
assert isinstance(m.root_model, MyRootModel)
def test_v1_compatibility_serializer():
class MyInnerModel(BaseModel):
x: int
class MyRootModel(RootModel[MyInnerModel]):
# The following field_serializer can be added to achieve the same behavior as v1 had for .dict()
@field_serializer('root', mode='wrap')
def embed_in_dict(self, v: Any, handler: SerializerFunctionWrapHandler):
return {'__root__': handler(v)}
class MyOuterModel(BaseModel):
my_root: MyRootModel
m = MyOuterModel.model_validate({'my_root': {'x': 1}})
assert m.model_dump() == {'my_root': {'__root__': {'x': 1}}}
with pytest.warns(PydanticDeprecatedSince20):
assert m.dict() == {'my_root': {'__root__': {'x': 1}}}
def test_construct():
class Base64Root(RootModel[Base64Str]):
pass
v = Base64Root.model_construct('test')
assert v.model_dump() == 'dGVzdA==\n'
def test_construct_nested():
class Base64RootProperty(BaseModel):
data: RootModel[Base64Str]
v = Base64RootProperty.model_construct(data=RootModel[Base64Str].model_construct('test'))
assert v.model_dump() == {'data': 'dGVzdA==\n'}
# Note: model_construct requires the inputs to be valid; the root model value does not get "validated" into
# an actual root model instance:
v = Base64RootProperty.model_construct(data='test')
assert isinstance(v.data, str) # should be RootModel[Base64Str], but model_construct skipped validation
with pytest.raises(AttributeError, match="'str' object has no attribute 'root'"):
v.model_dump()
def test_assignment():
Model = RootModel[int]
m = Model(1)
assert m.model_fields_set == {'root'}
assert m.root == 1
m.root = 2
assert m.root == 2
def test_model_validator_before():
class Model(RootModel[int]):
@model_validator(mode='before')
def words(cls, v):
if v == 'one':
return 1
elif v == 'two':
return 2
else:
return v
assert Model('one').root == 1
assert Model('two').root == 2
assert Model('3').root == 3
with pytest.raises(ValidationError) as exc_info:
Model('three')
# insert_assert(exc_info.value.errors())
assert exc_info.value.errors(include_url=False) == [
{
'type': 'int_parsing',
'loc': (),
'msg': 'Input should be a valid integer, unable to parse string as an integer',
'input': 'three',
}
]
def test_model_validator_after():
class Model(RootModel[int]):
@model_validator(mode='after')
def double(self) -> 'Model':
self.root *= 2
return self
assert Model('1').root == 2
assert Model('21').root == 42
def test_private_attr():
class Model(RootModel[int]):
_private_attr: str
_private_attr_default: str = PrivateAttr(default='abc')
m = Model(42)
assert m.root == 42
assert m._private_attr_default == 'abc'
with pytest.raises(AttributeError, match='_private_attr'):
m._private_attr
m._private_attr = 7
m._private_attr_default = 8
m._other_private_attr = 9
# TODO: Should this be an `AttributeError`?
with pytest.raises(ValueError, match='other_attr'):
m.other_attr = 10
assert m._private_attr == 7
assert m._private_attr_default == 8
assert m._other_private_attr == 9
assert m.model_dump() == 42
def test_validate_assignment_false():
Model = RootModel[int]
m = Model(42)
m.root = 'abc'
assert m.root == 'abc'
def test_validate_assignment_true():
class Model(RootModel[int]):
model_config = ConfigDict(validate_assignment=True)
m = Model(42)
with pytest.raises(ValidationError) as e:
m.root = 'abc'
assert e.value.errors(include_url=False) == [
{
'input': 'abc',
'loc': (),
'msg': 'Input should be a valid integer, unable to parse string as an integer',
'type': 'int_parsing',
}
]
def test_root_model_literal():
assert RootModel[int](42).root == 42
def test_root_model_equality():
assert RootModel[int](42) == RootModel[int](42)
assert RootModel[int](42) != RootModel[int](7)
assert RootModel[int](42) != RootModel[float](42)
assert RootModel[int](42) == RootModel[int].model_construct(42)
def test_root_model_with_private_attrs_equality():
class Model(RootModel[int]):
_private_attr: str = PrivateAttr(default='abc')
m = Model(42)
assert m == Model(42)
m._private_attr = 'xyz'
assert m != Model(42)
def test_root_model_nested_equality():
class Model(BaseModel):
value: RootModel[int]
assert Model(value=42).value == RootModel[int](42)
def test_root_model_base_model_equality():
class R(RootModel[int]):
pass
class B(BaseModel):
root: int
assert R(42) != B(root=42)
assert B(root=42) != R(42)
@pytest.mark.xfail(reason='TODO: raise error for `extra` with `RootModel`')
@pytest.mark.parametrize('extra_value', ['ignore', 'allow', 'forbid'])
def test_extra_error(extra_value):
with pytest.raises(PydanticUserError, match='extra'):
class Model(RootModel[int]):
model_config = ConfigDict(extra=extra_value)
def test_root_model_default_value():
class Model(RootModel):
root: int = 42
m = Model()
assert m.root == 42
assert m.model_dump() == 42
assert m.__pydantic_fields_set__ == set()
def test_root_model_default_factory():
class Model(RootModel):
root: int = Field(default_factory=lambda: 42)
m = Model()
assert m.root == 42
assert m.model_dump() == 42
assert m.__pydantic_fields_set__ == set()
def test_root_model_wrong_default_value_without_validate_default():
class Model(RootModel):
root: int = '42'
assert Model().root == '42'
def test_root_model_default_value_with_validate_default():
class Model(RootModel):
model_config = ConfigDict(validate_default=True)
root: int = '42'
m = Model()
assert m.root == 42
assert m.model_dump() == 42
assert m.__pydantic_fields_set__ == set()
def test_root_model_default_value_with_validate_default_on_field():
class Model(RootModel):
root: Annotated[int, Field(validate_default=True, default='42')]
m = Model()
assert m.root == 42
assert m.model_dump() == 42
assert m.__pydantic_fields_set__ == set()
def test_root_model_as_attr_with_validate_default():
class Model(BaseModel):
model_config = ConfigDict(validate_default=True)
rooted_value: RootModel[int] = 42
m = Model()
assert m.rooted_value == RootModel[int](42)
assert m.model_dump() == {'rooted_value': 42}
assert m.rooted_value.__pydantic_fields_set__ == {'root'}
def test_root_model_in_root_model_default():
class Nested(RootModel):
root: int = 42
class Model(RootModel):
root: Nested = Nested()
m = Model()
assert m.root.root == 42
assert m.__pydantic_fields_set__ == set()
assert m.root.__pydantic_fields_set__ == set()
def test_nested_root_model_naive_default():
class Nested(RootModel):
root: int = 42
class Model(BaseModel):
value: Nested
m = Model(value=Nested())
assert m.value.root == 42
assert m.value.__pydantic_fields_set__ == set()
def test_nested_root_model_proper_default():
class Nested(RootModel):
root: int = 42
class Model(BaseModel):
value: Nested = Field(default_factory=Nested)
m = Model()
assert m.value.root == 42
assert m.value.__pydantic_fields_set__ == set()
def test_root_model_json_schema_meta():
ParametrizedModel = RootModel[int]
class SubclassedModel(RootModel):
"""Subclassed Model docstring"""
root: int
parametrized_json_schema = ParametrizedModel.model_json_schema()
subclassed_json_schema = SubclassedModel.model_json_schema()
assert parametrized_json_schema.get('title') == 'RootModel[int]'
assert parametrized_json_schema.get('description') is None
assert subclassed_json_schema.get('title') == 'SubclassedModel'
assert subclassed_json_schema.get('description') == 'Subclassed Model docstring'
@pytest.mark.parametrize('order', ['BR', 'RB'])
def test_root_model_dump_with_base_model(order):
class BModel(BaseModel):
value: str
class RModel(RootModel):
root: int
if order == 'BR':
class Model(RootModel):
root: List[Union[BModel, RModel]]
elif order == 'RB':
class Model(RootModel):
root: List[Union[RModel, BModel]]
m = Model([1, 2, {'value': 'abc'}])
assert m.root == [RModel(1), RModel(2), BModel.model_construct(value='abc')]
assert m.model_dump() == [1, 2, {'value': 'abc'}]
assert m.model_dump_json() == '[1,2,{"value":"abc"}]'
@pytest.mark.parametrize(
'data',
[
pytest.param({'kind': 'IModel', 'int_value': 42}, id='IModel'),
pytest.param({'kind': 'SModel', 'str_value': 'abc'}, id='SModel'),
],
)
def test_mixed_discriminated_union(data):
class IModel(BaseModel):
kind: Literal['IModel']
int_value: int
class RModel(RootModel):
root: IModel
class SModel(BaseModel):
kind: Literal['SModel']
str_value: str
class Model(RootModel):
root: Union[SModel, RModel] = Field(discriminator='kind')
assert Model(data).model_dump() == data
assert Model(**data).model_dump() == data
def test_list_rootmodel():
class A(BaseModel):
type: Literal['a']
a: str
class B(BaseModel):
type: Literal['b']
b: str
class D(RootModel[Annotated[Union[A, B], Field(discriminator='type')]]):
pass
LD = RootModel[List[D]]
obj = LD.model_validate([{'type': 'a', 'a': 'a'}, {'type': 'b', 'b': 'b'}])
assert obj.model_dump() == [{'type': 'a', 'a': 'a'}, {'type': 'b', 'b': 'b'}]
def test_root_and_data_error():
class BModel(BaseModel):
value: int
other_value: str
Model = RootModel[BModel]
with pytest.raises(
ValueError,
match='"RootModel.__init__" accepts either a single positional argument or arbitrary keyword arguments',
):
Model({'value': 42}, other_value='abc')
def test_pickle_root_model(create_module):
@create_module
def module():
from pydantic import RootModel
class MyRootModel(RootModel[str]):
pass
MyRootModel = module.MyRootModel
assert MyRootModel(root='abc') == pickle.loads(pickle.dumps(MyRootModel(root='abc')))