@@ -127,7 +127,7 @@ impl SchemaValidator {
127127 pub fn py_new ( py : Python , schema : & Bound < ' _ , PyAny > , config : Option < & Bound < ' _ , PyDict > > ) -> PyResult < Self > {
128128 let mut definitions_builder = DefinitionsBuilder :: new ( ) ;
129129
130- let validator = build_validator ( schema, config, & mut definitions_builder) ?;
130+ let validator = build_validator_base ( schema, config, & mut definitions_builder) ?;
131131 let definitions = definitions_builder. finish ( ) ?;
132132 let py_schema = schema. clone ( ) . unbind ( ) ;
133133 let py_config = match config {
@@ -159,11 +159,6 @@ impl SchemaValidator {
159159 } )
160160 }
161161
162- pub fn __reduce__ < ' py > ( slf : & Bound < ' py , Self > ) -> PyResult < ( Bound < ' py , PyType > , Bound < ' py , PyTuple > ) > {
163- let init_args = ( & slf. get ( ) . py_schema , & slf. get ( ) . py_config ) . into_pyobject ( slf. py ( ) ) ?;
164- Ok ( ( slf. get_type ( ) , init_args) )
165- }
166-
167162 #[ allow( clippy:: too_many_arguments) ]
168163 #[ pyo3( signature = ( input, * , strict=None , from_attributes=None , context=None , self_instance=None , allow_partial=PartialMode :: Off , by_alias=None , by_name=None ) ) ]
169164 pub fn validate_python (
@@ -357,6 +352,11 @@ impl SchemaValidator {
357352 }
358353 }
359354
355+ pub fn __reduce__ < ' py > ( slf : & Bound < ' py , Self > ) -> PyResult < ( Bound < ' py , PyType > , Bound < ' py , PyTuple > ) > {
356+ let init_args = ( & slf. get ( ) . py_schema , & slf. get ( ) . py_config ) . into_pyobject ( slf. py ( ) ) ?;
357+ Ok ( ( slf. get_type ( ) , init_args) )
358+ }
359+
360360 pub fn __repr__ ( & self , py : Python ) -> String {
361361 format ! (
362362 "SchemaValidator(title={:?}, validator={:#?}, definitions={:#?}, cache_strings={})" ,
@@ -555,19 +555,40 @@ macro_rules! validator_match {
555555 } ;
556556}
557557
558+ // Used when creating the base validator instance, to avoid reusing the instance
559+ // when unpickling:
560+ pub fn build_validator_base (
561+ schema : & Bound < ' _ , PyAny > ,
562+ config : Option < & Bound < ' _ , PyDict > > ,
563+ definitions : & mut DefinitionsBuilder < CombinedValidator > ,
564+ ) -> PyResult < CombinedValidator > {
565+ build_validator_inner ( schema, config, definitions, false )
566+ }
567+
558568pub fn build_validator (
559569 schema : & Bound < ' _ , PyAny > ,
560570 config : Option < & Bound < ' _ , PyDict > > ,
561571 definitions : & mut DefinitionsBuilder < CombinedValidator > ,
572+ ) -> PyResult < CombinedValidator > {
573+ build_validator_inner ( schema, config, definitions, true )
574+ }
575+
576+ fn build_validator_inner (
577+ schema : & Bound < ' _ , PyAny > ,
578+ config : Option < & Bound < ' _ , PyDict > > ,
579+ definitions : & mut DefinitionsBuilder < CombinedValidator > ,
580+ use_prebuilt : bool ,
562581) -> PyResult < CombinedValidator > {
563582 let dict = schema. downcast :: < PyDict > ( ) ?;
564583 let py = schema. py ( ) ;
565584 let type_: Bound < ' _ , PyString > = dict. get_as_req ( intern ! ( py, "type" ) ) ?;
566585 let type_ = type_. to_str ( ) ?;
567586
568- // if we have a SchemaValidator on the type already, use it
569- if let Ok ( Some ( prebuilt_validator) ) = prebuilt:: PrebuiltValidator :: try_get_from_schema ( type_, dict) {
570- return Ok ( prebuilt_validator) ;
587+ if use_prebuilt {
588+ // if we have a SchemaValidator on the type already, use it
589+ if let Ok ( Some ( prebuilt_validator) ) = prebuilt:: PrebuiltValidator :: try_get_from_schema ( type_, dict) {
590+ return Ok ( prebuilt_validator) ;
591+ }
571592 }
572593
573594 validator_match ! (
0 commit comments