-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
test_config.py
663 lines (500 loc) · 21.8 KB
/
test_config.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
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
import re
import sys
from contextlib import nullcontext as does_not_raise
from decimal import Decimal
from inspect import signature
from typing import Any, ContextManager, Iterable, NamedTuple, Type, Union, get_type_hints
from dirty_equals import HasRepr, IsPartialDict
from pydantic_core import SchemaError
from pydantic import (
BaseConfig,
BaseModel,
Field,
PrivateAttr,
PydanticDeprecatedSince20,
PydanticSchemaGenerationError,
ValidationError,
create_model,
validate_call,
)
from pydantic._internal._config import ConfigWrapper, config_defaults
from pydantic.config import ConfigDict
from pydantic.dataclasses import dataclass as pydantic_dataclass
from pydantic.errors import PydanticUserError
from pydantic.v1.schema import get_model_name_map
if sys.version_info < (3, 9):
from typing_extensions import Annotated
else:
from typing import Annotated
import pytest
@pytest.fixture(scope='session', name='BaseConfigModelWithStrictConfig')
def model_with_strict_config():
class ModelWithStrictConfig(BaseModel):
a: int
# strict=False overrides the Config
b: Annotated[int, Field(strict=False)]
# strict=None or not including it is equivalent
# lets this field be overridden by the Config
c: Annotated[int, Field(strict=None)]
d: Annotated[int, Field()]
model_config = ConfigDict(strict=True)
return ModelWithStrictConfig
def _equals(a: Union[str, Iterable[str]], b: Union[str, Iterable[str]]) -> bool:
"""
Compare strings with spaces removed
"""
if isinstance(a, str) and isinstance(b, str):
return a.replace(' ', '') == b.replace(' ', '')
elif isinstance(a, Iterable) and isinstance(b, Iterable):
return all(_equals(a_, b_) for a_, b_ in zip(a, b))
else:
raise TypeError(f'arguments must be both strings or both lists, not {type(a)}, {type(b)}')
def test_config_dict_missing_keys():
assert ConfigDict().get('missing_property') is None
with pytest.raises(KeyError, match="'missing_property'"):
ConfigDict()['missing_property']
class TestsBaseConfig:
@pytest.mark.filterwarnings('ignore:.* is deprecated.*:DeprecationWarning')
def test_base_config_equality_defaults_of_config_dict_class(self):
for key, value in config_defaults.items():
assert getattr(BaseConfig, key) == value
def test_config_and_module_config_cannot_be_used_together(self):
with pytest.raises(PydanticUserError):
class MyModel(BaseModel):
model_config = ConfigDict(title='MyTitle')
class Config:
title = 'MyTitleConfig'
@pytest.mark.filterwarnings('ignore:.* is deprecated.*:DeprecationWarning')
def test_base_config_properly_converted_to_dict(self):
class MyConfig(BaseConfig):
title = 'MyTitle'
frozen = True
class MyBaseModel(BaseModel):
class Config(MyConfig):
...
class MyModel(MyBaseModel):
...
MyModel.model_config['title'] = 'MyTitle'
MyModel.model_config['frozen'] = True
assert 'str_to_lower' not in MyModel.model_config
def test_base_config_custom_init_signature(self):
class MyModel(BaseModel):
id: int
name: str = 'John Doe'
f__: str = Field(..., alias='foo')
model_config = ConfigDict(extra='allow')
def __init__(self, id: int = 1, bar=2, *, baz: Any, **data):
super().__init__(id=id, **data)
self.bar = bar
self.baz = baz
sig = signature(MyModel)
assert _equals(
map(str, sig.parameters.values()),
('id: int = 1', 'bar=2', 'baz: Any', "name: str = 'John Doe'", 'foo: str', '**data'),
)
assert _equals(str(sig), "(id: int = 1, bar=2, *, baz: Any, name: str = 'John Doe', foo: str, **data) -> None")
def test_base_config_custom_init_signature_with_no_var_kw(self):
class Model(BaseModel):
a: float
b: int = 2
c: int
def __init__(self, a: float, b: int):
super().__init__(a=a, b=b, c=1)
model_config = ConfigDict(extra='allow')
assert _equals(str(signature(Model)), '(a: float, b: int) -> None')
def test_base_config_use_field_name(self):
class Foo(BaseModel):
foo: str = Field(..., alias='this is invalid')
model_config = ConfigDict(populate_by_name=True)
assert _equals(str(signature(Foo)), '(*, foo: str) -> None')
def test_base_config_does_not_use_reserved_word(self):
class Foo(BaseModel):
from_: str = Field(..., alias='from')
model_config = ConfigDict(populate_by_name=True)
assert _equals(str(signature(Foo)), '(*, from_: str) -> None')
def test_base_config_extra_allow_no_conflict(self):
class Model(BaseModel):
spam: str
model_config = ConfigDict(extra='allow')
assert _equals(str(signature(Model)), '(*, spam: str, **extra_data: Any) -> None')
def test_base_config_extra_allow_conflict_twice(self):
class Model(BaseModel):
extra_data: str
extra_data_: str
model_config = ConfigDict(extra='allow')
assert _equals(str(signature(Model)), '(*, extra_data: str, extra_data_: str, **extra_data__: Any) -> None')
def test_base_config_extra_allow_conflict_custom_signature(self):
class Model(BaseModel):
extra_data: int
def __init__(self, extra_data: int = 1, **foobar: Any):
super().__init__(extra_data=extra_data, **foobar)
model_config = ConfigDict(extra='allow')
assert _equals(str(signature(Model)), '(extra_data: int = 1, **foobar: Any) -> None')
def test_base_config_private_attribute_intersection_with_extra_field(self):
class Model(BaseModel):
_foo = PrivateAttr('private_attribute')
model_config = ConfigDict(extra='allow')
assert set(Model.__private_attributes__) == {'_foo'}
m = Model(_foo='field')
assert m._foo == 'private_attribute'
assert m.__dict__ == {}
assert m.__pydantic_extra__ == {'_foo': 'field'}
assert m.model_dump() == {'_foo': 'field'}
m._foo = 'still_private'
assert m._foo == 'still_private'
assert m.__dict__ == {}
assert m.__pydantic_extra__ == {'_foo': 'field'}
assert m.model_dump() == {'_foo': 'field'}
def test_base_config_parse_model_with_strict_config_disabled(
self, BaseConfigModelWithStrictConfig: Type[BaseModel]
) -> None:
class Model(BaseConfigModelWithStrictConfig):
model_config = ConfigDict(strict=False)
values = [
Model(a='1', b=2, c=3, d=4),
Model(a=1, b=2, c='3', d=4),
Model(a=1, b=2, c=3, d='4'),
Model(a=1, b='2', c=3, d=4),
Model(a=1, b=2, c=3, d=4),
]
assert all(v.model_dump() == {'a': 1, 'b': 2, 'c': 3, 'd': 4} for v in values)
def test_finite_float_config(self):
class Model(BaseModel):
a: float
model_config = ConfigDict(allow_inf_nan=False)
assert Model(a=42).a == 42
with pytest.raises(ValidationError) as exc_info:
Model(a=float('nan'))
# insert_assert(exc_info.value.errors(include_url=False))
assert exc_info.value.errors(include_url=False) == [
{
'type': 'finite_number',
'loc': ('a',),
'msg': 'Input should be a finite number',
'input': HasRepr('nan'),
}
]
@pytest.mark.parametrize(
'enabled,str_check,result_str_check',
[
(True, ' 123 ', '123'),
(True, ' 123\t\n', '123'),
(False, ' 123 ', ' 123 '),
],
)
def test_str_strip_whitespace(self, enabled, str_check, result_str_check):
class Model(BaseModel):
str_check: str
model_config = ConfigDict(str_strip_whitespace=enabled)
m = Model(str_check=str_check)
assert m.str_check == result_str_check
@pytest.mark.parametrize(
'enabled,str_check,result_str_check',
[(True, 'ABCDefG', 'ABCDEFG'), (False, 'ABCDefG', 'ABCDefG')],
)
def test_str_to_upper(self, enabled, str_check, result_str_check):
class Model(BaseModel):
str_check: str
model_config = ConfigDict(str_to_upper=enabled)
m = Model(str_check=str_check)
assert m.str_check == result_str_check
@pytest.mark.parametrize(
'enabled,str_check,result_str_check',
[(True, 'ABCDefG', 'abcdefg'), (False, 'ABCDefG', 'ABCDefG')],
)
def test_str_to_lower(self, enabled, str_check, result_str_check):
class Model(BaseModel):
str_check: str
model_config = ConfigDict(str_to_lower=enabled)
m = Model(str_check=str_check)
assert m.str_check == result_str_check
def test_namedtuple_arbitrary_type(self):
class CustomClass:
pass
class Tup(NamedTuple):
c: CustomClass
class Model(BaseModel):
x: Tup
model_config = ConfigDict(arbitrary_types_allowed=True)
data = {'x': Tup(c=CustomClass())}
model = Model.model_validate(data)
assert isinstance(model.x.c, CustomClass)
with pytest.raises(PydanticSchemaGenerationError):
class ModelNoArbitraryTypes(BaseModel):
x: Tup
@pytest.mark.parametrize(
'use_construct, populate_by_name_config, arg_name, expectation',
[
[False, True, 'bar', does_not_raise()],
[False, True, 'bar_', does_not_raise()],
[False, False, 'bar', does_not_raise()],
[False, False, 'bar_', pytest.raises(ValueError)],
[True, True, 'bar', does_not_raise()],
[True, True, 'bar_', does_not_raise()],
[True, False, 'bar', does_not_raise()],
[True, False, 'bar_', does_not_raise()],
],
)
def test_populate_by_name_config(
self,
use_construct: bool,
populate_by_name_config: bool,
arg_name: str,
expectation: ContextManager,
):
expected_value: int = 7
class Foo(BaseModel):
bar_: int = Field(..., alias='bar')
model_config = dict(populate_by_name=populate_by_name_config)
with expectation:
if use_construct:
f = Foo.model_construct(**{arg_name: expected_value})
else:
f = Foo(**{arg_name: expected_value})
assert f.bar_ == expected_value
def test_immutable_copy_with_frozen(self):
class Model(BaseModel):
a: int
b: int
model_config = ConfigDict(frozen=True)
m = Model(a=40, b=10)
assert m == m.model_copy()
def test_config_class_is_deprecated(self):
with pytest.warns(
PydanticDeprecatedSince20, match='Support for class-based `config` is deprecated, use ConfigDict instead.'
):
class Config(BaseConfig):
pass
def test_config_class_attributes_are_deprecated(self):
with pytest.warns(
PydanticDeprecatedSince20,
match='Support for class-based `config` is deprecated, use ConfigDict instead.',
):
assert BaseConfig.validate_assignment is False
with pytest.warns(
PydanticDeprecatedSince20,
match='Support for class-based `config` is deprecated, use ConfigDict instead.',
):
assert BaseConfig().validate_assignment is False
with pytest.warns(
PydanticDeprecatedSince20,
match='Support for class-based `config` is deprecated, use ConfigDict instead.',
):
class Config(BaseConfig):
pass
with pytest.warns(
PydanticDeprecatedSince20,
match='Support for class-based `config` is deprecated, use ConfigDict instead.',
):
assert Config.validate_assignment is False
with pytest.warns(
PydanticDeprecatedSince20,
match='Support for class-based `config` is deprecated, use ConfigDict instead.',
):
assert Config().validate_assignment is False
@pytest.mark.filterwarnings('ignore:.* is deprecated.*:DeprecationWarning')
def test_config_class_missing_attributes(self):
with pytest.raises(AttributeError, match="type object 'BaseConfig' has no attribute 'missing_attribute'"):
BaseConfig.missing_attribute
with pytest.raises(AttributeError, match="'BaseConfig' object has no attribute 'missing_attribute'"):
BaseConfig().missing_attribute
class Config(BaseConfig):
pass
with pytest.raises(AttributeError, match="type object 'Config' has no attribute 'missing_attribute'"):
Config.missing_attribute
with pytest.raises(AttributeError, match="'Config' object has no attribute 'missing_attribute'"):
Config().missing_attribute
def test_config_key_deprecation():
config_dict = {
'allow_mutation': None,
'error_msg_templates': None,
'fields': None,
'getter_dict': None,
'schema_extra': None,
'smart_union': None,
'underscore_attrs_are_private': None,
'allow_population_by_field_name': None,
'anystr_lower': None,
'anystr_strip_whitespace': None,
'anystr_upper': None,
'keep_untouched': None,
'max_anystr_length': None,
'min_anystr_length': None,
'orm_mode': None,
'validate_all': None,
}
warning_message = """
Valid config keys have changed in V2:
* 'allow_population_by_field_name' has been renamed to 'populate_by_name'
* 'anystr_lower' has been renamed to 'str_to_lower'
* 'anystr_strip_whitespace' has been renamed to 'str_strip_whitespace'
* 'anystr_upper' has been renamed to 'str_to_upper'
* 'keep_untouched' has been renamed to 'ignored_types'
* 'max_anystr_length' has been renamed to 'str_max_length'
* 'min_anystr_length' has been renamed to 'str_min_length'
* 'orm_mode' has been renamed to 'from_attributes'
* 'schema_extra' has been renamed to 'json_schema_extra'
* 'validate_all' has been renamed to 'validate_default'
* 'allow_mutation' has been removed
* 'error_msg_templates' has been removed
* 'fields' has been removed
* 'getter_dict' has been removed
* 'smart_union' has been removed
* 'underscore_attrs_are_private' has been removed
""".strip()
with pytest.warns(UserWarning, match=re.escape(warning_message)):
class MyModel(BaseModel):
model_config = config_dict
with pytest.warns(UserWarning, match=re.escape(warning_message)):
create_model('MyCreatedModel', __config__=config_dict)
with pytest.warns(UserWarning, match=re.escape(warning_message)):
@pydantic_dataclass(config=config_dict)
class MyDataclass:
pass
with pytest.warns(UserWarning, match=re.escape(warning_message)):
@validate_call(config=config_dict)
def my_function():
pass
def test_invalid_extra():
extra_error = re.escape(
"Input should be 'allow', 'forbid' or 'ignore'"
" [type=literal_error, input_value='invalid-value', input_type=str]"
)
config_dict = {'extra': 'invalid-value'}
with pytest.raises(SchemaError, match=extra_error):
class MyModel(BaseModel):
model_config = config_dict
with pytest.raises(SchemaError, match=extra_error):
create_model('MyCreatedModel', __config__=config_dict)
with pytest.raises(SchemaError, match=extra_error):
@pydantic_dataclass(config=config_dict)
class MyDataclass:
pass
def test_invalid_config_keys():
@validate_call(config={'alias_generator': lambda x: x})
def my_function():
pass
def test_config_model_name() -> None:
CLIENT_USER_MODEL_NAME = "ClientUser"
BUSINESS_USER_MODEL_NAME = "BusinessUser"
def _get_business_user_class():
class User(BaseModel):
model_config = ConfigDict(model_name=BUSINESS_USER_MODEL_NAME)
return User
def _get_client_user_class():
class User(BaseModel):
model_config = ConfigDict(model_name=CLIENT_USER_MODEL_NAME)
return User
BusinessUser = _get_business_user_class()
ClientUser = _get_client_user_class()
name_map = get_model_name_map({BusinessUser, ClientUser})
assert name_map[BusinessUser] == BUSINESS_USER_MODEL_NAME
assert name_map[ClientUser] == CLIENT_USER_MODEL_NAME
assert BusinessUser().model_json_schema()["title"] == BUSINESS_USER_MODEL_NAME
assert ClientUser().model_json_schema()["title"] == CLIENT_USER_MODEL_NAME
def test_multiple_inheritance_config():
class Parent(BaseModel):
model_config = ConfigDict(frozen=True, extra='forbid')
class Mixin(BaseModel):
model_config = ConfigDict(use_enum_values=True)
class Child(Mixin, Parent):
model_config = ConfigDict(populate_by_name=True)
assert BaseModel.model_config.get('frozen') is None
assert BaseModel.model_config.get('populate_by_name') is None
assert BaseModel.model_config.get('extra') is None
assert BaseModel.model_config.get('use_enum_values') is None
assert Parent.model_config.get('frozen') is True
assert Parent.model_config.get('populate_by_name') is None
assert Parent.model_config.get('extra') == 'forbid'
assert Parent.model_config.get('use_enum_values') is None
assert Mixin.model_config.get('frozen') is None
assert Mixin.model_config.get('populate_by_name') is None
assert Mixin.model_config.get('extra') is None
assert Mixin.model_config.get('use_enum_values') is True
assert Child.model_config.get('frozen') is True
assert Child.model_config.get('populate_by_name') is True
assert Child.model_config.get('extra') == 'forbid'
assert Child.model_config.get('use_enum_values') is True
@pytest.mark.skipif(sys.version_info < (3, 10), reason='different on older versions')
def test_config_wrapper_match():
config_dict_annotations = [(k, str(v)) for k, v in get_type_hints(ConfigDict).items()]
# remove config
config_wrapper_annotations = [(k, str(v)) for k, v in get_type_hints(ConfigWrapper).items() if k != 'config_dict']
assert (
config_dict_annotations == config_wrapper_annotations
), 'ConfigDict and ConfigWrapper must have the same annotations (except ConfigWrapper.config_dict)'
@pytest.mark.skipif(sys.version_info < (3, 10), reason='different on older versions')
def test_config_defaults_match():
config_dict_keys = list(get_type_hints(ConfigDict).keys())
config_defaults_keys = list(config_defaults.keys())
assert config_dict_keys == config_defaults_keys, 'ConfigDict and config_defaults must have the same keys'
def test_config_is_not_inherited_in_model_fields():
from typing import List
from pydantic import BaseModel, ConfigDict
class Inner(BaseModel):
a: str
class Outer(BaseModel):
# this cause the inner model incorrectly dumpped:
model_config = ConfigDict(str_to_lower=True)
x: List[str] # should be converted to lower
inner: Inner # should not have fields converted to lower
m = Outer.model_validate(dict(x=['Abc'], inner=dict(a='Def')))
assert m.model_dump() == {'x': ['abc'], 'inner': {'a': 'Def'}}
@pytest.mark.parametrize(
'config,input_str',
(
({}, 'type=string_type, input_value=123, input_type=int'),
({'hide_input_in_errors': False}, 'type=string_type, input_value=123, input_type=int'),
({'hide_input_in_errors': True}, 'type=string_type'),
),
)
def test_hide_input_in_errors(config, input_str):
class Model(BaseModel):
x: str
model_config = ConfigDict(**config)
with pytest.raises(ValidationError, match=re.escape(f'Input should be a valid string [{input_str}]')):
Model(x=123)
parametrize_inf_nan_capable_type = pytest.mark.parametrize('inf_nan_capable_type', [float, Decimal])
parametrize_inf_nan_capable_value = pytest.mark.parametrize('inf_nan_value', ['Inf', 'NaN'])
@parametrize_inf_nan_capable_value
@parametrize_inf_nan_capable_type
def test_config_inf_nan_enabled(inf_nan_capable_type, inf_nan_value):
class Model(BaseModel):
model_config = ConfigDict(allow_inf_nan=True)
value: inf_nan_capable_type
assert Model(value=inf_nan_capable_type(inf_nan_value))
@parametrize_inf_nan_capable_value
@parametrize_inf_nan_capable_type
def test_config_inf_nan_disabled(inf_nan_capable_type, inf_nan_value):
class Model(BaseModel):
model_config = ConfigDict(allow_inf_nan=False)
value: inf_nan_capable_type
with pytest.raises(ValidationError) as e:
Model(value=inf_nan_capable_type(inf_nan_value))
assert e.value.errors(include_url=False)[0] == IsPartialDict(
{
'loc': ('value',),
'msg': 'Input should be a finite number',
'type': 'finite_number',
}
)
@pytest.mark.parametrize(
'config,expected',
(
(ConfigDict(), 'ConfigWrapper()'),
(ConfigDict(title='test'), "ConfigWrapper(title='test')"),
),
)
def test_config_wrapper_repr(config, expected):
assert repr(ConfigWrapper(config=config)) == expected
def test_config_wrapper_get_item():
config_wrapper = ConfigWrapper(config=ConfigDict(title='test'))
assert config_wrapper.title == 'test'
with pytest.raises(AttributeError, match="Config has no attribute 'test'"):
config_wrapper.test
def test_config_inheritance_with_annotations():
class Parent(BaseModel):
model_config: ConfigDict = {'extra': 'allow'}
class Child(Parent):
model_config: ConfigDict = {'str_to_lower': True}
assert Child.model_config == {'extra': 'allow', 'str_to_lower': True}