From e93c816898754ab350e36213bca09cecfff45ee4 Mon Sep 17 00:00:00 2001 From: Dirk Pranke Date: Sat, 9 Mar 2024 17:07:20 -0800 Subject: [PATCH] Reformat the code with Ruff (`./run format`). This reformats all the code with the Ruff Python formatter. pyproject.toml contains the configuration for the formatter. --- .gitignore | 1 + benchmarks/run.py | 32 +- json5/arg_parser.py | 11 +- json5/lib.py | 566 +++++++++++++++++++++++----------- json5/parser.py | 728 ++++++++++++++++++++++++++++++++------------ json5/tool.py | 94 ++++-- tests/host_fake.py | 2 +- tests/host_test.py | 1 + tests/lib_test.py | 254 +++++++++------- tests/tool_test.py | 94 ++++-- 10 files changed, 1227 insertions(+), 556 deletions(-) diff --git a/.gitignore b/.gitignore index 33f0c64..84b56f8 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .coverage +.ruff_cache build dist *.pyc diff --git a/benchmarks/run.py b/benchmarks/run.py index 74bde13..0d59ba4 100755 --- a/benchmarks/run.py +++ b/benchmarks/run.py @@ -35,8 +35,9 @@ def main(): parser = argparse.ArgumentParser() parser.add_argument('--pure', action='store_true') - parser.add_argument('-n', '--num-iterations', default=DEFAULT_ITERATIONS, - type=int) + parser.add_argument( + '-n', '--num-iterations', default=DEFAULT_ITERATIONS, type=int + ) parser.add_argument('benchmarks', nargs='*') args = parser.parse_args() if not args.benchmarks: @@ -47,7 +48,6 @@ def main(): with open(f, encoding='utf-8') as fp: file_contents.append(fp.read()) - # json.decoder.c_scanstring = py_scanstring def py_maker(*args, **kwargs): del args @@ -81,20 +81,28 @@ def py_maker(*args, **kwargs): if json5_time and json_time: if json5_time > json_time: avg = json5_time / json_time - print(f"{fname:-%20s}: JSON was {avg:5.1f}x faster " - f"({json_time:.6f} to {json5_time:.6fs}") + print( + f'{fname:-%20s}: JSON was {avg:5.1f}x faster ' + f'({json_time:.6f} to {json5_time:.6fs}' + ) else: avg = json_time / json5_time - print(f"{fname:-%20s}: JSON5 was {avg:5.1f}x faster " - f"({json5_time:.6f} to {json_time:.6fs}") + print( + f'{fname:-%20s}: JSON5 was {avg:5.1f}x faster ' + f'({json5_time:.6f} to {json_time:.6fs}' + ) elif json5_time: - print(f"{fname:-20s}: JSON5 took {json5_time:.6f} secs, " - f"JSON was too fast to measure") + print( + f'{fname:-20s}: JSON5 took {json5_time:.6f} secs, ' + f'JSON was too fast to measure' + ) elif json_time: - print(f"{fname:-20s}: JSON took {json_time:.6f} secs, " - f"JSON5 was too fast to measure") + print( + f'{fname:-20s}: JSON took {json_time:.6f} secs, ' + f'JSON5 was too fast to measure' + ) else: - print(f"{fname:-20s}: both were too fast to measure") + print(f'{fname:-20s}: both were too fast to measure') return 0 diff --git a/json5/arg_parser.py b/json5/arg_parser.py index fad6c59..8cc043e 100644 --- a/json5/arg_parser.py +++ b/json5/arg_parser.py @@ -29,8 +29,12 @@ def __init__(self, host, prog, desc, **kwargs): super().__init__(**kwargs) self._host = host self.exit_status = None - self.add_argument('-V', '--version', action='store_true', - help='print the version and exit') + self.add_argument( + '-V', + '--version', + action='store_true', + help='print the version and exit', + ) def parse_args(self, args=None, namespace=None): try: @@ -49,8 +53,7 @@ def print_help(self, file=None): def error(self, message, bailout=True): self.exit(2, f'{self.prog}: error: {message}\n', bailout=bailout) - def exit(self, status=0, message=None, - bailout=True): + def exit(self, status=0, message=None, bailout=True): self.exit_status = status if message: self._print_message(message, file=self._host.stderr) diff --git a/json5/lib.py b/json5/lib.py index 3c74011..7e7e081 100644 --- a/json5/lib.py +++ b/json5/lib.py @@ -14,24 +14,36 @@ import math import re -from typing import Any, Callable, IO, Iterable, Mapping, Optional, \ - Set, Tuple, Union +from typing import ( + Any, + Callable, + IO, + Iterable, + Mapping, + Optional, + Set, + Tuple, + Union, +) import unicodedata from .parser import Parser -def load(fp : IO, - *, - encoding: Optional[str] = None, - cls: None = None, - object_hook: Optional[Callable[[Mapping[str, Any]], Any]] = None, - parse_float: Optional[Callable[[str], Any]] = None, - parse_int: Optional[Callable[[str], Any]] = None, - parse_constant: Optional[Callable[[str], Any]] = None, - object_pairs_hook: - Optional[Callable[[Iterable[Tuple[str, Any]]], Any]] = None, - allow_duplicate_keys: bool = True) -> Any: +def load( + fp: IO, + *, + encoding: Optional[str] = None, + cls: None = None, + object_hook: Optional[Callable[[Mapping[str, Any]], Any]] = None, + parse_float: Optional[Callable[[str], Any]] = None, + parse_int: Optional[Callable[[str], Any]] = None, + parse_constant: Optional[Callable[[str], Any]] = None, + object_pairs_hook: Optional[ + Callable[[Iterable[Tuple[str, Any]]], Any] + ] = None, + allow_duplicate_keys: bool = True, +) -> Any: """Deserialize ``fp`` (a ``.read()``-supporting file-like object containing a JSON document) to a Python object. @@ -44,24 +56,33 @@ def load(fp : IO, """ s = fp.read() - return loads(s, encoding=encoding, cls=cls, object_hook=object_hook, - parse_float=parse_float, parse_int=parse_int, - parse_constant=parse_constant, - object_pairs_hook=object_pairs_hook, - allow_duplicate_keys=allow_duplicate_keys) - - -def loads(s: str, - *, - encoding: Optional[str] = None, - cls: None = None, - object_hook: Optional[Callable[[Mapping[str, Any]], Any]] = None, - parse_float: Optional[Callable[[str], Any]] = None, - parse_int: Optional[Callable[[str], Any]] = None, - parse_constant: Optional[Callable[[str], Any]] = None, - object_pairs_hook: - Optional[Callable[[Iterable[Tuple[str, Any]]], Any]] = None, - allow_duplicate_keys: bool = True): + return loads( + s, + encoding=encoding, + cls=cls, + object_hook=object_hook, + parse_float=parse_float, + parse_int=parse_int, + parse_constant=parse_constant, + object_pairs_hook=object_pairs_hook, + allow_duplicate_keys=allow_duplicate_keys, + ) + + +def loads( + s: str, + *, + encoding: Optional[str] = None, + cls: None = None, + object_hook: Optional[Callable[[Mapping[str, Any]], Any]] = None, + parse_float: Optional[Callable[[str], Any]] = None, + parse_int: Optional[Callable[[str], Any]] = None, + parse_constant: Optional[Callable[[str], Any]] = None, + object_pairs_hook: Optional[ + Callable[[Iterable[Tuple[str, Any]]], Any] + ] = None, + allow_duplicate_keys: bool = True, +): """Deserialize ``s`` (a string containing a JSON5 document) to a Python object. @@ -92,6 +113,7 @@ def _fp_constant_parser(s): if object_pairs_hook: dictify = object_pairs_hook elif object_hook: + def dictify(pairs): return object_hook(dict(pairs)) else: @@ -99,6 +121,7 @@ def dictify(pairs): if not allow_duplicate_keys: _orig_dictify = dictify + def dictify(pairs): return _reject_duplicate_keys(pairs, _orig_dictify) @@ -118,9 +141,13 @@ def _reject_duplicate_keys(pairs, dictify): return dictify(pairs) -def _walk_ast(el, - dictify: Callable[[Iterable[Tuple[str, Any]]], Any], - parse_float, parse_int, parse_constant): +def _walk_ast( + el, + dictify: Callable[[Iterable[Tuple[str, Any]]], Any], + parse_float, + parse_int, + parse_constant, +): if el == 'None': return None if el == 'True': @@ -141,32 +168,37 @@ def _walk_ast(el, if ty == 'object': pairs = [] for key, val_expr in v: - val = _walk_ast(val_expr, dictify, parse_float, parse_int, - parse_constant) + val = _walk_ast( + val_expr, dictify, parse_float, parse_int, parse_constant + ) pairs.append((key, val)) return dictify(pairs) if ty == 'array': - return [_walk_ast(el, dictify, parse_float, parse_int, parse_constant) - for el in v] + return [ + _walk_ast(el, dictify, parse_float, parse_int, parse_constant) + for el in v + ] raise ValueError('unknown el: ' + el) # pragma: no cover -def dump(obj: Any, - fp: IO, - *, - skipkeys: bool = False, - ensure_ascii: bool = True, - check_circular: bool =True, - allow_nan: bool = True, - cls: None = None, - indent: Optional[Union[int, str]] = None, - separators: Optional[Tuple[str, str]] = None, - default: Optional[Callable[[Any], Any]] = None, - sort_keys: bool = False, - quote_keys: bool = False, - trailing_commas: bool = True, - allow_duplicate_keys: bool = True, - **kwargs): +def dump( + obj: Any, + fp: IO, + *, + skipkeys: bool = False, + ensure_ascii: bool = True, + check_circular: bool = True, + allow_nan: bool = True, + cls: None = None, + indent: Optional[Union[int, str]] = None, + separators: Optional[Tuple[str, str]] = None, + default: Optional[Callable[[Any], Any]] = None, + sort_keys: bool = False, + quote_keys: bool = False, + trailing_commas: bool = True, + allow_duplicate_keys: bool = True, + **kwargs, +): """Serialize ``obj`` to a JSON5-formatted stream to ``fp``, a ``.write()``-supporting file-like object. @@ -204,29 +236,42 @@ def dump(obj: Any, """ del kwargs - fp.write(dumps(obj=obj, skipkeys=skipkeys, ensure_ascii=ensure_ascii, - check_circular=check_circular, allow_nan=allow_nan, - cls=cls, indent=indent, separators=separators, - default=default, sort_keys=sort_keys, - quote_keys=quote_keys, trailing_commas=trailing_commas, - allow_duplicate_keys=allow_duplicate_keys)) - - -def dumps(obj: Any, - *, - skipkeys: bool = False, - ensure_ascii: bool = True, - check_circular: bool =True, - allow_nan: bool = True, - cls: None = None, - indent: Optional[Union[int, str]] = None, - separators: Optional[Tuple[str, str]] = None, - default: Optional[Callable[[Any], Any]] = None, - sort_keys: bool = False, - quote_keys: bool = False, - trailing_commas: bool = True, - allow_duplicate_keys: bool = True, - **kwargs): + fp.write( + dumps( + obj=obj, + skipkeys=skipkeys, + ensure_ascii=ensure_ascii, + check_circular=check_circular, + allow_nan=allow_nan, + cls=cls, + indent=indent, + separators=separators, + default=default, + sort_keys=sort_keys, + quote_keys=quote_keys, + trailing_commas=trailing_commas, + allow_duplicate_keys=allow_duplicate_keys, + ) + ) + + +def dumps( + obj: Any, + *, + skipkeys: bool = False, + ensure_ascii: bool = True, + check_circular: bool = True, + allow_nan: bool = True, + cls: None = None, + indent: Optional[Union[int, str]] = None, + separators: Optional[Tuple[str, str]] = None, + default: Optional[Callable[[Any], Any]] = None, + sort_keys: bool = False, + quote_keys: bool = False, + trailing_commas: bool = True, + allow_duplicate_keys: bool = True, + **kwargs, +): """Serialize ``obj`` to a JSON5-formatted string. Supports the same arguments as ``json.dumps()``, except that: @@ -281,19 +326,43 @@ def dumps(obj: Any, level = 1 is_key = False - _, v = _dumps(obj, skipkeys, ensure_ascii, check_circular, - allow_nan, indent, separators, default, sort_keys, - quote_keys, trailing_commas, allow_duplicate_keys, - seen, level, is_key) + _, v = _dumps( + obj, + skipkeys, + ensure_ascii, + check_circular, + allow_nan, + indent, + separators, + default, + sort_keys, + quote_keys, + trailing_commas, + allow_duplicate_keys, + seen, + level, + is_key, + ) return v -def _dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, indent, - separators, default, sort_keys, - quote_keys, trailing_commas, allow_duplicate_keys, - seen: Optional[Set[int]], - level: int, - is_key: bool): +def _dumps( + obj, + skipkeys, + ensure_ascii, + check_circular, + allow_nan, + indent, + separators, + default, + sort_keys, + quote_keys, + trailing_commas, + allow_duplicate_keys, + seen: Optional[Set[int]], + level: int, + is_key: bool, +): # pylint: disable=too-many-statements if obj is True: s = 'true' @@ -317,8 +386,12 @@ def _dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, indent, else: raise ValueError() elif isinstance(obj, str): - if (is_key and _is_ident(obj) and not quote_keys - and not _is_reserved_word(obj)): + if ( + is_key + and _is_ident(obj) + and not quote_keys + and not _is_reserved_word(obj) + ): return True, obj return True, _dump_str(obj, ensure_ascii) elif isinstance(obj, int): @@ -375,36 +448,90 @@ def _dumps(obj, skipkeys, ensure_ascii, check_circular, allow_nan, indent, # here, but for backwards-compatibility with potential old callers, # we only check for the two attributes we need in each case. if hasattr(obj, 'keys') and hasattr(obj, '__getitem__'): - s = _dump_dict(obj, skipkeys, ensure_ascii, - check_circular, allow_nan, indent, - separators, default, sort_keys, - quote_keys, trailing_commas, - allow_duplicate_keys, seen, level + 1, - item_sep, kv_sep, indent_str, end_str) + s = _dump_dict( + obj, + skipkeys, + ensure_ascii, + check_circular, + allow_nan, + indent, + separators, + default, + sort_keys, + quote_keys, + trailing_commas, + allow_duplicate_keys, + seen, + level + 1, + item_sep, + kv_sep, + indent_str, + end_str, + ) elif hasattr(obj, '__getitem__') and hasattr(obj, '__iter__'): - s = _dump_array(obj, skipkeys, ensure_ascii, - check_circular, allow_nan, indent, - separators, default, sort_keys, - quote_keys, trailing_commas, - allow_duplicate_keys, seen, level + 1, - item_sep, indent_str, end_str) + s = _dump_array( + obj, + skipkeys, + ensure_ascii, + check_circular, + allow_nan, + indent, + separators, + default, + sort_keys, + quote_keys, + trailing_commas, + allow_duplicate_keys, + seen, + level + 1, + item_sep, + indent_str, + end_str, + ) else: - s = _dumps(default(obj), skipkeys, ensure_ascii, - check_circular, allow_nan, indent, - separators, default, sort_keys, - quote_keys, trailing_commas, - allow_duplicate_keys, seen, level, - is_key)[1] + s = _dumps( + default(obj), + skipkeys, + ensure_ascii, + check_circular, + allow_nan, + indent, + separators, + default, + sort_keys, + quote_keys, + trailing_commas, + allow_duplicate_keys, + seen, + level, + is_key, + )[1] if seen is not None: seen.remove(i) return False, s -def _dump_dict(obj, skipkeys, ensure_ascii, check_circular, allow_nan, - indent, separators, default, sort_keys, - quote_keys, trailing_commas, allow_duplicate_keys, - seen, level, item_sep, kv_sep, indent_str, end_str): +def _dump_dict( + obj, + skipkeys, + ensure_ascii, + check_circular, + allow_nan, + indent, + separators, + default, + sort_keys, + quote_keys, + trailing_commas, + allow_duplicate_keys, + seen, + level, + item_sep, + kv_sep, + indent_str, + end_str, +): if not obj: return '{}' @@ -418,12 +545,23 @@ def _dump_dict(obj, skipkeys, ensure_ascii, check_circular, allow_nan, num_items_added = 0 new_keys = set() for key in keys: - valid_key, key_str = _dumps(key, skipkeys, ensure_ascii, check_circular, - allow_nan, indent, separators, default, - sort_keys, - quote_keys, trailing_commas, - allow_duplicate_keys, - seen, level, is_key=True) + valid_key, key_str = _dumps( + key, + skipkeys, + ensure_ascii, + check_circular, + allow_nan, + indent, + separators, + default, + sort_keys, + quote_keys, + trailing_commas, + allow_duplicate_keys, + seen, + level, + is_key=True, + ) if valid_key: if not allow_duplicate_keys: if key_str in new_keys: @@ -431,12 +569,27 @@ def _dump_dict(obj, skipkeys, ensure_ascii, check_circular, allow_nan, new_keys.add(key_str) if num_items_added: s += item_sep - s += key_str + kv_sep + _dumps(obj[key], skipkeys, ensure_ascii, - check_circular, allow_nan, indent, - separators, default, sort_keys, - quote_keys, trailing_commas, - allow_duplicate_keys, - seen, level, is_key=False)[1] + s += ( + key_str + + kv_sep + + _dumps( + obj[key], + skipkeys, + ensure_ascii, + check_circular, + allow_nan, + indent, + separators, + default, + sort_keys, + quote_keys, + trailing_commas, + allow_duplicate_keys, + seen, + level, + is_key=False, + )[1] + ) num_items_added += 1 elif not skipkeys: raise TypeError(f'invalid key {repr(key)}') @@ -445,19 +598,55 @@ def _dump_dict(obj, skipkeys, ensure_ascii, check_circular, allow_nan, return s -def _dump_array(obj, skipkeys, ensure_ascii, check_circular, allow_nan, - indent, separators, default, sort_keys, - quote_keys, trailing_commas, allow_duplicate_keys, - seen, level, item_sep, indent_str, end_str): +def _dump_array( + obj, + skipkeys, + ensure_ascii, + check_circular, + allow_nan, + indent, + separators, + default, + sort_keys, + quote_keys, + trailing_commas, + allow_duplicate_keys, + seen, + level, + item_sep, + indent_str, + end_str, +): if not obj: return '[]' - return ('[' + indent_str + - item_sep.join([_dumps(el, skipkeys, ensure_ascii, check_circular, - allow_nan, indent, separators, default, - sort_keys, quote_keys, trailing_commas, - allow_duplicate_keys, - seen, level, False)[1] for el in obj]) + - end_str + ']') + return ( + '[' + + indent_str + + item_sep.join( + [ + _dumps( + el, + skipkeys, + ensure_ascii, + check_circular, + allow_nan, + indent, + separators, + default, + sort_keys, + quote_keys, + trailing_commas, + allow_duplicate_keys, + seen, + level, + False, + )[1] + for el in obj + ] + ) + + end_str + + ']' + ) def _dump_str(obj, ensure_ascii): @@ -495,8 +684,8 @@ def _dump_str(obj, ensure_ascii): ret.append(f'\\u{o:04x}') else: val = o - 0x10000 - high = 0xd800 + (val >> 10) - low = 0xdc00 + (val & 0x3ff) + high = 0xD800 + (val >> 10) + low = 0xDC00 + (val & 0x3FF) ret.append(f'\\u{high:04x}\\u{low:04x}') return ''.join(ret) + '"' @@ -512,59 +701,84 @@ def _is_ident(k): def _is_id_start(ch): return unicodedata.category(ch) in ( - 'Lu', 'Ll', 'Li', 'Lt', 'Lm', 'Lo', 'Nl') + 'Lu', + 'Ll', + 'Li', + 'Lt', + 'Lm', + 'Lo', + 'Nl', + ) def _is_id_continue(ch): return unicodedata.category(ch) in ( - 'Lu', 'Ll', 'Li', 'Lt', 'Lm', 'Lo', 'Nl', 'Nd', 'Mn', 'Mc', 'Pc') + 'Lu', + 'Ll', + 'Li', + 'Lt', + 'Lm', + 'Lo', + 'Nl', + 'Nd', + 'Mn', + 'Mc', + 'Pc', + ) _reserved_word_re = None + def _is_reserved_word(k): global _reserved_word_re if _reserved_word_re is None: # List taken from section 7.6.1 of ECMA-262. - _reserved_word_re = re.compile('(' + '|'.join([ - 'break', - 'case', - 'catch', - 'class', - 'const', - 'continue', - 'debugger', - 'default', - 'delete', - 'do', - 'else', - 'enum', - 'export', - 'extends', - 'false', - 'finally', - 'for', - 'function', - 'if', - 'import', - 'in', - 'instanceof', - 'new', - 'null', - 'return', - 'super', - 'switch', - 'this', - 'throw', - 'true', - 'try', - 'typeof', - 'var', - 'void', - 'while', - 'with', - ]) + ')$') + _reserved_word_re = re.compile( + '(' + + '|'.join( + [ + 'break', + 'case', + 'catch', + 'class', + 'const', + 'continue', + 'debugger', + 'default', + 'delete', + 'do', + 'else', + 'enum', + 'export', + 'extends', + 'false', + 'finally', + 'for', + 'function', + 'if', + 'import', + 'in', + 'instanceof', + 'new', + 'null', + 'return', + 'super', + 'switch', + 'this', + 'throw', + 'true', + 'try', + 'typeof', + 'var', + 'void', + 'while', + 'with', + ] + ) + + ')$' + ) return _reserved_word_re.match(k) is not None diff --git a/json5/parser.py b/json5/parser.py index 1f18ef9..4daa2a1 100644 --- a/json5/parser.py +++ b/json5/parser.py @@ -157,17 +157,34 @@ def _xtou(self, s): def _grammar_(self): self._push('grammar') - self._seq([self._sp_, lambda: self._bind(self._value_, 'v'), self._sp_, - self._end_, lambda: self._succeed(self._get('v'))]) + self._seq( + [ + self._sp_, + lambda: self._bind(self._value_, 'v'), + self._sp_, + self._end_, + lambda: self._succeed(self._get('v')), + ] + ) self._pop('grammar') def _sp_(self): self._star(self._ws_) def _ws_(self): - self._choose([self._ws__c0_, self._eol_, self._comment_, self._ws__c3_, - self._ws__c4_, self._ws__c5_, self._ws__c6_, - self._ws__c7_, self._ws__c8_]) + self._choose( + [ + self._ws__c0_, + self._eol_, + self._comment_, + self._ws__c3_, + self._ws__c4_, + self._ws__c5_, + self._ws__c6_, + self._ws__c7_, + self._ws__c8_, + ] + ) def _ws__c0_(self): self._ch(' ') @@ -189,8 +206,13 @@ def _ws__c7_(self): def _ws__c8_(self): self._push('ws__c8') - self._seq([self._ws__c8__s0_, lambda: self._bind(self._anything_, 'x'), - lambda: self._succeed(self._get('x'))]) + self._seq( + [ + self._ws__c8__s0_, + lambda: self._bind(self._anything_, 'x'), + lambda: self._succeed(self._get('x')), + ] + ) self._pop('ws__c8') def _ws__c8__s0_(self): @@ -200,8 +222,12 @@ def _ws__c8__s0_n_n_(self): self._choose([self._ws__c8__s0_n_n_g__c0_]) def _ws__c8__s0_n_n_g__c0_(self): - self._seq([lambda: self._bind(self._anything_, 'x'), - self._ws__c8__s0_n_n_g__c0__s1_]) + self._seq( + [ + lambda: self._bind(self._anything_, 'x'), + self._ws__c8__s0_n_n_g__c0__s1_, + ] + ) def _ws__c8__s0_n_n_g__c0__s1_(self): v = self._is_unicat(self._get('x'), 'Zs') @@ -211,8 +237,15 @@ def _ws__c8__s0_n_n_g__c0__s1_(self): self._fail() def _eol_(self): - self._choose([self._eol__c0_, self._eol__c1_, self._eol__c2_, - self._eol__c3_, self._eol__c4_]) + self._choose( + [ + self._eol__c0_, + self._eol__c1_, + self._eol__c2_, + self._eol__c3_, + self._eol__c4_, + ] + ) def _eol__c0_(self): self._seq([lambda: self._ch('\r'), lambda: self._ch('\n')]) @@ -233,26 +266,45 @@ def _comment_(self): self._choose([self._comment__c0_, self._comment__c1_]) def _comment__c0_(self): - self._seq([lambda: self._str('//'), - lambda: self._star(self._comment__c0__s1_p_)]) + self._seq( + [ + lambda: self._str('//'), + lambda: self._star(self._comment__c0__s1_p_), + ] + ) def _comment__c0__s1_p_(self): self._seq([lambda: self._not(self._eol_), self._anything_]) def _comment__c1_(self): - self._seq([lambda: self._str('/*'), self._comment__c1__s1_, - lambda: self._str('*/')]) + self._seq( + [ + lambda: self._str('/*'), + self._comment__c1__s1_, + lambda: self._str('*/'), + ] + ) def _comment__c1__s1_(self): - self._star(lambda: self._seq([self._comment__c1__s1_p__s0_, self._anything_])) + self._star( + lambda: self._seq([self._comment__c1__s1_p__s0_, self._anything_]) + ) def _comment__c1__s1_p__s0_(self): self._not(lambda: self._str('*/')) def _value_(self): - self._choose([self._value__c0_, self._value__c1_, self._value__c2_, - self._value__c3_, self._value__c4_, self._value__c5_, - self._value__c6_]) + self._choose( + [ + self._value__c0_, + self._value__c1_, + self._value__c2_, + self._value__c3_, + self._value__c4_, + self._value__c5_, + self._value__c6_, + ] + ) def _value__c0_(self): self._seq([lambda: self._str('null'), lambda: self._succeed('None')]) @@ -265,26 +317,42 @@ def _value__c2_(self): def _value__c3_(self): self._push('value__c3') - self._seq([lambda: self._bind(self._object_, 'v'), - lambda: self._succeed(['object', self._get('v')])]) + self._seq( + [ + lambda: self._bind(self._object_, 'v'), + lambda: self._succeed(['object', self._get('v')]), + ] + ) self._pop('value__c3') def _value__c4_(self): self._push('value__c4') - self._seq([lambda: self._bind(self._array_, 'v'), - lambda: self._succeed(['array', self._get('v')])]) + self._seq( + [ + lambda: self._bind(self._array_, 'v'), + lambda: self._succeed(['array', self._get('v')]), + ] + ) self._pop('value__c4') def _value__c5_(self): self._push('value__c5') - self._seq([lambda: self._bind(self._string_, 'v'), - lambda: self._succeed(['string', self._get('v')])]) + self._seq( + [ + lambda: self._bind(self._string_, 'v'), + lambda: self._succeed(['string', self._get('v')]), + ] + ) self._pop('value__c5') def _value__c6_(self): self._push('value__c6') - self._seq([lambda: self._bind(self._num_literal_, 'v'), - lambda: self._succeed(['number', self._get('v')])]) + self._seq( + [ + lambda: self._bind(self._num_literal_, 'v'), + lambda: self._succeed(['number', self._get('v')]), + ] + ) self._pop('value__c6') def _object_(self): @@ -292,36 +360,68 @@ def _object_(self): def _object__c0_(self): self._push('object__c0') - self._seq([lambda: self._ch('{'), self._sp_, - lambda: self._bind(self._member_list_, 'v'), self._sp_, - lambda: self._ch('}'), lambda: self._succeed(self._get('v'))]) + self._seq( + [ + lambda: self._ch('{'), + self._sp_, + lambda: self._bind(self._member_list_, 'v'), + self._sp_, + lambda: self._ch('}'), + lambda: self._succeed(self._get('v')), + ] + ) self._pop('object__c0') def _object__c1_(self): - self._seq([lambda: self._ch('{'), self._sp_, lambda: self._ch('}'), - lambda: self._succeed([])]) + self._seq( + [ + lambda: self._ch('{'), + self._sp_, + lambda: self._ch('}'), + lambda: self._succeed([]), + ] + ) def _array_(self): self._choose([self._array__c0_, self._array__c1_]) def _array__c0_(self): self._push('array__c0') - self._seq([lambda: self._ch('['), self._sp_, - lambda: self._bind(self._element_list_, 'v'), self._sp_, - lambda: self._ch(']'), lambda: self._succeed(self._get('v'))]) + self._seq( + [ + lambda: self._ch('['), + self._sp_, + lambda: self._bind(self._element_list_, 'v'), + self._sp_, + lambda: self._ch(']'), + lambda: self._succeed(self._get('v')), + ] + ) self._pop('array__c0') def _array__c1_(self): - self._seq([lambda: self._ch('['), self._sp_, lambda: self._ch(']'), - lambda: self._succeed([])]) + self._seq( + [ + lambda: self._ch('['), + self._sp_, + lambda: self._ch(']'), + lambda: self._succeed([]), + ] + ) def _string_(self): self._choose([self._string__c0_, self._string__c1_]) def _string__c0_(self): self._push('string__c0') - self._seq([self._squote_, self._string__c0__s1_, self._squote_, - lambda: self._succeed(self._join('', self._get('cs')))]) + self._seq( + [ + self._squote_, + self._string__c0__s1_, + self._squote_, + lambda: self._succeed(self._join('', self._get('cs'))), + ] + ) self._pop('string__c0') def _string__c0__s1_(self): @@ -329,8 +429,14 @@ def _string__c0__s1_(self): def _string__c1_(self): self._push('string__c1') - self._seq([self._dquote_, self._string__c1__s1_, self._dquote_, - lambda: self._succeed(self._join('', self._get('cs')))]) + self._seq( + [ + self._dquote_, + self._string__c1__s1_, + self._dquote_, + lambda: self._succeed(self._join('', self._get('cs'))), + ] + ) self._pop('string__c1') def _string__c1__s1_(self): @@ -341,8 +447,13 @@ def _sqchar_(self): def _sqchar__c0_(self): self._push('sqchar__c0') - self._seq([self._bslash_, lambda: self._bind(self._esc_char_, 'c'), - lambda: self._succeed(self._get('c'))]) + self._seq( + [ + self._bslash_, + lambda: self._bind(self._esc_char_, 'c'), + lambda: self._succeed(self._get('c')), + ] + ) self._pop('sqchar__c0') def _sqchar__c1_(self): @@ -350,11 +461,15 @@ def _sqchar__c1_(self): def _sqchar__c2_(self): self._push('sqchar__c2') - self._seq([lambda: self._not(self._bslash_), - lambda: self._not(self._squote_), - lambda: self._not(self._eol_), - lambda: self._bind(self._anything_, 'c'), - lambda: self._succeed(self._get('c'))]) + self._seq( + [ + lambda: self._not(self._bslash_), + lambda: self._not(self._squote_), + lambda: self._not(self._eol_), + lambda: self._bind(self._anything_, 'c'), + lambda: self._succeed(self._get('c')), + ] + ) self._pop('sqchar__c2') def _dqchar_(self): @@ -362,8 +477,13 @@ def _dqchar_(self): def _dqchar__c0_(self): self._push('dqchar__c0') - self._seq([self._bslash_, lambda: self._bind(self._esc_char_, 'c'), - lambda: self._succeed(self._get('c'))]) + self._seq( + [ + self._bslash_, + lambda: self._bind(self._esc_char_, 'c'), + lambda: self._succeed(self._get('c')), + ] + ) self._pop('dqchar__c0') def _dqchar__c1_(self): @@ -371,11 +491,15 @@ def _dqchar__c1_(self): def _dqchar__c2_(self): self._push('dqchar__c2') - self._seq([lambda: self._not(self._bslash_), - lambda: self._not(self._dquote_), - lambda: self._not(self._eol_), - lambda: self._bind(self._anything_, 'c'), - lambda: self._succeed(self._get('c'))]) + self._seq( + [ + lambda: self._not(self._bslash_), + lambda: self._not(self._dquote_), + lambda: self._not(self._eol_), + lambda: self._bind(self._anything_, 'c'), + lambda: self._succeed(self._get('c')), + ] + ) self._pop('dqchar__c2') def _bslash_(self): @@ -388,13 +512,23 @@ def _dquote_(self): self._ch('"') def _esc_char_(self): - self._choose([self._esc_char__c0_, self._esc_char__c1_, - self._esc_char__c2_, self._esc_char__c3_, - self._esc_char__c4_, self._esc_char__c5_, - self._esc_char__c6_, self._esc_char__c7_, - self._esc_char__c8_, self._esc_char__c9_, - self._esc_char__c10_, self._esc_char__c11_, - self._esc_char__c12_]) + self._choose( + [ + self._esc_char__c0_, + self._esc_char__c1_, + self._esc_char__c2_, + self._esc_char__c3_, + self._esc_char__c4_, + self._esc_char__c5_, + self._esc_char__c6_, + self._esc_char__c7_, + self._esc_char__c8_, + self._esc_char__c9_, + self._esc_char__c10_, + self._esc_char__c11_, + self._esc_char__c12_, + ] + ) def _esc_char__c0_(self): self._seq([lambda: self._ch('b'), lambda: self._succeed('\b')]) @@ -403,19 +537,32 @@ def _esc_char__c1_(self): self._seq([lambda: self._ch('f'), lambda: self._succeed('\f')]) def _esc_char__c10_(self): - self._seq([lambda: self._ch('0'), lambda: self._not(self._digit_), - lambda: self._succeed('\x00')]) + self._seq( + [ + lambda: self._ch('0'), + lambda: self._not(self._digit_), + lambda: self._succeed('\x00'), + ] + ) def _esc_char__c11_(self): self._push('esc_char__c11') - self._seq([lambda: self._bind(self._hex_esc_, 'c'), - lambda: self._succeed(self._get('c'))]) + self._seq( + [ + lambda: self._bind(self._hex_esc_, 'c'), + lambda: self._succeed(self._get('c')), + ] + ) self._pop('esc_char__c11') def _esc_char__c12_(self): self._push('esc_char__c12') - self._seq([lambda: self._bind(self._unicode_esc_, 'c'), - lambda: self._succeed(self._get('c'))]) + self._seq( + [ + lambda: self._bind(self._unicode_esc_, 'c'), + lambda: self._succeed(self._get('c')), + ] + ) self._pop('esc_char__c12') def _esc_char__c2_(self): @@ -441,19 +588,27 @@ def _esc_char__c8_(self): def _esc_char__c9_(self): self._push('esc_char__c9') - self._seq([self._esc_char__c9__s0_, - lambda: self._bind(self._anything_, 'c'), - lambda: self._succeed(self._get('c'))]) + self._seq( + [ + self._esc_char__c9__s0_, + lambda: self._bind(self._anything_, 'c'), + lambda: self._succeed(self._get('c')), + ] + ) self._pop('esc_char__c9') def _esc_char__c9__s0_(self): self._not(lambda: (self._esc_char__c9__s0_n_g_)()) def _esc_char__c9__s0_n_g_(self): - self._choose([self._esc_char__c9__s0_n_g__c0_, - self._esc_char__c9__s0_n_g__c1_, - lambda: self._seq([self._digit_]), - lambda: self._seq([self._eol_])]) + self._choose( + [ + self._esc_char__c9__s0_n_g__c0_, + self._esc_char__c9__s0_n_g__c1_, + lambda: self._seq([self._digit_]), + lambda: self._seq([self._eol_]), + ] + ) def _esc_char__c9__s0_n_g__c0_(self): self._seq([lambda: self._ch('x')]) @@ -463,25 +618,50 @@ def _esc_char__c9__s0_n_g__c1_(self): def _hex_esc_(self): self._push('hex_esc') - self._seq([lambda: self._ch('x'), lambda: self._bind(self._hex_, 'h1'), - lambda: self._bind(self._hex_, 'h2'), - lambda: self._succeed(self._xtou(self._get('h1') + self._get('h2')))]) + self._seq( + [ + lambda: self._ch('x'), + lambda: self._bind(self._hex_, 'h1'), + lambda: self._bind(self._hex_, 'h2'), + lambda: self._succeed( + self._xtou(self._get('h1') + self._get('h2')) + ), + ] + ) self._pop('hex_esc') def _unicode_esc_(self): self._push('unicode_esc') - self._seq([lambda: self._ch('u'), lambda: self._bind(self._hex_, 'a'), - lambda: self._bind(self._hex_, 'b'), - lambda: self._bind(self._hex_, 'c'), - lambda: self._bind(self._hex_, 'd'), - lambda: self._succeed(self._xtou(self._get('a') + self._get('b') + self._get('c') + self._get('d')))]) + self._seq( + [ + lambda: self._ch('u'), + lambda: self._bind(self._hex_, 'a'), + lambda: self._bind(self._hex_, 'b'), + lambda: self._bind(self._hex_, 'c'), + lambda: self._bind(self._hex_, 'd'), + lambda: self._succeed( + self._xtou( + self._get('a') + + self._get('b') + + self._get('c') + + self._get('d') + ) + ), + ] + ) self._pop('unicode_esc') def _element_list_(self): self._push('element_list') - self._seq([lambda: self._bind(self._value_, 'v'), - self._element_list__s1_, self._sp_, self._element_list__s3_, - lambda: self._succeed([self._get('v')] + self._get('vs'))]) + self._seq( + [ + lambda: self._bind(self._value_, 'v'), + self._element_list__s1_, + self._sp_, + self._element_list__s3_, + lambda: self._succeed([self._get('v')] + self._get('vs')), + ] + ) self._pop('element_list') def _element_list__s1_(self): @@ -495,9 +675,15 @@ def _element_list__s3_(self): def _member_list_(self): self._push('member_list') - self._seq([lambda: self._bind(self._member_, 'm'), - self._member_list__s1_, self._sp_, self._member_list__s3_, - lambda: self._succeed([self._get('m')] + self._get('ms'))]) + self._seq( + [ + lambda: self._bind(self._member_, 'm'), + self._member_list__s1_, + self._sp_, + self._member_list__s3_, + lambda: self._succeed([self._get('m')] + self._get('ms')), + ] + ) self._pop('member_list') def _member_list__s1_(self): @@ -514,39 +700,65 @@ def _member_(self): def _member__c0_(self): self._push('member__c0') - self._seq([lambda: self._bind(self._string_, 'k'), self._sp_, - lambda: self._ch(':'), self._sp_, - lambda: self._bind(self._value_, 'v'), - lambda: self._succeed([self._get('k'), self._get('v')])]) + self._seq( + [ + lambda: self._bind(self._string_, 'k'), + self._sp_, + lambda: self._ch(':'), + self._sp_, + lambda: self._bind(self._value_, 'v'), + lambda: self._succeed([self._get('k'), self._get('v')]), + ] + ) self._pop('member__c0') def _member__c1_(self): self._push('member__c1') - self._seq([lambda: self._bind(self._ident_, 'k'), self._sp_, - lambda: self._ch(':'), self._sp_, - lambda: self._bind(self._value_, 'v'), - lambda: self._succeed([self._get('k'), self._get('v')])]) + self._seq( + [ + lambda: self._bind(self._ident_, 'k'), + self._sp_, + lambda: self._ch(':'), + self._sp_, + lambda: self._bind(self._value_, 'v'), + lambda: self._succeed([self._get('k'), self._get('v')]), + ] + ) self._pop('member__c1') def _ident_(self): self._push('ident') - self._seq([lambda: self._bind(self._id_start_, 'hd'), self._ident__s1_, - lambda: self._succeed(self._join('', [self._get('hd')] + self._get('tl')))]) + self._seq( + [ + lambda: self._bind(self._id_start_, 'hd'), + self._ident__s1_, + lambda: self._succeed( + self._join('', [self._get('hd')] + self._get('tl')) + ), + ] + ) self._pop('ident') def _ident__s1_(self): self._bind(lambda: self._star(self._id_continue_), 'tl') def _id_start_(self): - self._choose([self._ascii_id_start_, self._other_id_start_, - self._id_start__c2_]) + self._choose( + [self._ascii_id_start_, self._other_id_start_, self._id_start__c2_] + ) def _id_start__c2_(self): self._seq([self._bslash_, self._unicode_esc_]) def _ascii_id_start_(self): - self._choose([self._ascii_id_start__c0_, self._ascii_id_start__c1_, - self._ascii_id_start__c2_, self._ascii_id_start__c3_]) + self._choose( + [ + self._ascii_id_start__c0_, + self._ascii_id_start__c1_, + self._ascii_id_start__c2_, + self._ascii_id_start__c3_, + ] + ) def _ascii_id_start__c0_(self): self._range('a', 'z') @@ -561,15 +773,26 @@ def _ascii_id_start__c3_(self): self._ch('_') def _other_id_start_(self): - self._choose([self._other_id_start__c0_, self._other_id_start__c1_, - self._other_id_start__c2_, self._other_id_start__c3_, - self._other_id_start__c4_, self._other_id_start__c5_]) + self._choose( + [ + self._other_id_start__c0_, + self._other_id_start__c1_, + self._other_id_start__c2_, + self._other_id_start__c3_, + self._other_id_start__c4_, + self._other_id_start__c5_, + ] + ) def _other_id_start__c0_(self): self._push('other_id_start__c0') - self._seq([lambda: self._bind(self._anything_, 'x'), - self._other_id_start__c0__s1_, - lambda: self._succeed(self._get('x'))]) + self._seq( + [ + lambda: self._bind(self._anything_, 'x'), + self._other_id_start__c0__s1_, + lambda: self._succeed(self._get('x')), + ] + ) self._pop('other_id_start__c0') def _other_id_start__c0__s1_(self): @@ -581,9 +804,13 @@ def _other_id_start__c0__s1_(self): def _other_id_start__c1_(self): self._push('other_id_start__c1') - self._seq([lambda: self._bind(self._anything_, 'x'), - self._other_id_start__c1__s1_, - lambda: self._succeed(self._get('x'))]) + self._seq( + [ + lambda: self._bind(self._anything_, 'x'), + self._other_id_start__c1__s1_, + lambda: self._succeed(self._get('x')), + ] + ) self._pop('other_id_start__c1') def _other_id_start__c1__s1_(self): @@ -595,9 +822,13 @@ def _other_id_start__c1__s1_(self): def _other_id_start__c2_(self): self._push('other_id_start__c2') - self._seq([lambda: self._bind(self._anything_, 'x'), - self._other_id_start__c2__s1_, - lambda: self._succeed(self._get('x'))]) + self._seq( + [ + lambda: self._bind(self._anything_, 'x'), + self._other_id_start__c2__s1_, + lambda: self._succeed(self._get('x')), + ] + ) self._pop('other_id_start__c2') def _other_id_start__c2__s1_(self): @@ -609,9 +840,13 @@ def _other_id_start__c2__s1_(self): def _other_id_start__c3_(self): self._push('other_id_start__c3') - self._seq([lambda: self._bind(self._anything_, 'x'), - self._other_id_start__c3__s1_, - lambda: self._succeed(self._get('x'))]) + self._seq( + [ + lambda: self._bind(self._anything_, 'x'), + self._other_id_start__c3__s1_, + lambda: self._succeed(self._get('x')), + ] + ) self._pop('other_id_start__c3') def _other_id_start__c3__s1_(self): @@ -623,9 +858,13 @@ def _other_id_start__c3__s1_(self): def _other_id_start__c4_(self): self._push('other_id_start__c4') - self._seq([lambda: self._bind(self._anything_, 'x'), - self._other_id_start__c4__s1_, - lambda: self._succeed(self._get('x'))]) + self._seq( + [ + lambda: self._bind(self._anything_, 'x'), + self._other_id_start__c4__s1_, + lambda: self._succeed(self._get('x')), + ] + ) self._pop('other_id_start__c4') def _other_id_start__c4__s1_(self): @@ -637,9 +876,13 @@ def _other_id_start__c4__s1_(self): def _other_id_start__c5_(self): self._push('other_id_start__c5') - self._seq([lambda: self._bind(self._anything_, 'x'), - self._other_id_start__c5__s1_, - lambda: self._succeed(self._get('x'))]) + self._seq( + [ + lambda: self._bind(self._anything_, 'x'), + self._other_id_start__c5__s1_, + lambda: self._succeed(self._get('x')), + ] + ) self._pop('other_id_start__c5') def _other_id_start__c5__s1_(self): @@ -650,17 +893,30 @@ def _other_id_start__c5__s1_(self): self._fail() def _id_continue_(self): - self._choose([self._ascii_id_start_, self._digit_, - self._other_id_start_, self._id_continue__c3_, - self._id_continue__c4_, self._id_continue__c5_, - self._id_continue__c6_, self._id_continue__c7_, - self._id_continue__c8_, self._id_continue__c9_]) + self._choose( + [ + self._ascii_id_start_, + self._digit_, + self._other_id_start_, + self._id_continue__c3_, + self._id_continue__c4_, + self._id_continue__c5_, + self._id_continue__c6_, + self._id_continue__c7_, + self._id_continue__c8_, + self._id_continue__c9_, + ] + ) def _id_continue__c3_(self): self._push('id_continue__c3') - self._seq([lambda: self._bind(self._anything_, 'x'), - self._id_continue__c3__s1_, - lambda: self._succeed(self._get('x'))]) + self._seq( + [ + lambda: self._bind(self._anything_, 'x'), + self._id_continue__c3__s1_, + lambda: self._succeed(self._get('x')), + ] + ) self._pop('id_continue__c3') def _id_continue__c3__s1_(self): @@ -672,9 +928,13 @@ def _id_continue__c3__s1_(self): def _id_continue__c4_(self): self._push('id_continue__c4') - self._seq([lambda: self._bind(self._anything_, 'x'), - self._id_continue__c4__s1_, - lambda: self._succeed(self._get('x'))]) + self._seq( + [ + lambda: self._bind(self._anything_, 'x'), + self._id_continue__c4__s1_, + lambda: self._succeed(self._get('x')), + ] + ) self._pop('id_continue__c4') def _id_continue__c4__s1_(self): @@ -686,9 +946,13 @@ def _id_continue__c4__s1_(self): def _id_continue__c5_(self): self._push('id_continue__c5') - self._seq([lambda: self._bind(self._anything_, 'x'), - self._id_continue__c5__s1_, - lambda: self._succeed(self._get('x'))]) + self._seq( + [ + lambda: self._bind(self._anything_, 'x'), + self._id_continue__c5__s1_, + lambda: self._succeed(self._get('x')), + ] + ) self._pop('id_continue__c5') def _id_continue__c5__s1_(self): @@ -700,9 +964,13 @@ def _id_continue__c5__s1_(self): def _id_continue__c6_(self): self._push('id_continue__c6') - self._seq([lambda: self._bind(self._anything_, 'x'), - self._id_continue__c6__s1_, - lambda: self._succeed(self._get('x'))]) + self._seq( + [ + lambda: self._bind(self._anything_, 'x'), + self._id_continue__c6__s1_, + lambda: self._succeed(self._get('x')), + ] + ) self._pop('id_continue__c6') def _id_continue__c6__s1_(self): @@ -722,29 +990,48 @@ def _id_continue__c9_(self): self._ch('\u200d') def _num_literal_(self): - self._choose([self._num_literal__c0_, self._num_literal__c1_, - self._num_literal__c2_, self._hex_literal_, - self._num_literal__c4_, self._num_literal__c5_]) + self._choose( + [ + self._num_literal__c0_, + self._num_literal__c1_, + self._num_literal__c2_, + self._hex_literal_, + self._num_literal__c4_, + self._num_literal__c5_, + ] + ) def _num_literal__c0_(self): self._push('num_literal__c0') - self._seq([lambda: self._ch('-'), - lambda: self._bind(self._num_literal_, 'n'), - lambda: self._succeed('-' + self._get('n'))]) + self._seq( + [ + lambda: self._ch('-'), + lambda: self._bind(self._num_literal_, 'n'), + lambda: self._succeed('-' + self._get('n')), + ] + ) self._pop('num_literal__c0') def _num_literal__c1_(self): self._push('num_literal__c1') - self._seq([lambda: self._ch('+'), - lambda: self._bind(self._num_literal_, 'n'), - lambda: self._succeed(self._get('n'))]) + self._seq( + [ + lambda: self._ch('+'), + lambda: self._bind(self._num_literal_, 'n'), + lambda: self._succeed(self._get('n')), + ] + ) self._pop('num_literal__c1') def _num_literal__c2_(self): self._push('num_literal__c2') - self._seq([lambda: self._bind(self._dec_literal_, 'd'), - lambda: self._not(self._id_start_), - lambda: self._succeed(self._get('d'))]) + self._seq( + [ + lambda: self._bind(self._dec_literal_, 'd'), + lambda: self._not(self._id_start_), + lambda: self._succeed(self._get('d')), + ] + ) self._pop('num_literal__c2') def _num_literal__c4_(self): @@ -754,63 +1041,107 @@ def _num_literal__c5_(self): self._str('NaN') def _dec_literal_(self): - self._choose([self._dec_literal__c0_, self._dec_literal__c1_, - self._dec_literal__c2_, self._dec_literal__c3_, - self._dec_literal__c4_, self._dec_literal__c5_]) + self._choose( + [ + self._dec_literal__c0_, + self._dec_literal__c1_, + self._dec_literal__c2_, + self._dec_literal__c3_, + self._dec_literal__c4_, + self._dec_literal__c5_, + ] + ) def _dec_literal__c0_(self): self._push('dec_literal__c0') - self._seq([lambda: self._bind(self._dec_int_lit_, 'd'), - lambda: self._bind(self._frac_, 'f'), - lambda: self._bind(self._exp_, 'e'), - lambda: self._succeed(self._get('d') + self._get('f') + self._get('e'))]) + self._seq( + [ + lambda: self._bind(self._dec_int_lit_, 'd'), + lambda: self._bind(self._frac_, 'f'), + lambda: self._bind(self._exp_, 'e'), + lambda: self._succeed( + self._get('d') + self._get('f') + self._get('e') + ), + ] + ) self._pop('dec_literal__c0') def _dec_literal__c1_(self): self._push('dec_literal__c1') - self._seq([lambda: self._bind(self._dec_int_lit_, 'd'), - lambda: self._bind(self._frac_, 'f'), - lambda: self._succeed(self._get('d') + self._get('f'))]) + self._seq( + [ + lambda: self._bind(self._dec_int_lit_, 'd'), + lambda: self._bind(self._frac_, 'f'), + lambda: self._succeed(self._get('d') + self._get('f')), + ] + ) self._pop('dec_literal__c1') def _dec_literal__c2_(self): self._push('dec_literal__c2') - self._seq([lambda: self._bind(self._dec_int_lit_, 'd'), - lambda: self._bind(self._exp_, 'e'), - lambda: self._succeed(self._get('d') + self._get('e'))]) + self._seq( + [ + lambda: self._bind(self._dec_int_lit_, 'd'), + lambda: self._bind(self._exp_, 'e'), + lambda: self._succeed(self._get('d') + self._get('e')), + ] + ) self._pop('dec_literal__c2') def _dec_literal__c3_(self): self._push('dec_literal__c3') - self._seq([lambda: self._bind(self._dec_int_lit_, 'd'), - lambda: self._succeed(self._get('d'))]) + self._seq( + [ + lambda: self._bind(self._dec_int_lit_, 'd'), + lambda: self._succeed(self._get('d')), + ] + ) self._pop('dec_literal__c3') def _dec_literal__c4_(self): self._push('dec_literal__c4') - self._seq([lambda: self._bind(self._frac_, 'f'), - lambda: self._bind(self._exp_, 'e'), - lambda: self._succeed(self._get('f') + self._get('e'))]) + self._seq( + [ + lambda: self._bind(self._frac_, 'f'), + lambda: self._bind(self._exp_, 'e'), + lambda: self._succeed(self._get('f') + self._get('e')), + ] + ) self._pop('dec_literal__c4') def _dec_literal__c5_(self): self._push('dec_literal__c5') - self._seq([lambda: self._bind(self._frac_, 'f'), - lambda: self._succeed(self._get('f'))]) + self._seq( + [ + lambda: self._bind(self._frac_, 'f'), + lambda: self._succeed(self._get('f')), + ] + ) self._pop('dec_literal__c5') def _dec_int_lit_(self): self._choose([self._dec_int_lit__c0_, self._dec_int_lit__c1_]) def _dec_int_lit__c0_(self): - self._seq([lambda: self._ch('0'), lambda: self._not(self._digit_), - lambda: self._succeed('0')]) + self._seq( + [ + lambda: self._ch('0'), + lambda: self._not(self._digit_), + lambda: self._succeed('0'), + ] + ) def _dec_int_lit__c1_(self): self._push('dec_int_lit__c1') - self._seq([lambda: self._bind(self._nonzerodigit_, 'd'), - self._dec_int_lit__c1__s1_, - lambda: self._succeed(self._get('d') + self._join('', self._get('ds')))]) + self._seq( + [ + lambda: self._bind(self._nonzerodigit_, 'd'), + self._dec_int_lit__c1__s1_, + lambda: self._succeed( + self._get('d') + self._join('', self._get('ds')) + ), + ] + ) self._pop('dec_int_lit__c1') def _dec_int_lit__c1__s1_(self): @@ -824,8 +1155,13 @@ def _nonzerodigit_(self): def _hex_literal_(self): self._push('hex_literal') - self._seq([self._hex_literal__s0_, self._hex_literal__s1_, - lambda: self._succeed('0x' + self._join('', self._get('hs')))]) + self._seq( + [ + self._hex_literal__s0_, + self._hex_literal__s1_, + lambda: self._succeed('0x' + self._join('', self._get('hs'))), + ] + ) self._pop('hex_literal') def _hex_literal__s0_(self): @@ -845,8 +1181,13 @@ def _hex__c1_(self): def _frac_(self): self._push('frac') - self._seq([lambda: self._ch('.'), self._frac__s1_, - lambda: self._succeed('.' + self._join('', self._get('ds')))]) + self._seq( + [ + lambda: self._ch('.'), + self._frac__s1_, + lambda: self._succeed('.' + self._join('', self._get('ds'))), + ] + ) self._pop('frac') def _frac__s1_(self): @@ -857,10 +1198,16 @@ def _exp_(self): def _exp__c0_(self): self._push('exp__c0') - self._seq([self._exp__c0__s0_, - lambda: self._bind(self._exp__c0__s1_l_, 's'), - self._exp__c0__s2_, - lambda: self._succeed('e' + self._get('s') + self._join('', self._get('ds')))]) + self._seq( + [ + self._exp__c0__s0_, + lambda: self._bind(self._exp__c0__s1_l_, 's'), + self._exp__c0__s2_, + lambda: self._succeed( + 'e' + self._get('s') + self._join('', self._get('ds')) + ), + ] + ) self._pop('exp__c0') def _exp__c0__s0_(self): @@ -874,8 +1221,13 @@ def _exp__c0__s2_(self): def _exp__c1_(self): self._push('exp__c1') - self._seq([self._exp__c1__s0_, self._exp__c1__s1_, - lambda: self._succeed('e' + self._join('', self._get('ds')))]) + self._seq( + [ + self._exp__c1__s0_, + self._exp__c1__s1_, + lambda: self._succeed('e' + self._join('', self._get('ds'))), + ] + ) self._pop('exp__c1') def _exp__c1__s0_(self): diff --git a/json5/tool.py b/json5/tool.py index 7649d80..0aa2f9a 100644 --- a/json5/tool.py +++ b/json5/tool.py @@ -38,34 +38,62 @@ def main(argv=None, host=None): host = host or Host() parser = arg_parser.ArgumentParser(host, prog='json5', desc=__doc__) - parser.add_argument('-c', metavar='STR', dest='cmd', - help='inline json5 string to read instead of ' - 'reading from a file') - parser.add_argument('--as-json', dest='as_json', action='store_const', - const=True, default=False, - help='output as JSON ' - '(same as --quote-keys --no-trailing-commas)') - parser.add_argument('--indent', dest='indent', default=4, - help='amount to indent each line ' - '(default is 4 spaces)') - parser.add_argument('--quote-keys', action='store_true', default=False, - help='quote all object keys') - parser.add_argument('--no-quote-keys', action='store_false', - dest='quote_keys', - help="don't quote object keys that are identifiers" - " (this is the default)") - parser.add_argument('--trailing-commas', action='store_true', - default=True, - help='add commas after the last item in multi-line ' - 'objects and arrays (this is the default)') - parser.add_argument('--no-trailing-commas', dest='trailing_commas', - action='store_false', - help='do not add commas after the last item in ' - 'multi-line lists and objects') - parser.add_argument('file', metavar='FILE', nargs='?', default='-', - help='optional file to read JSON5 document from; if ' - 'not specified or "-", will read from stdin ' - 'instead') + parser.add_argument( + '-c', + metavar='STR', + dest='cmd', + help='inline json5 string to read instead of ' 'reading from a file', + ) + parser.add_argument( + '--as-json', + dest='as_json', + action='store_const', + const=True, + default=False, + help='output as JSON ' '(same as --quote-keys --no-trailing-commas)', + ) + parser.add_argument( + '--indent', + dest='indent', + default=4, + help='amount to indent each line ' '(default is 4 spaces)', + ) + parser.add_argument( + '--quote-keys', + action='store_true', + default=False, + help='quote all object keys', + ) + parser.add_argument( + '--no-quote-keys', + action='store_false', + dest='quote_keys', + help="don't quote object keys that are identifiers" + ' (this is the default)', + ) + parser.add_argument( + '--trailing-commas', + action='store_true', + default=True, + help='add commas after the last item in multi-line ' + 'objects and arrays (this is the default)', + ) + parser.add_argument( + '--no-trailing-commas', + dest='trailing_commas', + action='store_false', + help='do not add commas after the last item in ' + 'multi-line lists and objects', + ) + parser.add_argument( + 'file', + metavar='FILE', + nargs='?', + default='-', + help='optional file to read JSON5 document from; if ' + 'not specified or "-", will read from stdin ' + 'instead', + ) args = parser.parse_args(argv) if parser.exit_status is not None: @@ -95,10 +123,12 @@ def main(argv=None, host=None): args.trailing_commas = False obj = lib.loads(inp) - s = lib.dumps(obj, - indent=args.indent, - quote_keys=args.quote_keys, - trailing_commas=args.trailing_commas) + s = lib.dumps( + obj, + indent=args.indent, + quote_keys=args.quote_keys, + trailing_commas=args.trailing_commas, + ) host.print_(s) return 0 diff --git a/tests/host_fake.py b/tests/host_fake.py index 1a8e4bf..8012619 100644 --- a/tests/host_fake.py +++ b/tests/host_fake.py @@ -72,7 +72,7 @@ def join(self, *comps): # pragma: no cover while '/..' in p: comps = p.split('/') idx = comps.index('..') - comps = comps[:idx-1] + comps[idx+1:] + comps = comps[: idx - 1] + comps[idx + 1 :] p = '/'.join(comps) return p diff --git a/tests/host_test.py b/tests/host_test.py index 51f9fcc..97ff218 100644 --- a/tests/host_test.py +++ b/tests/host_test.py @@ -17,6 +17,7 @@ from json5.host import Host + class HostTest(unittest.TestCase): maxDiff = None diff --git a/tests/lib_test.py b/tests/lib_test.py index 904d10d..90f9a75 100644 --- a/tests/lib_test.py +++ b/tests/lib_test.py @@ -29,19 +29,23 @@ from hypothesis import given some_json = some.recursive( - some.none() | - some.booleans() | - some.floats(allow_nan=False) | - some.text(printable), + some.none() + | some.booleans() + | some.floats(allow_nan=False) + | some.text(printable), lambda children: some.lists(children, min_size=1) | some.dictionaries(some.text(printable), children, min_size=1), ) except ImportError: + def given(x): del x + def func(y): del y + return func + some_json = {} @@ -79,23 +83,27 @@ def test_cls_is_not_supported(self): self.assertRaises(AssertionError, json5.loads, '1', cls=lambda x: x) def test_duplicate_keys_should_be_allowed(self): - self.assertEqual(json5.loads('{foo: 1, foo: 2}', - allow_duplicate_keys=True), - {"foo": 2}) + self.assertEqual( + json5.loads('{foo: 1, foo: 2}', allow_duplicate_keys=True), + {'foo': 2}, + ) def test_duplicate_keys_should_be_allowed_by_default(self): - self.check('{foo: 1, foo: 2}', {"foo": 2}) + self.check('{foo: 1, foo: 2}', {'foo': 2}) def test_duplicate_keys_should_not_be_allowed(self): - self.assertRaises(ValueError, json5.loads, '{foo: 1, foo: 2}', - allow_duplicate_keys=False) + self.assertRaises( + ValueError, + json5.loads, + '{foo: 1, foo: 2}', + allow_duplicate_keys=False, + ) def test_empty_strings_are_errors(self): self.check_fail('', 'Empty strings are not legal JSON5') def test_encoding(self): - self.assertEqual(json5.loads(b'"\xf6"', encoding='iso-8859-1'), - '\xf6') + self.assertEqual(json5.loads(b'"\xf6"', encoding='iso-8859-1'), '\xf6') def test_numbers(self): # decimal literals @@ -145,61 +153,71 @@ def test_null(self): def test_object_hook(self): def hook(d): return [d] - self.assertEqual(json5.loads('{foo: 1}', object_hook=hook), - [{"foo": 1}]) + + self.assertEqual( + json5.loads('{foo: 1}', object_hook=hook), [{'foo': 1}] + ) def test_object_pairs_hook(self): def hook(pairs): return pairs - self.assertEqual(json5.loads('{foo: 1, bar: 2}', - object_pairs_hook=hook), - [('foo', 1), ('bar', 2)]) + + self.assertEqual( + json5.loads('{foo: 1, bar: 2}', object_pairs_hook=hook), + [('foo', 1), ('bar', 2)], + ) def test_objects(self): self.check('{}', {}) - self.check('{"foo": 0}', {"foo": 0}) - self.check('{"foo":0,"bar":1}', {"foo": 0, "bar": 1}) - self.check('{ "foo" : 0 , "bar" : 1 }', {"foo": 0, "bar": 1}) + self.check('{"foo": 0}', {'foo': 0}) + self.check('{"foo":0,"bar":1}', {'foo': 0, 'bar': 1}) + self.check('{ "foo" : 0 , "bar" : 1 }', {'foo': 0, 'bar': 1}) def test_parse_constant(self): def hook(x): return x - self.assertEqual(json5.loads('-Infinity', parse_constant=hook), - '-Infinity') - self.assertEqual(json5.loads('NaN', parse_constant=hook), - 'NaN') + + self.assertEqual( + json5.loads('-Infinity', parse_constant=hook), '-Infinity' + ) + self.assertEqual(json5.loads('NaN', parse_constant=hook), 'NaN') def test_parse_float(self): def hook(x): return x + self.assertEqual(json5.loads('1.0', parse_float=hook), '1.0') def test_parse_int(self): def hook(x, base=10): del base return x + self.assertEqual(json5.loads('1', parse_int=hook), '1') def test_sample_file(self): path = os.path.join(os.path.dirname(__file__), '..', 'sample.json5') with open(path, encoding='utf-8') as fp: obj = json5.load(fp) - self.assertEqual({ - 'oh': [ - "we shouldn't forget", - "arrays can have", - "trailing commas too", - ], - "this": "is a multi-line string", - "delta": 10, - "hex": 3735928559, - "finally": "a trailing comma", - "here": "is another", - "to": float("inf"), - "while": True, - "half": 0.5, - "foo": "bar" - }, obj) + self.assertEqual( + { + 'oh': [ + "we shouldn't forget", + 'arrays can have', + 'trailing commas too', + ], + 'this': 'is a multi-line string', + 'delta': 10, + 'hex': 3735928559, + 'finally': 'a trailing comma', + 'here': 'is another', + 'to': float('inf'), + 'while': True, + 'half': 0.5, + 'foo': 'bar', + }, + obj, + ) def test_strings(self): self.check('"foo"', 'foo') @@ -251,14 +269,13 @@ def test_whitespace(self): self.check('\r\n1', 1) self.check('\t1', 1) self.check('\v1', 1) - self.check('\uFEFF 1', 1) - self.check('\u00A0 1', 1) + self.check('\ufeff 1', 1) + self.check('\u00a0 1', 1) self.check('\u2028 1', 1) self.check('\u2029 1', 1) def test_error_reporting(self): - self.check_fail('[ ,]', - err=':1 Unexpected "," at column 3') + self.check_fail('[ ,]', err=':1 Unexpected "," at column 3') self.check_fail( '{\n' @@ -269,7 +286,8 @@ def test_error_reporting(self): ' "Python"foo\n' ' ]\n' '}\n', - err=':6 Unexpected "f" at column 17') + err=':6 Unexpected "f" at column 17', + ) class TestDump(unittest.TestCase): @@ -286,19 +304,24 @@ def check(self, obj, s): self.assertEqual(json5.dumps(obj), s) def test_allow_duplicate_keys(self): - self.assertIn(json5.dumps({1: "foo", "1": "bar"}), - {'{"1": "foo", "1": "bar"}', - '{"1": "bar", "1": "foo"}'}) - - self.assertRaises(ValueError, json5.dumps, - {1: "foo", "1": "bar"}, - allow_duplicate_keys=False) + self.assertIn( + json5.dumps({1: 'foo', '1': 'bar'}), + {'{"1": "foo", "1": "bar"}', '{"1": "bar", "1": "foo"}'}, + ) + + self.assertRaises( + ValueError, + json5.dumps, + {1: 'foo', '1': 'bar'}, + allow_duplicate_keys=False, + ) def test_arrays(self): self.check([], '[]') self.check([1, 2, 3], '[1, 2, 3]') - self.check([{'foo': 'bar'}, {'baz': 'quux'}], - '[{foo: "bar"}, {baz: "quux"}]') + self.check( + [{'foo': 'bar'}, {'baz': 'quux'}], '[{foo: "bar"}, {baz: "quux"}]' + ) def test_bools(self): self.check(True, 'true') @@ -321,9 +344,8 @@ def test_check_circular(self): # This checks that repeated but non-circular references # are okay. x = [1, 2] - y = {"foo": x, "bar": x} - self.check(y, - '{foo: [1, 2], bar: [1, 2]}') + y = {'foo': x, 'bar': x} + self.check(y, '{foo: [1, 2], bar: [1, 2]}') # This tests a more complicated cycle. x = {} @@ -392,42 +414,43 @@ class MyStr(str): self.assertEqual(json5.dumps({'foo': MyStr('bar')}), '{foo: "bar"}') def test_default(self): - def _custom_serializer(obj): del obj return 'something' self.assertRaises(TypeError, json5.dumps, set()) - self.assertEqual(json5.dumps(set(), default=_custom_serializer), - '"something"') + self.assertEqual( + json5.dumps(set(), default=_custom_serializer), '"something"' + ) def test_ensure_ascii(self): self.check('\u00fc', '"\\u00fc"') - self.assertEqual(json5.dumps('\u00fc', ensure_ascii=False), - '"\u00fc"') + self.assertEqual(json5.dumps('\u00fc', ensure_ascii=False), '"\u00fc"') def test_indent(self): - self.assertEqual(json5.dumps([1, 2, 3], indent=None), - '[1, 2, 3]') - self.assertEqual(json5.dumps([1, 2, 3], indent=-1), - '[\n1,\n2,\n3,\n]') - self.assertEqual(json5.dumps([1, 2, 3], indent=0), - '[\n1,\n2,\n3,\n]') - self.assertEqual(json5.dumps([], indent=2), - '[]') - self.assertEqual(json5.dumps([1, 2, 3], indent=2), - '[\n 1,\n 2,\n 3,\n]') - self.assertEqual(json5.dumps([1, 2, 3], indent=' '), - '[\n 1,\n 2,\n 3,\n]') - self.assertEqual(json5.dumps([1, 2, 3], indent='++'), - '[\n++1,\n++2,\n++3,\n]') - self.assertEqual(json5.dumps([[1, 2, 3]], indent=2), - '[\n [\n 1,\n 2,\n 3,\n ],\n]') - - self.assertEqual(json5.dumps({}, indent=2), - '{}') - self.assertEqual(json5.dumps({'foo': 'bar', 'baz': 'quux'}, indent=2), - '{\n foo: "bar",\n baz: "quux",\n}') + self.assertEqual(json5.dumps([1, 2, 3], indent=None), '[1, 2, 3]') + self.assertEqual(json5.dumps([1, 2, 3], indent=-1), '[\n1,\n2,\n3,\n]') + self.assertEqual(json5.dumps([1, 2, 3], indent=0), '[\n1,\n2,\n3,\n]') + self.assertEqual(json5.dumps([], indent=2), '[]') + self.assertEqual( + json5.dumps([1, 2, 3], indent=2), '[\n 1,\n 2,\n 3,\n]' + ) + self.assertEqual( + json5.dumps([1, 2, 3], indent=' '), '[\n 1,\n 2,\n 3,\n]' + ) + self.assertEqual( + json5.dumps([1, 2, 3], indent='++'), '[\n++1,\n++2,\n++3,\n]' + ) + self.assertEqual( + json5.dumps([[1, 2, 3]], indent=2), + '[\n [\n 1,\n 2,\n 3,\n ],\n]', + ) + + self.assertEqual(json5.dumps({}, indent=2), '{}') + self.assertEqual( + json5.dumps({'foo': 'bar', 'baz': 'quux'}, indent=2), + '{\n foo: "bar",\n baz: "quux",\n}', + ) def test_numbers(self): self.check(15, '15') @@ -436,12 +459,15 @@ def test_numbers(self): self.check(float('-inf'), '-Infinity') self.check(float('nan'), 'NaN') - self.assertRaises(ValueError, json5.dumps, - float('inf'), allow_nan=False) - self.assertRaises(ValueError, json5.dumps, - float('-inf'), allow_nan=False) - self.assertRaises(ValueError, json5.dumps, - float('nan'), allow_nan=False) + self.assertRaises( + ValueError, json5.dumps, float('inf'), allow_nan=False + ) + self.assertRaises( + ValueError, json5.dumps, float('-inf'), allow_nan=False + ) + self.assertRaises( + ValueError, json5.dumps, float('nan'), allow_nan=False + ) def test_null(self): self.check(None, 'null') @@ -455,27 +481,36 @@ def test_reserved_words_in_object_keys_are_quoted(self): self.check({'new': 1}, '{"new": 1}') # pylint: disable=invalid-name - def test_identifiers_only_starting_with_reserved_words_are_not_quoted(self): + def test_identifiers_only_starting_with_reserved_words_are_not_quoted( + self, + ): self.check({'newbie': 1}, '{newbie: 1}') + # pylint: enable=invalid-name def test_non_string_keys(self): - self.assertEqual(json5.dumps({False: 'a', 1: 'b', 2.0: 'c', None: 'd'}), - '{"false": "a", "1": "b", "2.0": "c", "null": "d"}') + self.assertEqual( + json5.dumps({False: 'a', 1: 'b', 2.0: 'c', None: 'd'}), + '{"false": "a", "1": "b", "2.0": "c", "null": "d"}', + ) def test_quote_keys(self): - self.assertEqual(json5.dumps({"foo": 1}, quote_keys=True), - '{"foo": 1}') + self.assertEqual( + json5.dumps({'foo': 1}, quote_keys=True), '{"foo": 1}' + ) def test_strings(self): self.check("'single'", '"\'single\'"') self.check('"double"', '"\\"double\\""') - self.check("'single \\' and double \"'", - '"\'single \\\\\' and double \\"\'"') + self.check( + "'single \\' and double \"'", '"\'single \\\\\' and double \\"\'"' + ) def test_string_escape_sequences(self): - self.check('\u2028\u2029\b\t\f\n\r\v\\\0', - '"\\u2028\\u2029\\b\\t\\f\\n\\r\\v\\\\\\0"') + self.check( + '\u2028\u2029\b\t\f\n\r\v\\\0', + '"\\u2028\\u2029\\b\\t\\f\\n\\r\\v\\\\\\0"', + ) def test_skip_keys(self): od = OrderedDict() @@ -498,22 +533,21 @@ def test_sort_keys(self): od = OrderedDict() od['foo'] = 1 od['bar'] = 2 - self.assertEqual(json5.dumps(od, sort_keys=True), - '{bar: 2, foo: 1}') + self.assertEqual(json5.dumps(od, sort_keys=True), '{bar: 2, foo: 1}') def test_trailing_commas(self): # By default, multi-line dicts and lists should have trailing # commas after their last items. - self.assertEqual(json5.dumps({"foo": 1}, indent=2), - '{\n foo: 1,\n}') - self.assertEqual(json5.dumps([1], indent=2), - '[\n 1,\n]') - - self.assertEqual(json5.dumps({"foo": 1}, indent=2, - trailing_commas=False), - '{\n foo: 1\n}') - self.assertEqual(json5.dumps([1], indent=2, trailing_commas=False), - '[\n 1\n]') + self.assertEqual(json5.dumps({'foo': 1}, indent=2), '{\n foo: 1,\n}') + self.assertEqual(json5.dumps([1], indent=2), '[\n 1,\n]') + + self.assertEqual( + json5.dumps({'foo': 1}, indent=2, trailing_commas=False), + '{\n foo: 1\n}', + ) + self.assertEqual( + json5.dumps([1], indent=2, trailing_commas=False), '[\n 1\n]' + ) def test_supplemental_unicode(self): try: diff --git a/tests/tool_test.py b/tests/tool_test.py index 7c89395..33f490c 100644 --- a/tests/tool_test.py +++ b/tests/tool_test.py @@ -25,8 +25,9 @@ def _write_files(self, host, files): for path, contents in list(files.items()): host.write_text_file(path, contents) - def check_cmd(self, args, stdin=None, files=None, - returncode=None, out=None, err=None): + def check_cmd( + self, args, stdin=None, files=None, returncode=None, out=None, err=None + ): host = self._host() orig_wd, tmpdir = None, None try: @@ -50,8 +51,9 @@ class UnitTestMixin: def _host(self): return FakeHost() - def _call(self, host, args, stdin=None, - returncode=None, out=None, err=None): + def _call( + self, host, args, stdin=None, returncode=None, out=None, err=None + ): if stdin is not None: host.stdin.write(str(stdin)) host.stdin.seek(0) @@ -74,32 +76,46 @@ def test_help(self): self.check_cmd(['--help'], returncode=0) def test_inline_expression(self): - self.check_cmd(['-c', '{foo: 1}'], returncode=0, - out='{\n foo: 1,\n}\n') + self.check_cmd( + ['-c', '{foo: 1}'], returncode=0, out='{\n foo: 1,\n}\n' + ) def test_indent(self): - self.check_cmd(['--indent=None', '-c', '[1]'], returncode=0, - out='[1]\n') - self.check_cmd(['--indent=2', '-c', '[1]'], returncode=0, - out='[\n 1,\n]\n') - self.check_cmd(['--indent= ', '-c', '[1]'], returncode=0, - out='[\n 1,\n]\n') + self.check_cmd( + ['--indent=None', '-c', '[1]'], returncode=0, out='[1]\n' + ) + self.check_cmd( + ['--indent=2', '-c', '[1]'], returncode=0, out='[\n 1,\n]\n' + ) + self.check_cmd( + ['--indent= ', '-c', '[1]'], returncode=0, out='[\n 1,\n]\n' + ) def test_as_json(self): - self.check_cmd(['--as-json', '-c', '{foo: 1}'], returncode=0, - out='{\n "foo": 1\n}\n') + self.check_cmd( + ['--as-json', '-c', '{foo: 1}'], + returncode=0, + out='{\n "foo": 1\n}\n', + ) def test_quote_keys(self): - self.check_cmd(['--quote-keys', '-c', '{foo: 1}'], returncode=0, - out='{\n "foo": 1,\n}\n') + self.check_cmd( + ['--quote-keys', '-c', '{foo: 1}'], + returncode=0, + out='{\n "foo": 1,\n}\n', + ) def test_no_quote_keys(self): - self.check_cmd(['--no-quote-keys', '-c', '{foo: 1}'], returncode=0, - out='{\n foo: 1,\n}\n') + self.check_cmd( + ['--no-quote-keys', '-c', '{foo: 1}'], + returncode=0, + out='{\n foo: 1,\n}\n', + ) def test_keys_are_quoted_by_default(self): - self.check_cmd(['-c', '{foo: 1}'], returncode=0, - out='{\n foo: 1,\n}\n') + self.check_cmd( + ['-c', '{foo: 1}'], returncode=0, out='{\n foo: 1,\n}\n' + ) def test_read_command(self): self.check_cmd(['-c', '"foo"'], returncode=0, out='"foo"\n') @@ -114,27 +130,39 @@ def test_read_from_a_file(self): self.check_cmd(['foo.json5'], files=files, returncode=0, out='"foo"\n') def test_trailing_commas(self): - self.check_cmd(['--trailing-commas', '-c', '{foo: 1}'], returncode=0, - out='{\n foo: 1,\n}\n') + self.check_cmd( + ['--trailing-commas', '-c', '{foo: 1}'], + returncode=0, + out='{\n foo: 1,\n}\n', + ) def test_no_trailing_commas(self): - self.check_cmd(['--no-trailing-commas', '-c', '{foo: 1}'], returncode=0, - out='{\n foo: 1\n}\n') + self.check_cmd( + ['--no-trailing-commas', '-c', '{foo: 1}'], + returncode=0, + out='{\n foo: 1\n}\n', + ) def test_trailing_commas_are_there_by_default(self): - self.check_cmd(['-c', '{foo: 1}'], returncode=0, - out='{\n foo: 1,\n}\n') + self.check_cmd( + ['-c', '{foo: 1}'], returncode=0, out='{\n foo: 1,\n}\n' + ) def test_unknown_switch(self): - self.check_cmd(['--unknown-switch'], returncode=2, - err='json5: error: unrecognized arguments: ' - '--unknown-switch\n\n') + self.check_cmd( + ['--unknown-switch'], + returncode=2, + err='json5: error: unrecognized arguments: ' + '--unknown-switch\n\n', + ) def test_version(self): - self.check_cmd(['--version'], returncode=0, - out=str(json5.VERSION) + '\n') - self.check_cmd(['--version'], returncode=0, - out=str(json5.__version__) + '\n') + self.check_cmd( + ['--version'], returncode=0, out=str(json5.VERSION) + '\n' + ) + self.check_cmd( + ['--version'], returncode=0, out=str(json5.__version__) + '\n' + ) if __name__ == '__main__': # pragma: no cover