@@ -127,7 +127,7 @@ impl SchemaValidator {
127
127
pub fn py_new ( py : Python , schema : & Bound < ' _ , PyAny > , config : Option < & Bound < ' _ , PyDict > > ) -> PyResult < Self > {
128
128
let mut definitions_builder = DefinitionsBuilder :: new ( ) ;
129
129
130
- let validator = build_validator ( schema, config, & mut definitions_builder) ?;
130
+ let validator = build_validator_base ( schema, config, & mut definitions_builder) ?;
131
131
let definitions = definitions_builder. finish ( ) ?;
132
132
let py_schema = schema. clone ( ) . unbind ( ) ;
133
133
let py_config = match config {
@@ -159,11 +159,6 @@ impl SchemaValidator {
159
159
} )
160
160
}
161
161
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
-
167
162
#[ allow( clippy:: too_many_arguments) ]
168
163
#[ pyo3( signature = ( input, * , strict=None , from_attributes=None , context=None , self_instance=None , allow_partial=PartialMode :: Off , by_alias=None , by_name=None ) ) ]
169
164
pub fn validate_python (
@@ -355,6 +350,11 @@ impl SchemaValidator {
355
350
}
356
351
}
357
352
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
+
358
358
pub fn __repr__ ( & self , py : Python ) -> String {
359
359
format ! (
360
360
"SchemaValidator(title={:?}, validator={:#?}, definitions={:#?}, cache_strings={})" ,
@@ -553,19 +553,40 @@ macro_rules! validator_match {
553
553
} ;
554
554
}
555
555
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
+
556
566
pub fn build_validator (
557
567
schema : & Bound < ' _ , PyAny > ,
558
568
config : Option < & Bound < ' _ , PyDict > > ,
559
569
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 ,
560
579
) -> PyResult < CombinedValidator > {
561
580
let dict = schema. downcast :: < PyDict > ( ) ?;
562
581
let py = schema. py ( ) ;
563
582
let type_: Bound < ' _ , PyString > = dict. get_as_req ( intern ! ( py, "type" ) ) ?;
564
583
let type_ = type_. to_str ( ) ?;
565
584
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
+ }
569
590
}
570
591
571
592
validator_match ! (
0 commit comments