Skip to content
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

For missing fields, we don't warn on exclude_, so don't warn on exclude #1403

Merged
merged 2 commits into from
Aug 13, 2024
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
2 changes: 1 addition & 1 deletion src/serializers/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ impl GeneralFieldsSerializer {

if extra.check.enabled()
// If any of these are true we can't count fields
&& !(extra.exclude_defaults || extra.exclude_unset || extra.exclude_none)
&& !(extra.exclude_defaults || extra.exclude_unset || extra.exclude_none || exclude.is_some())
// Check for missing fields, we can't have extra fields here
&& self.required_fields > used_req_fields
{
Expand Down
21 changes: 21 additions & 0 deletions tests/serializers/test_model.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import dataclasses
import json
import platform
import warnings
from random import randint
from typing import Any, ClassVar, Dict

Expand Down Expand Up @@ -1080,3 +1081,23 @@ class Model:
m.__pydantic_extra__ = {'extra': 'extra'}

assert s.to_python(m) == {'extra': 'extra bam!'}


def test_no_warn_on_exclude() -> None:
warnings.simplefilter('error')

s = SchemaSerializer(
core_schema.model_schema(
BasicModel,
core_schema.model_fields_schema(
{
'a': core_schema.model_field(core_schema.int_schema()),
'b': core_schema.model_field(core_schema.int_schema()),
}
),
)
)

value = BasicModel(a=0, b=1)
assert s.to_python(value, exclude={'b'}) == {'a': 0}
assert s.to_python(value, mode='json', exclude={'b'}) == {'a': 0}
Loading