@@ -10,7 +10,7 @@ use crate::py_gc::PyGcTraverse;
1010
1111use config:: SerializationConfig ;
1212pub use errors:: { PydanticSerializationError , PydanticSerializationUnexpectedValue } ;
13- use extra:: { CollectWarnings , SerRecursionState } ;
13+ use extra:: { CollectWarnings , SerRecursionState , WarningsMode } ;
1414pub ( crate ) use extra:: { DuckTypingSerMode , Extra , SerMode , SerializationState } ;
1515pub use shared:: CombinedSerializer ;
1616use shared:: { to_json_bytes, BuildSerializer , TypeSerializer } ;
@@ -27,6 +27,12 @@ pub mod ser;
2727mod shared;
2828mod type_serializers;
2929
30+ #[ derive( FromPyObject ) ]
31+ pub enum WarningsArg {
32+ Bool ( bool ) ,
33+ Literal ( WarningsMode ) ,
34+ }
35+
3036#[ pyclass( module = "pydantic_core._pydantic_core" , frozen) ]
3137#[ derive( Debug ) ]
3238pub struct SchemaSerializer {
@@ -98,7 +104,7 @@ impl SchemaSerializer {
98104
99105 #[ allow( clippy:: too_many_arguments) ]
100106 #[ pyo3( signature = ( value, * , mode = None , include = None , exclude = None , by_alias = true ,
101- exclude_unset = false , exclude_defaults = false , exclude_none = false , round_trip = false , warnings = true ,
107+ exclude_unset = false , exclude_defaults = false , exclude_none = false , round_trip = false , warnings = WarningsArg :: Bool ( true ) ,
102108 fallback = None , serialize_as_any = false , context = None ) ) ]
103109 pub fn to_python (
104110 & self ,
@@ -112,13 +118,17 @@ impl SchemaSerializer {
112118 exclude_defaults : bool ,
113119 exclude_none : bool ,
114120 round_trip : bool ,
115- warnings : bool ,
121+ warnings : WarningsArg ,
116122 fallback : Option < & Bound < ' _ , PyAny > > ,
117123 serialize_as_any : bool ,
118124 context : Option < & Bound < ' _ , PyAny > > ,
119125 ) -> PyResult < PyObject > {
120126 let mode: SerMode = mode. into ( ) ;
121- let warnings = CollectWarnings :: new ( warnings) ;
127+ let warnings_mode = match warnings {
128+ WarningsArg :: Bool ( b) => b. into ( ) ,
129+ WarningsArg :: Literal ( mode) => mode,
130+ } ;
131+ let warnings = CollectWarnings :: new ( warnings_mode) ;
122132 let rec_guard = SerRecursionState :: default ( ) ;
123133 let duck_typing_ser_mode = DuckTypingSerMode :: from_bool ( serialize_as_any) ;
124134 let extra = self . build_extra (
@@ -143,7 +153,7 @@ impl SchemaSerializer {
143153
144154 #[ allow( clippy:: too_many_arguments) ]
145155 #[ pyo3( signature = ( value, * , indent = None , include = None , exclude = None , by_alias = true ,
146- exclude_unset = false , exclude_defaults = false , exclude_none = false , round_trip = false , warnings = true ,
156+ exclude_unset = false , exclude_defaults = false , exclude_none = false , round_trip = false , warnings = WarningsArg :: Bool ( true ) ,
147157 fallback = None , serialize_as_any = false , context = None ) ) ]
148158 pub fn to_json (
149159 & self ,
@@ -157,12 +167,16 @@ impl SchemaSerializer {
157167 exclude_defaults : bool ,
158168 exclude_none : bool ,
159169 round_trip : bool ,
160- warnings : bool ,
170+ warnings : WarningsArg ,
161171 fallback : Option < & Bound < ' _ , PyAny > > ,
162172 serialize_as_any : bool ,
163173 context : Option < & Bound < ' _ , PyAny > > ,
164174 ) -> PyResult < PyObject > {
165- let warnings = CollectWarnings :: new ( warnings) ;
175+ let warnings_mode = match warnings {
176+ WarningsArg :: Bool ( b) => b. into ( ) ,
177+ WarningsArg :: Literal ( mode) => mode,
178+ } ;
179+ let warnings = CollectWarnings :: new ( warnings_mode) ;
166180 let rec_guard = SerRecursionState :: default ( ) ;
167181 let duck_typing_ser_mode = DuckTypingSerMode :: from_bool ( serialize_as_any) ;
168182 let extra = self . build_extra (
0 commit comments