@@ -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 (
@@ -355,6 +350,11 @@ impl SchemaValidator {
355350 }
356351 }
357352
353+ pub fn __reduce__ < ' py > ( slf : & Bound < ' py , Self > ) -> PyResult < ( Bound < ' py , PyType > , Bound < ' py , PyTuple > ) > {
354+ let init_args = ( & slf. get ( ) . py_schema , & slf. get ( ) . py_config ) . into_pyobject ( slf. py ( ) ) ?;
355+ Ok ( ( slf. get_type ( ) , init_args) )
356+ }
357+
358358 pub fn __repr__ ( & self , py : Python ) -> String {
359359 format ! (
360360 "SchemaValidator(title={:?}, validator={:#?}, definitions={:#?}, cache_strings={})" ,
@@ -553,19 +553,40 @@ macro_rules! validator_match {
553553 } ;
554554}
555555
556+ // Used when creating the base validator instance, to avoid reusing the instance
557+ // when unpickling:
558+ pub fn build_validator_base (
559+ schema : & Bound < ' _ , PyAny > ,
560+ config : Option < & Bound < ' _ , PyDict > > ,
561+ definitions : & mut DefinitionsBuilder < CombinedValidator > ,
562+ ) -> PyResult < CombinedValidator > {
563+ build_validator_inner ( schema, config, definitions, false )
564+ }
565+
556566pub fn build_validator (
557567 schema : & Bound < ' _ , PyAny > ,
558568 config : Option < & Bound < ' _ , PyDict > > ,
559569 definitions : & mut DefinitionsBuilder < CombinedValidator > ,
570+ ) -> PyResult < CombinedValidator > {
571+ build_validator_inner ( schema, config, definitions, true )
572+ }
573+
574+ fn build_validator_inner (
575+ schema : & Bound < ' _ , PyAny > ,
576+ config : Option < & Bound < ' _ , PyDict > > ,
577+ definitions : & mut DefinitionsBuilder < CombinedValidator > ,
578+ use_prebuilt : bool ,
560579) -> PyResult < CombinedValidator > {
561580 let dict = schema. downcast :: < PyDict > ( ) ?;
562581 let py = schema. py ( ) ;
563582 let type_: Bound < ' _ , PyString > = dict. get_as_req ( intern ! ( py, "type" ) ) ?;
564583 let type_ = type_. to_str ( ) ?;
565584
566- // if we have a SchemaValidator on the type already, use it
567- if let Ok ( Some ( prebuilt_validator) ) = prebuilt:: PrebuiltValidator :: try_get_from_schema ( type_, dict) {
568- return Ok ( prebuilt_validator) ;
585+ if use_prebuilt {
586+ // if we have a SchemaValidator on the type already, use it
587+ if let Ok ( Some ( prebuilt_validator) ) = prebuilt:: PrebuiltValidator :: try_get_from_schema ( type_, dict) {
588+ return Ok ( prebuilt_validator) ;
589+ }
569590 }
570591
571592 validator_match ! (
0 commit comments