Skip to content

Commit

Permalink
[lint] fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
apalala committed Dec 29, 2024
1 parent 1b632e8 commit 1d994c5
Show file tree
Hide file tree
Showing 14 changed files with 21 additions and 28 deletions.
1 change: 1 addition & 0 deletions ruff.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ lint.ignore = [
"E501", # line-too-long
"E741", # ambiguous-variable-name
"E402", # module-import-not-at-top-of-file
"FURB189", # subclassing str
"S101", # use of assert
"PERF203", # try-except within loop
"PLC0415", # import-outside-top-level
Expand Down
2 changes: 1 addition & 1 deletion tatsu/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from .util import asjson, is_list


class AST(dict): # noqa: FURB189
class AST(dict):
_frozen = False

def __init__(self, *args, **kwargs):
Expand Down
5 changes: 2 additions & 3 deletions tatsu/buffering.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
)
from .tokenizing import Tokenizer
from .util import (
RETYPE,
contains_sublist,
extend_list,
identity,
Expand Down Expand Up @@ -85,7 +84,7 @@ def build_whitespace_re(whitespace):
return DEFAULT_WHITESPACE_RE
if whitespace in {None, ''}:
return None
elif isinstance(whitespace, RETYPE):
elif isinstance(whitespace, re.Pattern):
return whitespace
elif whitespace:
if not isinstance(whitespace, str):
Expand Down Expand Up @@ -354,7 +353,7 @@ def matchre(self, pattern):
return token

def _scanre(self, pattern):
if isinstance(pattern, RETYPE):
if isinstance(pattern, re.Pattern):
cre = pattern
else:
cre = re.compile(pattern, re.MULTILINE)
Expand Down
6 changes: 3 additions & 3 deletions tatsu/codegen/objectmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ def _get_full_name(cls):
# Try to reference the class
try:
idents = name.split('.')
_cls = getattr(module, idents[0])
cls_ = getattr(module, idents[0])
for ident in idents[1:]:
_cls = getattr(_cls, ident)
cls_ = getattr(cls_, ident)

assert _cls == cls
assert cls_ == cls
except AttributeError as e:
raise CodegenError(
"Couldn't find base type, it has to be importable",
Expand Down
5 changes: 3 additions & 2 deletions tatsu/codegen/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
"""
from __future__ import annotations

import re
import textwrap

from .. import grammars
from ..collections import OrderedSet as oset
from ..exceptions import CodegenError
from ..objectmodel import BASE_CLASS_TOKEN, Node
from ..util import RETYPE, compress_seq, indent, safe_name, timestamp, trim
from ..util import compress_seq, indent, safe_name, timestamp, trim
from .cgbase import CodeGenerator, ModelRenderer


Expand Down Expand Up @@ -448,7 +449,7 @@ def render_fields(self, fields):
whitespace = self.node.config.whitespace
if not whitespace:
whitespace = 'None'
elif isinstance(whitespace, RETYPE):
elif isinstance(whitespace, re.Pattern):
whitespace = repr(whitespace)
else:
whitespace = f're.compile(r"{whitespace}")'
Expand Down
2 changes: 1 addition & 1 deletion tatsu/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def isname(impl):
return impl


class closure(list): # noqa: FURB189
class closure(list):
def __hash__(self):
return hash(tuple(self))

Expand Down
2 changes: 1 addition & 1 deletion tatsu/g2e/semantics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

def camel2py(name):
return re.sub(
'([a-z0-9])([A-Z])',
r'([a-z0-9])([A-Z])',
lambda m: m.group(1) + '_' + m.group(2).lower(),
name,
)
Expand Down
4 changes: 2 additions & 2 deletions tatsu/grammars.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
PRAGMA_RE = r'^\s*#include.*$'


class _ref(str): # noqa: FURB189
class _ref(str):
def __repr__(self):
return f'<{self}>'

Expand Down Expand Up @@ -519,7 +519,7 @@ def _to_str(self, lean=False):

if multi:
return '\n|\n'.join(indent(o) for o in options)
elif len(options) and len(single) > PEP8_LLEN:
elif options and len(single) > PEP8_LLEN:
return '| ' + '\n| '.join(o for o in options)
else:
return single
Expand Down
2 changes: 1 addition & 1 deletion tatsu/infos.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .util.unicode_characters import C_DERIVE


class UndefinedStr(str): # noqa: FURB189
class UndefinedStr(str):
pass


Expand Down
3 changes: 0 additions & 3 deletions tatsu/util/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@
logger.addHandler(ch)


RETYPE = type(re.compile('.'))


ESCAPE_SEQUENCE_RE = re.compile(
r"""
( \\U........ # 8-digit Unicode escapes
Expand Down
4 changes: 1 addition & 3 deletions tatsu/util/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
from collections.abc import Iterable
from typing import TypeVar

from ._common import RETYPE

_T = TypeVar('_T')

_undefined = object() # unique object for when None is not a good default
Expand Down Expand Up @@ -62,7 +60,7 @@ def findalliter(pattern, string, pos=None, endpos=None, flags=0):
"""
r = (
pattern
if isinstance(pattern, RETYPE)
if isinstance(pattern, re.Pattern)
else re.compile(pattern, flags=flags)
)
if endpos is not None:
Expand Down
2 changes: 1 addition & 1 deletion tatsu/walkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def pythonize_match(m):

# walk__pythonic_name with double underscore after walk
pythonic_name = re.sub(
'[A-Z]+', pythonize_match, node_cls.__name__,
r'[A-Z]+', pythonize_match, node_cls.__name__,
)
if pythonic_name != cammelcase_name:
walker = getattr(cls, prefix + pythonic_name, None)
Expand Down
7 changes: 3 additions & 4 deletions test/parser_equivalence_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,9 @@ def test_none_whitespace():
output = parser.parse(input, parseinfo=False)
assert output == ('This is a', ' test')


def test_sep_join():
grammar = """
grammar = r"""
@@grammar::numbers
start
Expand All @@ -186,6 +187,4 @@ def test_sep_join():
digit = /\d+/ ;
"""
parser = generate_and_load_parser('W', grammar)
ast = parser.parse('1,2,3,4', nameguard=False)


parser.parse('1,2,3,4', nameguard=False)
4 changes: 1 addition & 3 deletions test/zzz_bootstrap/bootstrap_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import unittest
from pathlib import Path

from tatsu import util
from tatsu.ngcodegen import codegen
from tatsu.parser import EBNFParser, GrammarGenerator
from tatsu.parser_semantics import EBNFGrammarSemantics
Expand Down Expand Up @@ -163,8 +162,7 @@ def walk_default(self, o, children):
except ImportError:
print('PyGraphViz not found!')
else:
if not util.PY37:
draw('./tmp/13.png', g11)
draw('./tmp/13.png', g11)


def suite():
Expand Down

0 comments on commit 1d994c5

Please sign in to comment.