@@ -30,6 +30,8 @@ pub(crate) struct SerializationState<'py> {
3030 pub model : Option < Bound < ' py , PyAny > > ,
3131 /// The name of the field currently being serialized, if any
3232 pub field_name : Option < FieldName < ' py > > ,
33+ /// Inside unions, checks are applied to attempt to select a preferred branch
34+ pub check : SerCheck ,
3335 pub include_exclude : ( Option < Bound < ' py , PyAny > > , Option < Bound < ' py , PyAny > > ) ,
3436}
3537
@@ -69,6 +71,7 @@ impl<'py> SerializationState<'py> {
6971 config,
7072 model : None ,
7173 field_name : None ,
74+ check : SerCheck :: None ,
7275 include_exclude : ( include, exclude) ,
7376 } )
7477 }
@@ -86,19 +89,18 @@ impl SerializationState<'_> {
8689 } )
8790 }
8891
89- pub fn warn_fallback_py ( & mut self , field_type : & str , value : & Bound < ' _ , PyAny > , extra : & Extra ) -> PyResult < ( ) > {
92+ pub fn warn_fallback_py ( & mut self , field_type : & str , value : & Bound < ' _ , PyAny > ) -> PyResult < ( ) > {
9093 self . warnings
91- . on_fallback_py ( field_type, value, self . field_name . as_ref ( ) , extra )
94+ . on_fallback_py ( field_type, value, self . field_name . as_ref ( ) , self . check )
9295 }
9396
94- pub fn warn_fallback_ser < ' py , S : serde:: ser:: Serializer > (
97+ pub fn warn_fallback_ser < S : serde:: ser:: Serializer > (
9598 & mut self ,
9699 field_type : & str ,
97- value : & Bound < ' py , PyAny > ,
98- extra : & Extra < ' _ , ' py > ,
100+ value : & Bound < ' _ , PyAny > ,
99101 ) -> Result < ( ) , S :: Error > {
100102 self . warnings
101- . on_fallback_ser :: < S > ( field_type, value, self . field_name . as_ref ( ) , extra )
103+ . on_fallback_ser :: < S > ( field_type, value, self . field_name . as_ref ( ) , self . check )
102104 }
103105
104106 pub fn final_check ( & self , py : Python ) -> PyResult < ( ) > {
@@ -160,8 +162,6 @@ pub(crate) struct Extra<'a, 'py> {
160162 pub exclude_none : bool ,
161163 pub exclude_computed_fields : bool ,
162164 pub round_trip : bool ,
163- // the next two are used for union logic
164- pub check : SerCheck ,
165165 pub serialize_unknown : bool ,
166166 pub fallback : Option < & ' a Bound < ' py , PyAny > > ,
167167 pub serialize_as_any : bool ,
@@ -193,7 +193,6 @@ impl<'a, 'py> Extra<'a, 'py> {
193193 exclude_none,
194194 exclude_computed_fields,
195195 round_trip,
196- check : SerCheck :: None ,
197196 serialize_unknown,
198197 fallback,
199198 serialize_as_any,
@@ -283,7 +282,7 @@ impl ExtraOwned {
283282 round_trip : extra. round_trip ,
284283 config : state. config ,
285284 rec_guard : state. rec_guard . clone ( ) ,
286- check : extra . check ,
285+ check : state . check ,
287286 model : state. model . as_ref ( ) . map ( |model| model. clone ( ) . into ( ) ) ,
288287 field_name : state. field_name . as_ref ( ) . map ( |name| match name {
289288 FieldName :: Root => FieldNameOwned :: Root ,
@@ -308,7 +307,6 @@ impl ExtraOwned {
308307 exclude_none : self . exclude_none ,
309308 exclude_computed_fields : self . exclude_computed_fields ,
310309 round_trip : self . round_trip ,
311- check : self . check ,
312310 serialize_unknown : self . serialize_unknown ,
313311 fallback : self . fallback . as_ref ( ) . map ( |m| m. bind ( py) ) ,
314312 serialize_as_any : self . serialize_as_any ,
@@ -321,16 +319,17 @@ impl ExtraOwned {
321319 warnings : self . warnings . clone ( ) ,
322320 rec_guard : self . rec_guard . clone ( ) ,
323321 config : self . config ,
322+ model : self . model . as_ref ( ) . map ( |m| m. bind ( py) . clone ( ) ) ,
324323 field_name : match & self . field_name {
325324 Some ( FieldNameOwned :: Root ) => Some ( FieldName :: Root ) ,
326325 Some ( FieldNameOwned :: Regular ( b) ) => Some ( FieldName :: Regular ( b. bind ( py) . clone ( ) ) ) ,
327326 None => None ,
328327 } ,
328+ check : self . check ,
329329 include_exclude : (
330330 self . include . as_ref ( ) . map ( |m| m. bind ( py) . clone ( ) ) ,
331331 self . exclude . as_ref ( ) . map ( |m| m. bind ( py) . clone ( ) ) ,
332332 ) ,
333- model : self . model . as_ref ( ) . map ( |m| m. bind ( py) . clone ( ) ) ,
334333 }
335334 }
336335}
@@ -443,17 +442,17 @@ impl CollectWarnings {
443442 }
444443 }
445444
446- pub fn on_fallback_py < ' py > (
445+ fn on_fallback_py (
447446 & mut self ,
448447 field_type : & str ,
449- value : & Bound < ' py , PyAny > ,
448+ value : & Bound < ' _ , PyAny > ,
450449 field_name : Option < & FieldName < ' _ > > ,
451- extra : & Extra < ' _ , ' py > ,
450+ check : SerCheck ,
452451 ) -> PyResult < ( ) > {
453452 // special case for None as it's very common e.g. as a default value
454453 if value. is_none ( ) {
455454 Ok ( ( ) )
456- } else if extra . check . enabled ( ) {
455+ } else if check. enabled ( ) {
457456 Err ( PydanticSerializationUnexpectedValue :: new_from_parts (
458457 field_name. map ( ToString :: to_string) ,
459458 Some ( field_type. to_string ( ) ) ,
@@ -466,17 +465,17 @@ impl CollectWarnings {
466465 }
467466 }
468467
469- pub fn on_fallback_ser < ' py , S : serde:: ser:: Serializer > (
468+ pub fn on_fallback_ser < S : serde:: ser:: Serializer > (
470469 & mut self ,
471470 field_type : & str ,
472- value : & Bound < ' py , PyAny > ,
471+ value : & Bound < ' _ , PyAny > ,
473472 field_name : Option < & FieldName < ' _ > > ,
474- extra : & Extra < ' _ , ' py > ,
473+ check : SerCheck ,
475474 ) -> Result < ( ) , S :: Error > {
476475 // special case for None as it's very common e.g. as a default value
477476 if value. is_none ( ) {
478477 Ok ( ( ) )
479- } else if extra . check . enabled ( ) {
478+ } else if check. enabled ( ) {
480479 // note: I think this should never actually happen since we use `to_python(..., mode='json')` during
481480 // JSON serialization to "try" union branches, but it's here for completeness/correctness
482481 // in particular, in future we could allow errors instead of warnings on fallback
0 commit comments