@@ -9,7 +9,7 @@ use pyo3::PyVisit;
99use super :: { build_validator, BuildValidator , CombinedValidator , DefinitionsBuilder , ValidationState , Validator } ;
1010use crate :: build_tools:: py_schema_err;
1111use crate :: build_tools:: schema_or_config_same;
12- use crate :: errors:: { LocItem , ValError , ValResult } ;
12+ use crate :: errors:: { ErrorTypeDefaults , LocItem , ValError , ValResult } ;
1313use crate :: input:: Input ;
1414use crate :: py_gc:: PyGcTraverse ;
1515use crate :: tools:: SchemaDict ;
@@ -180,6 +180,11 @@ impl Validator for WithDefaultValidator {
180180 outer_loc : Option < impl Into < LocItem > > ,
181181 state : & mut ValidationState < ' _ , ' py > ,
182182 ) -> ValResult < Option < PyObject > > {
183+ if matches ! ( self . default , DefaultType :: DefaultFactory ( _, true ) ) && state. has_field_error {
184+ // The default factory might use data from fields that failed to validate, and this results
185+ // in an unhelpul error.
186+ return Err ( ValError :: new ( ErrorTypeDefaults :: DefaultFactoryNotCalled , input) ) ;
187+ }
183188 match self . default . default_value ( py, state. extra ( ) . data . as_ref ( ) ) ? {
184189 Some ( stored_dft) => {
185190 let dft: Py < PyAny > = if self . copy_default {
0 commit comments