Skip to content

Commit

Permalink
For missing fields, we don't warn on exclude_, so don't warn on `ex…
Browse files Browse the repository at this point in the history
…clude` (#1403)
  • Loading branch information
sydney-runkle committed Aug 13, 2024
1 parent 39a6b10 commit a072575
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
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}

0 comments on commit a072575

Please sign in to comment.