Skip to content

Commit

Permalink
[lint] add SIM set to ruff
Browse files Browse the repository at this point in the history
  • Loading branch information
apalala committed Nov 10, 2023
1 parent b27fb5f commit d4d7531
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 56 deletions.
7 changes: 6 additions & 1 deletion ruff.toml
Original file line number Diff line number Diff line change
@@ -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']

Expand Down
5 changes: 1 addition & 4 deletions tatsu/codegen/objectmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
16 changes: 5 additions & 11 deletions tatsu/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
9 changes: 2 additions & 7 deletions tatsu/grammars.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 ''

Expand Down
15 changes: 7 additions & 8 deletions tatsu/leftrec.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 1 addition & 4 deletions tatsu/util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions test/grammar/left_recursion_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'''
Expand Down Expand Up @@ -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
Expand All @@ -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 = '''
Expand All @@ -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
Expand All @@ -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 = '''
Expand All @@ -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 = '''
Expand All @@ -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
13 changes: 4 additions & 9 deletions test/grammar/parameter_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import contextlib
import unittest

from tatsu.codegen import codegen
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/parser_equivalence_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
8 changes: 4 additions & 4 deletions test/pickle_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.
Expand Down

0 comments on commit d4d7531

Please sign in to comment.