Skip to content

Commit

Permalink
[lint] stricter ruff, apply to generated code (#329)
Browse files Browse the repository at this point in the history
* [lint] stricter ruff, apply to generated code
* [lint] stricter linting on tests and examples
* [codegen] make sure KEYWORDS is a set in generated parser
  • Loading branch information
apalala authored Nov 23, 2023
1 parent b2e4d3a commit f8650f5
Show file tree
Hide file tree
Showing 17 changed files with 432 additions and 284 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ lint: ruff mypy


ruff:
pip install -q -U ruff
-@ pip install -q -U ruff
ruff check --preview tatsu test examples


mypy:
pip install -q -U mypy
-@ pip install -q -U mypy
mypy --ignore-missing-imports . --exclude dist


Expand Down
15 changes: 8 additions & 7 deletions examples/calc/calc.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
from pathlib import Path
from pprint import pprint

from codegen import PostfixCodeGenerator
Expand All @@ -9,7 +10,7 @@


def simple_parse():
grammar = open('grammars/calc_cut.ebnf').read()
grammar = Path('grammars/calc_cut.ebnf').read_text()

parser = tatsu.compile(grammar)
ast = parser.parse('3 + 5 * ( 10 - 20 )', trace=False, colorize=True)
Expand All @@ -26,7 +27,7 @@ def simple_parse():


def annotated_parse():
grammar = open('grammars/calc_annotated.ebnf').read()
grammar = Path('grammars/calc_annotated.ebnf').read_text()

parser = tatsu.compile(grammar)
ast = parser.parse('3 + 5 * ( 10 - 20 )')
Expand Down Expand Up @@ -63,7 +64,7 @@ def expression(self, ast):


def parse_with_basic_semantics():
grammar = open('grammars/calc_annotated.ebnf').read()
grammar = Path('grammars/calc_annotated.ebnf').read_text()

parser = tatsu.compile(grammar)
result = parser.parse(
Expand Down Expand Up @@ -95,7 +96,7 @@ def division(self, ast):


def parse_factored():
grammar = open('grammars/calc_factored.ebnf').read()
grammar = Path('grammars/calc_factored.ebnf').read_text()

parser = tatsu.compile(grammar)
ast = parser.parse('3 + 5 * ( 10 - 20 )', semantics=CalcSemantics())
Expand All @@ -107,7 +108,7 @@ def parse_factored():


def parse_to_model():
grammar = open('grammars/calc_model.ebnf').read()
grammar = Path('grammars/calc_model.ebnf').read_text()

parser = tatsu.compile(grammar, asmodel=True)
model = parser.parse('3 + 5 * ( 10 - 20 )')
Expand Down Expand Up @@ -136,7 +137,7 @@ def walk__divide(self, node):


def parse_and_walk_model():
grammar = open('grammars/calc_model.ebnf').read()
grammar = Path('grammars/calc_model.ebnf').read_text()

parser = tatsu.compile(grammar, asmodel=True)
model = parser.parse('3 + 5 * ( 10 - 20 )')
Expand All @@ -150,7 +151,7 @@ def parse_and_walk_model():


def parse_and_translate():
grammar = open('grammars/calc_model.ebnf').read()
grammar = Path('grammars/calc_model.ebnf').read_text()

parser = tatsu.compile(grammar, asmodel=True)
model = parser.parse('3 + 5 * ( 10 - 20 )')
Expand Down
56 changes: 35 additions & 21 deletions ruff.toml
Original file line number Diff line number Diff line change
@@ -1,30 +1,44 @@
line-length = 88

select = [
'F', 'E', 'W', 'UP',
'I', 'YTT', 'S', 'B',
'COM', 'C4', 'SIM',
'PTH', 'TRY',
'FLY', 'PERF', 'FURB', 'RUF',
'PL', 'PGH', 'RET',
# 'ERA', # commented out code
"F", "E", "W", "UP",
"I", "YTT", "S", "B",
"COM", "C4", "SIM",
"PTH", "TRY",
"FLY", "PERF", "FURB", "RUF",
"PL", "PGH", "RET",
# "ERA", # commented out code
]
ignore = [
'E501', 'E741', 'E402',
'S101', 'S102', 'S105', 'S301', 'S311',
'C408',
'SIM115', 'SIM114',
'PTH123',
'TRY003', 'TRY300',
'PLW1514', 'PLR6301',
'PLC0415',
'PLR0913', 'PLR2004', 'PLR0904', 'PLR0915',
'PLW0603', 'PLW2901', 'PLW3201',
'RET505',
# "C408", # unnecessary-collection-call
"E501", # line-too-long
"E741", # ambiguous-variable-name
"E402", # module-import-not-at-top-of-file
"S101", # use of assert
"PLC0415", # import-outside-top-level
"PLR6301", # no-self-use
"PLR0904", # too-many-public-methods
"PLR0913", # too-many-arguments
"PLR0915", # too-many-statements
"PLR2004", # magic-value-comparison
"PLW1514", # unspecified-encoding
# "PLW0603", # global-statement
# "PLW2901", # redefined-loop-name
"PLW3201", # bad-dunder-method-name
# "PTH123", # builtin-open
"RET505", # superfluous-else-return
"S102", # exec-builtin
# "S105", # hardcoded-password-string
"S301", # suspicious-pickle-usage
# "S311", # suspicious-non-cryptographic-random-usage
"SIM115", # open-file-with-context-handler
"SIM114", # if-with-same-arms
"TRY003", # raise-vanilla-args
"TRY300", # try-consider-else
]
exclude = ['tatsu/bootstrap.py']
exclude = []

target-version = 'py312'
target-version = "py312"

[per-file-ignores]

Expand All @@ -33,4 +47,4 @@ target-version = 'py312'
max-complexity = 10

[pydocstyle]
convention = 'numpy'
convention = "numpy"
6 changes: 2 additions & 4 deletions tatsu/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,11 @@ def _safekey(self, key):
return key

def _define(self, keys, list_keys=None):
for key in keys:
key = self._safekey(key)
for key in (self._safekey(k) for k in keys):
if key not in self:
super().__setitem__(key, None)

for key in list_keys or []:
key = self._safekey(key)
for key in (self._safekey(k) for k in list_keys or []):
if key not in self:
super().__setitem__(key, [])

Expand Down
Loading

0 comments on commit f8650f5

Please sign in to comment.