diff --git a/ruff.toml b/ruff.toml index d55fd9f7..5a23da74 100644 --- a/ruff.toml +++ b/ruff.toml @@ -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 diff --git a/tatsu/ast.py b/tatsu/ast.py index bfba54c0..95db84a1 100644 --- a/tatsu/ast.py +++ b/tatsu/ast.py @@ -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): diff --git a/tatsu/buffering.py b/tatsu/buffering.py index 87358d99..bf3a58a7 100644 --- a/tatsu/buffering.py +++ b/tatsu/buffering.py @@ -23,7 +23,6 @@ ) from .tokenizing import Tokenizer from .util import ( - RETYPE, contains_sublist, extend_list, identity, @@ -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): @@ -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) diff --git a/tatsu/codegen/objectmodel.py b/tatsu/codegen/objectmodel.py index d52ea9df..bc787f59 100644 --- a/tatsu/codegen/objectmodel.py +++ b/tatsu/codegen/objectmodel.py @@ -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", diff --git a/tatsu/codegen/python.py b/tatsu/codegen/python.py index 31e0dea9..d1d2fdcd 100755 --- a/tatsu/codegen/python.py +++ b/tatsu/codegen/python.py @@ -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 @@ -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}")' diff --git a/tatsu/contexts.py b/tatsu/contexts.py index 12873fdb..50d7df30 100644 --- a/tatsu/contexts.py +++ b/tatsu/contexts.py @@ -101,7 +101,7 @@ def isname(impl): return impl -class closure(list): # noqa: FURB189 +class closure(list): def __hash__(self): return hash(tuple(self)) diff --git a/tatsu/g2e/semantics.py b/tatsu/g2e/semantics.py index 982ed777..ccf0b497 100644 --- a/tatsu/g2e/semantics.py +++ b/tatsu/g2e/semantics.py @@ -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, ) diff --git a/tatsu/grammars.py b/tatsu/grammars.py index 65def8b9..4ac6c127 100644 --- a/tatsu/grammars.py +++ b/tatsu/grammars.py @@ -22,7 +22,7 @@ PRAGMA_RE = r'^\s*#include.*$' -class _ref(str): # noqa: FURB189 +class _ref(str): def __repr__(self): return f'<{self}>' @@ -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 diff --git a/tatsu/infos.py b/tatsu/infos.py index 6ec898ad..0efb982f 100644 --- a/tatsu/infos.py +++ b/tatsu/infos.py @@ -12,7 +12,7 @@ from .util.unicode_characters import C_DERIVE -class UndefinedStr(str): # noqa: FURB189 +class UndefinedStr(str): pass diff --git a/tatsu/util/_common.py b/tatsu/util/_common.py index c0819064..48c15038 100644 --- a/tatsu/util/_common.py +++ b/tatsu/util/_common.py @@ -27,9 +27,6 @@ logger.addHandler(ch) -RETYPE = type(re.compile('.')) - - ESCAPE_SEQUENCE_RE = re.compile( r""" ( \\U........ # 8-digit Unicode escapes diff --git a/tatsu/util/misc.py b/tatsu/util/misc.py index 89688550..1abf21c1 100644 --- a/tatsu/util/misc.py +++ b/tatsu/util/misc.py @@ -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 @@ -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: diff --git a/tatsu/walkers.py b/tatsu/walkers.py index 3de070ea..7762a4d7 100644 --- a/tatsu/walkers.py +++ b/tatsu/walkers.py @@ -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) diff --git a/test/parser_equivalence_test.py b/test/parser_equivalence_test.py index 02b4367f..12b1f599 100644 --- a/test/parser_equivalence_test.py +++ b/test/parser_equivalence_test.py @@ -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 @@ -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) diff --git a/test/zzz_bootstrap/bootstrap_test.py b/test/zzz_bootstrap/bootstrap_test.py index 1fde06e3..309ebc54 100644 --- a/test/zzz_bootstrap/bootstrap_test.py +++ b/test/zzz_bootstrap/bootstrap_test.py @@ -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 @@ -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():