From d4d7531b4fea720a5cf21154538531aa6f4b0d26 Mon Sep 17 00:00:00 2001 From: apalala Date: Fri, 10 Nov 2023 13:16:20 -0400 Subject: [PATCH] [lint] add SIM set to ruff --- ruff.toml | 7 ++++++- tatsu/codegen/objectmodel.py | 5 +---- tatsu/contexts.py | 16 +++++----------- tatsu/grammars.py | 9 ++------- tatsu/leftrec.py | 15 +++++++-------- tatsu/util/misc.py | 5 +---- test/grammar/left_recursion_test.py | 14 +++++++------- test/grammar/parameter_test.py | 13 ++++--------- test/parser_equivalence_test.py | 2 +- test/pickle_test.py | 8 ++++---- 10 files changed, 38 insertions(+), 56 deletions(-) diff --git a/ruff.toml b/ruff.toml index 8e3440d0..0478f15d 100644 --- a/ruff.toml +++ b/ruff.toml @@ -1,10 +1,15 @@ line-length = 88 -select = ['F', 'E', 'W', 'UP', 'I', 'YTT', 'S', 'B', 'COM', 'C4'] +select = [ + 'F', 'E', 'W', 'UP', + 'I', 'YTT', 'S', 'B', + 'COM', 'C4', 'SIM', +] ignore = [ 'E501', 'E741', 'E402', 'S101', 'S102', 'S105', 'S301', 'S311', 'C408', + 'SIM115', 'SIM114', ] exclude = ['tatsu/bootstrap.py'] diff --git a/tatsu/codegen/objectmodel.py b/tatsu/codegen/objectmodel.py index 0e35e543..11b7169b 100644 --- a/tatsu/codegen/objectmodel.py +++ b/tatsu/codegen/objectmodel.py @@ -139,10 +139,7 @@ def render_fields(self, fields): spec = fields["spec"] kwargs = '\n'.join('%s: Any = None' % d for d in defs) - if kwargs: - kwargs = indent(kwargs) - else: - kwargs = indent('pass') + kwargs = indent(kwargs) if kwargs else indent('pass') fields.update( class_name=spec.class_name, diff --git a/tatsu/contexts.py b/tatsu/contexts.py index b80f54ac..87a3c3e8 100644 --- a/tatsu/contexts.py +++ b/tatsu/contexts.py @@ -3,7 +3,7 @@ import ast as stdlib_ast import functools import sys -from contextlib import contextmanager +from contextlib import contextmanager, suppress from copy import copy from . import buffering, color, tokenizing @@ -312,10 +312,7 @@ def ast_set(self, name, value, as_list=False): previous = ast.get(name) if previous is None: - if as_list: - new_value = [value] - else: - new_value = value + new_value = [value] if as_list else value elif is_list(previous): new_value = previous + [value] else: @@ -796,17 +793,14 @@ def _option(self): @contextmanager def _choice(self): self.last_node = None - try: + with suppress(OptionSucceeded): yield - except OptionSucceeded: - pass @contextmanager def _optional(self): self.last_node = None - with self._choice(): - with self._option(): - yield + with self._choice(), self._option(): + yield @contextmanager def _group(self): diff --git a/tatsu/grammars.py b/tatsu/grammars.py index 22966e61..60632731 100644 --- a/tatsu/grammars.py +++ b/tatsu/grammars.py @@ -866,9 +866,7 @@ def _nullable(self): @staticmethod def param_repr(p): - if isinstance(p, int | float): - return str(p) - elif isinstance(p, str) and p.isalnum(): + if isinstance(p, int | float) or isinstance(p, str) and p.isalnum(): return str(p) else: return repr(p) @@ -894,10 +892,7 @@ def _to_str(self, lean=False): elif kwparams: params = '(%s)' % (kwparams) elif params: - if len(self.params) == 1: - params = '::%s' % params - else: - params = '(%s)' % params + params = '::%s' % params if len(self.params) == 1 else '(%s)' % params base = ' < %s' % str(self.base.name) if self.base else '' diff --git a/tatsu/leftrec.py b/tatsu/leftrec.py index 4257cb5d..74ccfed5 100644 --- a/tatsu/leftrec.py +++ b/tatsu/leftrec.py @@ -160,14 +160,13 @@ def walk(model): child = follow(child, rule_dict) walk(child) # afterEdge - if state[child] == CUTOFF: # active cycle - if stack_positions[child] > lr_stack_positions[-1]: - # turn off memoization for all rules that were involved in this cycle - for rule in stack_positions: - if isinstance(rule, grammars.Rule): - rule.is_memoizable = False - - child.is_leftrec = True + if state[child] == CUTOFF and stack_positions[child] > lr_stack_positions[-1]: + # turn off memoization for all rules that were involved in this cycle + for rule in stack_positions: + if isinstance(rule, grammars.Rule): + rule.is_memoizable = False + + child.is_leftrec = True # afterNode if leftrec: diff --git a/tatsu/util/misc.py b/tatsu/util/misc.py index 48b8bf98..a2701d23 100644 --- a/tatsu/util/misc.py +++ b/tatsu/util/misc.py @@ -58,10 +58,7 @@ def findalliter(pattern, string, pos=None, endpos=None, flags=0): implementation taken from cpython/Modules/_sre.c/findall() ''' - if isinstance(pattern, RETYPE): - r = pattern - else: - r = re.compile(pattern, flags=flags) + r = pattern if isinstance(pattern, RETYPE) else re.compile(pattern, flags=flags) if endpos is not None: iterator = r.finditer(string, pos=pos, endpos=endpos) elif pos is not None: diff --git a/test/grammar/left_recursion_test.py b/test/grammar/left_recursion_test.py index 6b590230..7d8a50c7 100644 --- a/test/grammar/left_recursion_test.py +++ b/test/grammar/left_recursion_test.py @@ -401,7 +401,7 @@ def test_partial_input_bug(self, trace=False): model = compile(grammar) ast = model.parse(input, trace=trace, colorize=True) - assert ('{', 'size', '}') == ast + assert ast == ('{', 'size', '}') def test_dropped_input_bug(self, trace=False): grammar = r''' @@ -498,7 +498,7 @@ def test_associativity(self): A = | A 'a' | 'a' ; ''' - assert (('a', 'a'), 'a') == parse(left_grammar, 'aaa') + assert parse(left_grammar, 'aaa') == (('a', 'a'), 'a') right_grammar = ''' @@left_recursion :: True @@ -508,7 +508,7 @@ def test_associativity(self): A = | 'a' A | 'a' ; ''' - assert ('a', ('a', 'a')) == parse(right_grammar, 'aaa') + assert parse(right_grammar, 'aaa') == ('a', ('a', 'a')) def test_peg_associativity(self): left_grammar = ''' @@ -519,7 +519,7 @@ def test_peg_associativity(self): A = | A 'a' | 'a' A | 'a' ; ''' - assert ('a', ('a', 'a')) == parse(left_grammar, 'aaa') + assert parse(left_grammar, 'aaa') == ('a', ('a', 'a')) right_grammar = ''' @@left_recursion :: True @@ -529,7 +529,7 @@ def test_peg_associativity(self): A = | 'a' A | A 'a' | 'a' ; ''' - assert ('a', ('a', 'a')) == parse(right_grammar, 'aaa') + assert parse(right_grammar, 'aaa') == ('a', ('a', 'a')) def test_nullable_void(self): left_grammar = ''' @@ -540,7 +540,7 @@ def test_nullable_void(self): A = | A 'a' | () ; ''' - assert (('a', 'a'), 'a') == parse(left_grammar, 'aaa') + assert parse(left_grammar, 'aaa') == (('a', 'a'), 'a') def test_leftrec_with_void(self): left_grammar = ''' @@ -551,5 +551,5 @@ def test_leftrec_with_void(self): A = | A 'a' | 'a' ; ''' - assert (('a', 'a'), 'a') == parse(left_grammar, 'aaa') + assert parse(left_grammar, 'aaa') == (('a', 'a'), 'a') assert parse(left_grammar, '') is None diff --git a/test/grammar/parameter_test.py b/test/grammar/parameter_test.py index 5effe99d..bd93c833 100644 --- a/test/grammar/parameter_test.py +++ b/test/grammar/parameter_test.py @@ -1,3 +1,4 @@ +import contextlib import unittest from tatsu.codegen import codegen @@ -139,18 +140,12 @@ def test_36_unichars(self): def _trydelete(pymodule): import os - try: + with contextlib.suppress(OSError): os.unlink(pymodule + ".py") - except OSError: - pass - try: + with contextlib.suppress(OSError): os.unlink(pymodule + ".pyc") - except OSError: - pass - try: + with contextlib.suppress(OSError): os.unlink(pymodule + ".pyo") - except OSError: - pass def assert_equal(target, value): self.assertEqual(target, value) diff --git a/test/parser_equivalence_test.py b/test/parser_equivalence_test.py index b6fa285c..ce09b332 100644 --- a/test/parser_equivalence_test.py +++ b/test/parser_equivalence_test.py @@ -50,7 +50,7 @@ def generate_and_load_parser(name, grammar): def test_model_parse(): model = tatsu.compile(name='Test', grammar=GRAMMAR) - assert OUTPUT == model.parse(INPUT) + assert model.parse(INPUT) == OUTPUT def test_codegen_parse(): diff --git a/test/pickle_test.py b/test/pickle_test.py index a60da3b6..e09f5966 100644 --- a/test/pickle_test.py +++ b/test/pickle_test.py @@ -21,11 +21,11 @@ def test_synth_model(): m = compile(grammar, 'ASeq') model = m.parse('a a a', semantics=ModelBuilderSemantics()) - assert 'ASeq' == type(model).__name__ + assert type(model).__name__ == 'ASeq' p = pickle.dumps(model) new_model = pickle.loads(p) - assert 'ASeq' == type(new_model).__name__ + assert type(new_model).__name__ == 'ASeq' assert model.ast == new_model.ast @@ -46,11 +46,11 @@ def test_nested_class_synth_model(): m = compile(grammar, 'ASeq') model = m.parse('a a a', semantics=ModelBuilderSemantics()) - assert 'ASeq' == type(model).__name__ + assert type(model).__name__ == 'ASeq' p = pickle.dumps(model) new_model = pickle.loads(p) - assert 'ASeq' == type(new_model).__name__ + assert type(new_model).__name__ == 'ASeq' # NOTE: Since we are unpickling an object which contains nested objects, we can't do # self.assertEqual(model.ast, new_model.ast) as the memory locations will be different.