Skip to content

Commit 63d2ccc

Browse files
committed
Count validated model fields once in smart unions
Backport of: #13731
1 parent a53ec2e commit 63d2ccc

2 files changed

Lines changed: 62 additions & 10 deletions

File tree

‎pydantic-core/src/validators/model_fields.rs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,6 @@ impl Validator for ModelFieldsValidator {
179179
};
180180
self.validate_by_get_item(py, input, dict, state)?
181181
};
182-
state.add_fields_set(fields_set.len());
183182

184183
// if we have extra=allow, but we didn't create a dict because we were validating
185184
// from attributes, set it now so __pydantic_extra__ is always a dict if extra=allow
@@ -559,6 +558,7 @@ impl ModelFieldsValidator {
559558
(0..self.fields.len()).map(|_| None).collect();
560559
let mut errors: Vec<ValLineError> = Vec::new();
561560
let fields_set = PySet::empty(py)?;
561+
let mut fields_set_count: usize = 0;
562562

563563
let state = &mut state.rebind_extra(|extra| extra.data = Some(model_dict.clone()));
564564
let state = &mut state.scoped_set(|state| &mut state.has_field_error, false);
@@ -635,6 +635,7 @@ impl ModelFieldsValidator {
635635
match field.validator.validate(py, field_json_value, state) {
636636
Ok(value) => {
637637
fields_set.add(&field.name)?;
638+
fields_set_count += 1;
638639
value
639640
}
640641
Err(ValError::Omit) => continue,
@@ -696,6 +697,7 @@ impl ModelFieldsValidator {
696697
return Err(ValError::LineErrors(errors));
697698
}
698699

700+
state.add_fields_set(fields_set_count);
699701
Ok((model_dict, model_extra_dict_op, fields_set))
700702
}
701703
}

‎tests/types/test_union.py‎

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
from typing import ClassVar, Literal, Optional, Union
1+
from typing import Any, ClassVar, Literal, Optional, Union
22

3+
import pytest
34
from typing_extensions import TypedDict
45

56
import pydantic
7+
from pydantic import BaseModel, ConfigDict, TypeAdapter, model_validator
68

79

810
def test_field_serializer_in_nested_union_called_only_twice():
9-
class MyModel(pydantic.BaseModel):
11+
class MyModel(BaseModel):
1012
a: int
1113
b: int
1214

@@ -40,7 +42,7 @@ class Container2(TypedDict):
4042

4143

4244
def test_field_serializer_in_nested_tagged_union_called_only_twice():
43-
class MyModel(pydantic.BaseModel):
45+
class MyModel(BaseModel):
4446
type_: Literal['a'] = 'a'
4547

4648
a: int
@@ -53,14 +55,14 @@ def serialize_my_field(self, value: int) -> str:
5355
self.__class__.field_a_serializer_calls += 1
5456
return str(value)
5557

56-
class ModelB(pydantic.BaseModel):
58+
class ModelB(BaseModel):
5759
type_: Literal['b'] = 'b'
5860

59-
class Container(pydantic.BaseModel):
61+
class Container(BaseModel):
6062
type_: Literal['a'] = 'a'
6163
u: Union[MyModel, ModelB] = pydantic.Field(..., discriminator='type_')
6264

63-
class Container2(pydantic.BaseModel):
65+
class Container2(BaseModel):
6466
u: Union[Container, ModelB] = pydantic.Field(..., discriminator='type_')
6567

6668
# forcibly construct model with a False value
@@ -77,14 +79,14 @@ class Container2(pydantic.BaseModel):
7779

7880

7981
def test_exclude_unset_in_nested_union():
80-
class Cat(pydantic.BaseModel):
82+
class Cat(BaseModel):
8183
type: Literal['cat']
8284
color: Optional[str] = None # field with default
8385

84-
class Dog(pydantic.BaseModel):
86+
class Dog(BaseModel):
8587
type: Literal['dog']
8688

87-
class Zoo(pydantic.BaseModel):
89+
class Zoo(BaseModel):
8890
animals: list[Union[Cat, Dog]]
8991

9092
cat = Cat(type='cat')
@@ -121,3 +123,51 @@ class TD(TypedDict):
121123
# test to document the behaviour that if all options in the union fail,
122124
# the field is omitted, even if it is required in the TypedDict.
123125
assert ta.validate_python({'x': 'foo'}) == {}
126+
127+
128+
@pytest.mark.parametrize('extra', ['ignore', 'allow'])
129+
@pytest.mark.parametrize('reverse_members', [False, True])
130+
def test_smart_union_fields_set_count_consistent_between_json_and_python(
131+
extra: Literal['ignore', 'allow'], reverse_members: bool
132+
) -> None:
133+
"""https://github.com/pydantic/pydantic/issues/13729"""
134+
135+
class Smaller(BaseModel):
136+
model_config = ConfigDict(extra=extra)
137+
138+
a: int
139+
b: int
140+
141+
@model_validator(mode='before')
142+
@classmethod
143+
def keep_input(cls, value: Any) -> Any:
144+
return value
145+
146+
class Larger(BaseModel):
147+
a: int
148+
b: int
149+
c: int
150+
151+
ta = TypeAdapter(Union[Larger, Smaller] if reverse_members else Union[Smaller, Larger])
152+
153+
assert isinstance(ta.validate_python({'a': 1, 'b': 2, 'c': 3}), Larger)
154+
assert isinstance(ta.validate_json('{"a": 1, "b": 2, "c": 3}'), Larger)
155+
156+
157+
@pytest.mark.parametrize('reverse_members', [False, True])
158+
def test_smart_union_fields_set_count_ignores_extra(reverse_members: bool) -> None:
159+
"""https://github.com/pydantic/pydantic/issues/13729"""
160+
161+
class WithExtra(BaseModel):
162+
model_config = ConfigDict(extra='allow')
163+
164+
a: int
165+
166+
class Explicit(BaseModel):
167+
a: int
168+
b: int
169+
170+
ta = TypeAdapter(Union[Explicit, WithExtra] if reverse_members else Union[WithExtra, Explicit])
171+
172+
assert isinstance(ta.validate_python({'a': 1, 'b': 2}), Explicit)
173+
assert isinstance(ta.validate_json('{"a": 1, "b": 2}'), Explicit)

0 commit comments

Comments
 (0)