Skip to content

Commit 76ecbf5

Browse files
committed
fix strict behavior for unions
1 parent a0e60bd commit 76ecbf5

File tree

1 file changed

+2
-20
lines changed

1 file changed

+2
-20
lines changed

src/validators/union.rs

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use pyo3::{intern, PyTraverseError, PyVisit};
88
use smallvec::SmallVec;
99

1010
use crate::build_tools::py_schema_err;
11-
use crate::build_tools::{is_strict, schema_or_config};
11+
use crate::build_tools::schema_or_config;
1212
use crate::common::union::{Discriminator, SMALL_UNION_THRESHOLD};
1313
use crate::errors::{ErrorType, ToErrorValue, ValError, ValLineError, ValResult};
1414
use crate::input::{BorrowInput, Input, ValidatedDict};
@@ -43,7 +43,6 @@ pub struct UnionValidator {
4343
mode: UnionMode,
4444
choices: Vec<(CombinedValidator, Option<String>)>,
4545
custom_error: Option<CustomError>,
46-
strict: bool,
4746
name: String,
4847
}
4948

@@ -91,7 +90,6 @@ impl BuildValidator for UnionValidator {
9190
mode,
9291
choices,
9392
custom_error: CustomError::build(schema, config, definitions)?,
94-
strict: is_strict(schema, config)?,
9593
name: format!("{}[{descr}]", Self::EXPECTED_TYPE),
9694
}
9795
.into())
@@ -110,17 +108,11 @@ impl UnionValidator {
110108
let old_exactness = state.exactness;
111109
let old_fields_set_count = state.fields_set_count;
112110

113-
let strict = state.strict_or(self.strict);
114111
let mut errors = MaybeErrors::new(self.custom_error.as_ref());
115112

116113
let mut best_match: Option<(Py<PyAny>, Exactness, Option<usize>)> = None;
117114

118115
for (choice, label) in &self.choices {
119-
let state = &mut state.rebind_extra(|extra| {
120-
if strict {
121-
extra.strict = Some(strict);
122-
}
123-
});
124116
state.exactness = Some(Exactness::Exact);
125117
state.fields_set_count = None;
126118
let result = choice.validate(py, input, state);
@@ -197,14 +189,6 @@ impl UnionValidator {
197189
) -> ValResult<PyObject> {
198190
let mut errors = MaybeErrors::new(self.custom_error.as_ref());
199191

200-
let mut rebound_state;
201-
let state = if state.strict_or(self.strict) {
202-
rebound_state = state.rebind_extra(|extra| extra.strict = Some(true));
203-
&mut rebound_state
204-
} else {
205-
state
206-
};
207-
208192
for (validator, label) in &self.choices {
209193
match validator.validate(py, input, state) {
210194
Err(ValError::LineErrors(lines)) => errors.push(validator, label.as_deref(), lines),
@@ -300,7 +284,6 @@ pub struct TaggedUnionValidator {
300284
discriminator: Discriminator,
301285
lookup: LiteralLookup<CombinedValidator>,
302286
from_attributes: bool,
303-
strict: bool,
304287
custom_error: Option<CustomError>,
305288
tags_repr: String,
306289
discriminator_repr: String,
@@ -349,7 +332,6 @@ impl BuildValidator for TaggedUnionValidator {
349332
discriminator,
350333
lookup,
351334
from_attributes,
352-
strict: is_strict(schema, config)?,
353335
custom_error: CustomError::build(schema, config, definitions)?,
354336
tags_repr,
355337
discriminator_repr,
@@ -371,7 +353,7 @@ impl Validator for TaggedUnionValidator {
371353
match &self.discriminator {
372354
Discriminator::LookupKey(lookup_key) => {
373355
let from_attributes = state.extra().from_attributes.unwrap_or(self.from_attributes);
374-
let dict = input.validate_model_fields(self.strict, from_attributes)?;
356+
let dict = input.validate_model_fields(state.strict_or(false), from_attributes)?;
375357
// note this methods returns PyResult<Option<(data, data)>>, the outer Err is just for
376358
// errors when getting attributes which should be "raised"
377359
let tag = match dict.get_item(lookup_key)? {

0 commit comments

Comments
 (0)