15
15
import math
16
16
import re
17
17
from typing import Any , Callable , IO , Iterable , Mapping , Optional , \
18
- Sequence , Set , Tuple , Union
18
+ Set , Tuple , Union
19
19
import unicodedata
20
20
21
21
from .parser import Parser
@@ -92,13 +92,15 @@ def _fp_constant_parser(s):
92
92
if object_pairs_hook :
93
93
dictify = object_pairs_hook
94
94
elif object_hook :
95
- dictify = lambda pairs : object_hook (dict (pairs ))
95
+ def dictify (pairs ):
96
+ return object_hook (dict (pairs ))
96
97
else :
97
- dictify = lambda pairs : dict ( pairs ) # pylint: disable=unnecessary-lambda
98
+ dictify = dict
98
99
99
100
if not allow_duplicate_keys :
100
101
_orig_dictify = dictify
101
- dictify = lambda pairs : _reject_duplicate_keys (pairs , _orig_dictify )
102
+ def dictify (pairs ):
103
+ return _reject_duplicate_keys (pairs , _orig_dictify )
102
104
103
105
parse_float = parse_float or float
104
106
parse_int = parse_int or int
@@ -111,7 +113,7 @@ def _reject_duplicate_keys(pairs, dictify):
111
113
keys = set ()
112
114
for key , _ in pairs :
113
115
if key in keys :
114
- raise ValueError ('Duplicate key "%s " found in object' , key )
116
+ raise ValueError (f 'Duplicate key "{ key } " found in object' )
115
117
keys .add (key )
116
118
return dictify (pairs )
117
119
@@ -129,12 +131,11 @@ def _walk_ast(el,
129
131
if ty == 'number' :
130
132
if v .startswith ('0x' ) or v .startswith ('0X' ):
131
133
return parse_int (v , base = 16 )
132
- elif '.' in v or 'e' in v or 'E' in v :
134
+ if '.' in v or 'e' in v or 'E' in v :
133
135
return parse_float (v )
134
- elif 'Infinity' in v or 'NaN' in v :
136
+ if 'Infinity' in v or 'NaN' in v :
135
137
return parse_constant (v )
136
- else :
137
- return parse_int (v )
138
+ return parse_int (v )
138
139
if ty == 'string' :
139
140
return v
140
141
if ty == 'object' :
@@ -147,7 +148,7 @@ def _walk_ast(el,
147
148
if ty == 'array' :
148
149
return [_walk_ast (el , dictify , parse_float , parse_int , parse_constant )
149
150
for el in v ]
150
- raise Exception ('unknown el: ' + el ) # pragma: no cover
151
+ raise ValueError ('unknown el: ' + el ) # pragma: no cover
151
152
152
153
153
154
def dump (obj : Any ,
@@ -202,6 +203,7 @@ def dump(obj: Any,
202
203
should produce exactly the same output as ``json.dump(obj, fp).``
203
204
"""
204
205
206
+ del kwargs
205
207
fp .write (dumps (obj = obj , skipkeys = skipkeys , ensure_ascii = ensure_ascii ,
206
208
check_circular = check_circular , allow_nan = allow_nan ,
207
209
cls = cls , indent = indent , separators = separators ,
@@ -261,6 +263,7 @@ def dumps(obj: Any,
261
263
"""
262
264
263
265
assert kwargs .get ('cls' , None ) is None , 'Custom encoders are not supported'
266
+ del cls
264
267
265
268
if separators is None :
266
269
if indent is None :
@@ -291,6 +294,7 @@ def _dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, indent,
291
294
seen : Optional [Set [int ]],
292
295
level : int ,
293
296
is_key : bool ):
297
+ # pylint: disable=too-many-statements
294
298
if obj is True :
295
299
s = 'true'
296
300
elif obj is False :
@@ -332,10 +336,10 @@ def _dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, indent,
332
336
333
337
if is_key :
334
338
if s is not None :
335
- return True , '"%s"' % s
339
+ return True , f'" { s } "'
336
340
if skipkeys :
337
341
return False , None
338
- raise TypeError ('invalid key %s' % repr (obj ))
342
+ raise TypeError (f 'invalid key { repr (obj )} ' )
339
343
340
344
if s is not None :
341
345
return True , s
@@ -344,7 +348,7 @@ def _dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, indent,
344
348
end_str = ''
345
349
if trailing_commas :
346
350
end_str = ','
347
- if type (indent ) == int :
351
+ if isinstance (indent , int ) :
348
352
if indent > 0 :
349
353
indent_str = '\n ' + ' ' * indent * level
350
354
end_str += '\n ' + ' ' * indent * (level - 1 )
@@ -365,11 +369,11 @@ def _dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, indent,
365
369
i = id (obj )
366
370
if i in seen :
367
371
raise ValueError ('Circular reference detected.' )
368
- else :
369
- seen .add (i )
372
+ seen .add (i )
370
373
371
- # In Python3, we'd check if this was an abc.Mapping or an abc.Sequence.
372
- # For now, just check for the attrs we need to iterate over the object.
374
+ # Ideally we'd use collections.abc.Mapping and collections.abc.Sequence
375
+ # here, but for backwards-compatibility with potential old callers,
376
+ # we only check for the two attributes we need in each case.
373
377
if hasattr (obj , 'keys' ) and hasattr (obj , '__getitem__' ):
374
378
s = _dump_dict (obj , skipkeys , ensure_ascii ,
375
379
check_circular , allow_nan , indent ,
@@ -423,9 +427,8 @@ def _dump_dict(obj, skipkeys, ensure_ascii, check_circular, allow_nan,
423
427
if valid_key :
424
428
if not allow_duplicate_keys :
425
429
if key_str in new_keys :
426
- raise ValueError ('duplicate key %s' % repr (key ))
427
- else :
428
- new_keys .add (key_str )
430
+ raise ValueError (f'duplicate key { repr (key )} ' )
431
+ new_keys .add (key_str )
429
432
if num_items_added :
430
433
s += item_sep
431
434
s += key_str + kv_sep + _dumps (obj [key ], skipkeys , ensure_ascii ,
@@ -436,7 +439,7 @@ def _dump_dict(obj, skipkeys, ensure_ascii, check_circular, allow_nan,
436
439
seen , level , is_key = False )[1 ]
437
440
num_items_added += 1
438
441
elif not skipkeys :
439
- raise TypeError ('invalid key %s' % repr (key ))
442
+ raise TypeError (f 'invalid key { repr (key )} ' )
440
443
441
444
s += end_str + '}'
442
445
return s
@@ -486,15 +489,15 @@ def _dump_str(obj, ensure_ascii):
486
489
ret .append (ch )
487
490
else :
488
491
o = ord (ch )
489
- if o >= 32 and o < 128 :
492
+ if 32 <= o < 128 :
490
493
ret .append (ch )
491
494
elif o < 65536 :
492
- ret .append ('\\ u' + '% 04x' % o )
495
+ ret .append (f '\\ u{ o : 04x} ' )
493
496
else :
494
497
val = o - 0x10000
495
498
high = 0xd800 + (val >> 10 )
496
499
low = 0xdc00 + (val & 0x3ff )
497
- ret .append ('\\ u% 04x\\ u% 04x' % ( high , low ) )
500
+ ret .append (f '\\ u{ high : 04x} \\ u{ low : 04x} ' )
498
501
return '' .join (ret ) + '"'
499
502
500
503
@@ -566,4 +569,4 @@ def _is_reserved_word(k):
566
569
567
570
568
571
def _raise_type_error (obj ):
569
- raise TypeError ('%s is not JSON5 serializable' % repr ( obj ) )
572
+ raise TypeError (f' { repr ( obj ) } is not JSON5 serializable' )
0 commit comments