8000 Remove warning for mutable default by dmontagu · Pull Request #594 · pydantic/pydantic-core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Remove warning for mutable default #594

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions src/validators/with_default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,6 @@ impl BuildValidator for WithDefaultValidator {
} else {
false
};
if copy_default {
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());
let user_warning_type = py.import("builtins")?.getattr("UserWarning")?;
PyErr::warn(py, user_warning_type, &message, 0)?;
}

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

Expand Down
91 changes: 41 additions & 50 deletions tests/validators/test_with_default.py
FD81
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ def test_typed_dict_error():


def test_on_error_default_not_int():
with pytest.warns(UserWarning):
v = SchemaValidator({'type': 'default', 'schema': {'type': 'int'}, 'default': [1, 2, 3], 'on_error': 'default'})
v = SchemaValidator({'type': 'default', 'schema': {'type': 'int'}, 'default': [1, 2, 3], 'on_error': 'default'})
assert v.validate_python(42) == 42
assert v.validate_python('42') == 42
assert v.validate_python('wrong') == [1, 2, 3]
Expand Down Expand Up @@ -278,25 +277,24 @@ class MyModel:
field_a: str
field_b: int

with pytest.warns(UserWarning):
v = SchemaValidator(
{
'type': 'model',
'cls': MyModel,
v = SchemaValidator(
{
'type': 'model',
'cls': MyModel,
'schema': {
'type': 'default',
'schema': {
'type': 'default',
'schema': {
'type': 'model-fields',
'fields': {
'field_a': {'type': 'model-field', 'schema': {'type': 'str'}},
'field_b': {'type': 'model-field', 'schema': {'type': 'int'}},
},
'type': 'model-fields',
'fields': {
'field_a': {'type': 'model-field', 'schema': {'type': 'str'}},
'field_b': {'type': 'model-field', 'schema': {'type': 'int'}},
},
'default': ({'field_a': '[default-a]', 'field_b': '[default-b]'}, None, set()),
'on_error': 'default',
},
}
)
'default': ({'field_a': '[default-a]', 'field_b': '[default-b]'}, None, set()),
'on_error': 'default',
},
}
)
m = v.validate_python({'field_a': 'test', 'field_b': 12})
assert isinstance(m, MyModel)
assert m.field_a == 'test'
Expand Down Expand Up @@ -388,44 +386,37 @@ class Model:
int_list_with_default: List[int] = stored_empty_list
str_dict_with_default: Dict[str, str] = stored_empty_dict

with pytest.warns(
UserWarning,
match=(
r'`[Ll]ist\[int\]` has a mutable default value that will be deep copied during validation.'
r' Consider using `default_factory` instead for finer control.'
),
):
v = SchemaValidator(
{
'type': 'model',
'cls': Model,
'schema': {
'type': 'model-fields',
'fields': {
'int_list_with_default': {
'type': 'model-field',
'schema': {
'type': 'default',
'schema': {'type': 'list', 'items_schema': {'type': 'int'}},
'default': stored_empty_list,
},
v = SchemaValidator(
{
'type': 'model',
'cls': Model,
'schema': {
'type': 'model-fields',
'fields': {
'int_list_with_default': {
'type': 'model-field',
'schema': {
'type': 'default',
'schema': {'type': 'list', 'items_schema': {'type': 'int'}},
'default': stored_empty_list,
},
'str_dict_with_default': {
'type': 'model-field',
},
'str_dict_with_default': {
'type': 'model-field',
'schema': {
'type': 'default',
'schema': {
'type': 'default',
'schema': {
'type': 'dict',
'keys_schema': {'type': 'str'},
'values_schema': {'type': 'str'},
},
'default': stored_empty_dict,
'type': 'dict',
'keys_schema': {'type': 'str'},
'values_schema': {'type': 'str'},
},
'default': stored_empty_dict,
},
},
},
}
)
},
}
)

m1 = v.validate_python({})

Expand Down
0