8
8
JSON_TYPE_TO_PYTHON_TYPE = {
9
9
'null' : 'NoneType' ,
10
10
'boolean' : 'bool' ,
11
- 'number' : 'int, float' ,
11
+ 'number' : 'int, float, Decimal ' ,
12
12
'integer' : 'int' ,
13
13
'string' : 'str' ,
14
14
'array' : 'list, tuple' ,
@@ -285,8 +285,8 @@ def _generate_format(self, format_name, regexp_name, regexp):
285
285
self .exc ('{name} must be {}' , format_name , rule = 'format' )
286
286
287
287
def generate_minimum (self ):
288
- with self .l ('if isinstance({variable}, (int, float)):' ):
289
- if not isinstance (self ._definition ['minimum' ], (int , float )):
288
+ with self .l ('if isinstance({variable}, (int, float, Decimal )):' ):
289
+ if not isinstance (self ._definition ['minimum' ], (int , float , decimal . Decimal )):
290
290
raise JsonSchemaDefinitionException ('minimum must be a number' )
291
291
if self ._definition .get ('exclusiveMinimum' , False ):
292
292
with self .l ('if {variable} <= {minimum}:' ):
@@ -296,8 +296,8 @@ def generate_minimum(self):
296
296
self .exc ('{name} must be bigger than or equal to {minimum}' , rule = 'minimum' )
297
297
298
298
def generate_maximum (self ):
299
- with self .l ('if isinstance({variable}, (int, float)):' ):
300
- if not isinstance (self ._definition ['maximum' ], (int , float )):
299
+ with self .l ('if isinstance({variable}, (int, float, Decimal )):' ):
300
+ if not isinstance (self ._definition ['maximum' ], (int , float , decimal . Decimal )):
301
301
raise JsonSchemaDefinitionException ('maximum must be a number' )
302
302
if self ._definition .get ('exclusiveMaximum' , False ):
303
303
with self .l ('if {variable} >= {maximum}:' ):
@@ -307,14 +307,12 @@ def generate_maximum(self):
307
307
self .exc ('{name} must be smaller than or equal to {maximum}' , rule = 'maximum' )
308
308
309
309
def generate_multiple_of (self ):
310
- with self .l ('if isinstance({variable}, (int, float)):' ):
311
- if not isinstance (self ._definition ['multipleOf' ], (int , float )):
310
+ with self .l ('if isinstance({variable}, (int, float, Decimal )):' ):
311
+ if not isinstance (self ._definition ['multipleOf' ], (int , float , decimal . Decimal )):
312
312
raise JsonSchemaDefinitionException ('multipleOf must be a number' )
313
313
# For proper multiplication check of floats we need to use decimals,
314
314
# because for example 19.01 / 0.01 = 1901.0000000000002.
315
315
if isinstance (self ._definition ['multipleOf' ], float ):
316
- self ._extra_imports_lines .append ('from decimal import Decimal' )
317
- self ._extra_imports_objects ['Decimal' ] = decimal .Decimal
318
316
self .l ('quotient = Decimal(repr({variable})) / Decimal(repr({multipleOf}))' )
319
317
else :
320
318
self .l ('quotient = {variable} / {multipleOf}' )
0 commit comments