Skip to content

Commit 493643e

Browse files
authored
Remove warning for mutable default (#594)
1 parent b396f93 commit 493643e

File tree

2 files changed

+41
-55
lines changed

2 files changed

+41
-55
lines changed

src/validators/with_default.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,6 @@ impl BuildValidator for WithDefaultValidator {
9595
} else {
9696
false
9797
};
98-
if copy_default {
99-
let message = format!("`{}` has a mutable default value that will be deep copied during validation. Consider using `default_factory` instead for finer control.", validator.get_name());
100-
let user_warning_type = py.import("builtins")?.getattr("UserWarning")?;
101-
PyErr::warn(py, user_warning_type, &message, 0)?;
102-
}
10398

10499
let name = format!("{}[{}]", Self::EXPECTED_TYPE, validator.get_name());
105100

tests/validators/test_with_default.py

Lines changed: 41 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,7 @@ def test_typed_dict_error():
239239

240240

241241
def test_on_error_default_not_int():
242-
with pytest.warns(UserWarning):
243-
v = SchemaValidator({'type': 'default', 'schema': {'type': 'int'}, 'default': [1, 2, 3], 'on_error': 'default'})
242+
v = SchemaValidator({'type': 'default', 'schema': {'type': 'int'}, 'default': [1, 2, 3], 'on_error': 'default'})
244243
assert v.validate_python(42) == 42
245244
assert v.validate_python('42') == 42
246245
assert v.validate_python('wrong') == [1, 2, 3]
@@ -278,25 +277,24 @@ class MyModel:
278277
field_a: str
279278
field_b: int
280279

281-
with pytest.warns(UserWarning):
282-
v = SchemaValidator(
283-
{
284-
'type': 'model',
285-
'cls': MyModel,
280+
v = SchemaValidator(
281+
{
282+
'type': 'model',
283+
'cls': MyModel,
284+
'schema': {
285+
'type': 'default',
286286
'schema': {
287-
'type': 'default',
288-
'schema': {
289-
'type': 'model-fields',
290-
'fields': {
291-
'field_a': {'type': 'model-field', 'schema': {'type': 'str'}},
292-
'field_b': {'type': 'model-field', 'schema': {'type': 'int'}},
293-
},
287+
'type': 'model-fields',
288+
'fields': {
289+
'field_a': {'type': 'model-field', 'schema': {'type': 'str'}},
290+
'field_b': {'type': 'model-field', 'schema': {'type': 'int'}},
294291
},
295-
'default': ({'field_a': '[default-a]', 'field_b': '[default-b]'}, None, set()),
296-
'on_error': 'default',
297292
},
298-
}
299-
)
293+
'default': ({'field_a': '[default-a]', 'field_b': '[default-b]'}, None, set()),
294+
'on_error': 'default',
295+
},
296+
}
297+
)
300298
m = v.validate_python({'field_a': 'test', 'field_b': 12})
301299
assert isinstance(m, MyModel)
302300
assert m.field_a == 'test'
@@ -388,44 +386,37 @@ class Model:
388386
int_list_with_default: List[int] = stored_empty_list
389387
str_dict_with_default: Dict[str, str] = stored_empty_dict
390388

391-
with pytest.warns(
392-
UserWarning,
393-
match=(
394-
r'`[Ll]ist\[int\]` has a mutable default value that will be deep copied during validation.'
395-
r' Consider using `default_factory` instead for finer control.'
396-
),
397-
):
398-
v = SchemaValidator(
399-
{
400-
'type': 'model',
401-
'cls': Model,
402-
'schema': {
403-
'type': 'model-fields',
404-
'fields': {
405-
'int_list_with_default': {
406-
'type': 'model-field',
407-
'schema': {
408-
'type': 'default',
409-
'schema': {'type': 'list', 'items_schema': {'type': 'int'}},
410-
'default': stored_empty_list,
411-
},
389+
v = SchemaValidator(
390+
{
391+
'type': 'model',
392+
'cls': Model,
393+
'schema': {
394+
'type': 'model-fields',
395+
'fields': {
396+
'int_list_with_default': {
397+
'type': 'model-field',
398+
'schema': {
399+
'type': 'default',
400+
'schema': {'type': 'list', 'items_schema': {'type': 'int'}},
401+
'default': stored_empty_list,
412402
},
413-
'str_dict_with_default': {
414-
'type': 'model-field',
403+
},
404+
'str_dict_with_default': {
405+
'type': 'model-field',
406+
'schema': {
407+
'type': 'default',
415408
'schema': {
416-
'type': 'default',
417-
'schema': {
418-
'type': 'dict',
419-
'keys_schema': {'type': 'str'},
420-
'values_schema': {'type': 'str'},
421-
},
422-
'default': stored_empty_dict,
409+
'type': 'dict',
410+
'keys_schema': {'type': 'str'},
411+
'values_schema': {'type': 'str'},
423412
},
413+
'default': stored_empty_dict,
424414
},
425415
},
426416
},
427-
}
428-
)
417+
},
418+
}
419+
)
429420

430421
m1 = v.validate_python({})
431422

0 commit comments

Comments
 (0)