1
+ use std:: convert:: Infallible ;
1
2
use std:: error:: Error ;
2
3
use std:: fmt;
3
4
use std:: ops:: Deref ;
@@ -9,6 +10,7 @@ use pyo3::prelude::*;
9
10
use pyo3:: types:: { PyDict , PyList , PyString } ;
10
11
use pyo3:: { intern, FromPyObject , PyErrArguments } ;
11
12
13
+ use crate :: config:: CoreConfig ;
12
14
use crate :: errors:: { PyLineError , ValError } ;
13
15
use crate :: input:: InputType ;
14
16
use crate :: tools:: SchemaDict ;
43
45
schema_or_config ( schema, config, key, key)
44
46
}
45
47
46
- pub fn is_strict ( schema : & Bound < ' _ , PyDict > , config : Option < & Bound < ' _ , PyDict > > ) -> PyResult < bool > {
48
+ pub fn is_strict ( schema : & Bound < ' _ , PyDict > , config : & CoreConfig ) -> PyResult < bool > {
47
49
let py = schema. py ( ) ;
48
- Ok ( schema_or_config_same ( schema, config, intern ! ( py, "strict" ) ) ?. unwrap_or ( false ) )
50
+ let is_strict = schema
51
+ . get_as ( intern ! ( py, "strict" ) ) ?
52
+ . flatten ( )
53
+ . or ( config. strict )
54
+ . unwrap_or ( false ) ;
55
+ Ok ( is_strict)
49
56
}
50
57
51
58
enum SchemaErrorEnum {
@@ -189,21 +196,36 @@ impl ExtraBehavior {
189
196
pub fn from_schema_or_config (
190
197
py : Python ,
191
198
schema : & Bound < ' _ , PyDict > ,
192
- config : Option < & Bound < ' _ , PyDict > > ,
199
+ config : & CoreConfig ,
193
200
default : Self ,
194
201
) -> PyResult < Self > {
195
- let extra_behavior = schema_or_config :: < Option < Bound < ' _ , PyString > > > (
196
- schema,
197
- config,
198
- intern ! ( py, "extra_behavior" ) ,
199
- intern ! ( py, "extra_fields_behavior" ) ,
200
- ) ?
201
- . flatten ( ) ;
202
- let res = match extra_behavior. as_ref ( ) . map ( |s| s. to_str ( ) ) . transpose ( ) ? {
203
- Some ( s) => Self :: from_str ( s) ?,
204
- None => default,
202
+ let extra_behavior = schema. get_as ( intern ! ( py, "extra_behavior" ) ) ?. flatten ( ) ;
203
+ let extra_behavior = extra_behavior. or ( config. extra_fields_behavior ) ;
204
+ Ok ( extra_behavior. unwrap_or ( default) )
205
+ }
206
+ }
207
+
208
+ impl FromPyObject < ' _ > for ExtraBehavior {
209
+ fn extract_bound ( ob : & Bound < ' _ , PyAny > ) -> PyResult < Self > {
210
+ let s: & str = ob. extract ( ) ?;
211
+ Self :: from_str ( s)
212
+ }
213
+ }
214
+
215
+ impl < ' py > IntoPyObject < ' py > for ExtraBehavior {
216
+ type Target = PyString ;
217
+
218
+ type Output = Borrowed < ' py , ' py , PyString > ;
219
+
220
+ type Error = Infallible ;
221
+
222
+ fn into_pyobject ( self , py : Python < ' py > ) -> Result < Self :: Output , Self :: Error > {
223
+ let s = match self {
224
+ Self :: Allow => intern ! ( py, "allow" ) ,
225
+ Self :: Forbid => intern ! ( py, "forbid" ) ,
226
+ Self :: Ignore => intern ! ( py, "ignore" ) ,
205
227
} ;
206
- Ok ( res )
228
+ Ok ( s . as_borrowed ( ) )
207
229
}
208
230
}
209
231
0 commit comments