@@ -248,7 +248,7 @@ impl<'py> Input<'py> for Bound<'py, PyAny> {
248
248
str_as_int ( self , s)
249
249
} else if self . is_exact_instance_of :: < PyFloat > ( ) {
250
250
float_as_int ( self , self . extract :: < f64 > ( ) ?)
251
- } else if let Ok ( decimal) = self . strict_decimal ( self . py ( ) ) {
251
+ } else if let Ok ( decimal) = self . validate_decimal ( strict , self . py ( ) ) {
252
252
decimal_as_int ( self , & decimal. into_inner ( ) )
253
253
} else if let Ok ( float) = self . extract :: < f64 > ( ) {
254
254
float_as_int ( self , float)
@@ -307,51 +307,38 @@ impl<'py> Input<'py> for Bound<'py, PyAny> {
307
307
Err ( ValError :: new ( ErrorTypeDefaults :: FloatType , self ) )
308
308
}
309
309
310
- fn strict_decimal ( & self , py : Python < ' py > ) -> ValMatch < Bound < ' py , PyAny > > {
310
+ fn validate_decimal ( & self , strict : bool , py : Python < ' py > ) -> ValMatch < Bound < ' py , PyAny > > {
311
311
let decimal_type = get_decimal_type ( py) ;
312
- // Fast path for existing decimal objects
313
- if self . is_exact_instance ( decimal_type) {
314
- return Ok ( ValidationMatch :: exact ( self . to_owned ( ) ) ) ;
315
- }
316
-
317
- // Try subclasses of decimals, they will be upcast to Decimal
318
- if self . is_instance ( decimal_type) ? {
319
- return create_decimal ( self , self ) . map ( ValidationMatch :: strict) ;
320
- }
321
-
322
- Err ( ValError :: new (
323
- ErrorType :: IsInstanceOf {
324
- class : decimal_type
325
- . qualname ( )
326
- . and_then ( |name| name. extract ( ) )
327
- . unwrap_or_else ( |_| "Decimal" . to_owned ( ) ) ,
328
- context : None ,
329
- } ,
330
- self ,
331
- ) )
332
- }
333
312
334
- fn lax_decimal ( & self , py : Python < ' py > ) -> ValMatch < Bound < ' py , PyAny > > {
335
- let decimal_type = get_decimal_type ( py) ;
336
313
// Fast path for existing decimal objects
337
314
if self . is_exact_instance ( decimal_type) {
338
- return Ok ( ValidationMatch :: exact ( self . to_owned ( ) . clone ( ) ) ) ;
339
- }
340
-
341
- // TODO: I can see the case for int and float being strict - wdyt @davidhewitt?
342
- return if self . is_instance_of :: < PyString > ( )
343
- || ( self . is_instance_of :: < PyInt > ( ) && !self . is_instance_of :: < PyBool > ( ) )
344
- {
345
- // checking isinstance for str / int / bool is fast compared to decimal / float
346
- create_decimal ( self , self ) . map ( ValidationMatch :: lax)
315
+ Ok ( ValidationMatch :: exact ( self . to_owned ( ) . clone ( ) ) )
347
316
} else if self . is_instance ( decimal_type) ? {
348
- // upcast subclasses to decimal
317
+ // Upcast subclasses to decimal
349
318
create_decimal ( self , self ) . map ( ValidationMatch :: strict)
350
- } else if self . is_instance_of :: < PyFloat > ( ) {
351
- create_decimal ( self . str ( ) ?. as_any ( ) , self ) . map ( ValidationMatch :: lax)
352
319
} else {
353
- Err ( ValError :: new ( ErrorTypeDefaults :: DecimalType , self ) )
354
- } ;
320
+ if strict {
321
+ return Err ( ValError :: new (
322
+ ErrorType :: IsInstanceOf {
323
+ class : decimal_type
324
+ . qualname ( )
325
+ . and_then ( |name| name. extract ( ) )
326
+ . unwrap_or_else ( |_| "Decimal" . to_owned ( ) ) ,
327
+ context : None ,
328
+ } ,
329
+ self ,
330
+ ) ) ;
331
+ }
332
+ if self . is_instance_of :: < PyString > ( ) || ( self . is_instance_of :: < PyInt > ( ) && !self . is_instance_of :: < PyBool > ( ) )
333
+ {
334
+ // Checking isinstance for str / int / bool is fast compared to decimal / float
335
+ create_decimal ( self , self ) . map ( ValidationMatch :: lax)
336
+ } else if self . is_instance_of :: < PyFloat > ( ) {
337
+ create_decimal ( self . str ( ) ?. as_any ( ) , self ) . map ( ValidationMatch :: lax)
338
+ } else {
339
+ Err ( ValError :: new ( ErrorTypeDefaults :: DecimalType , self ) )
340
+ }
341
+ }
355
342
}
356
343
357
344
type Dict < ' a > = GenericPyMapping < ' a , ' py > where Self : ' a ;
0 commit comments