Skip to content

Commit

Permalink
Tweaks for Python 3.8 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dnicolodi committed May 27, 2024
1 parent 1263277 commit 7cb0a35
Show file tree
Hide file tree
Showing 13 changed files with 39 additions and 17 deletions.
2 changes: 2 additions & 0 deletions tatsu/bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

# ruff: noqa: C405, COM812, I001, F401, PLR1702, PLC2801, SIM117

from __future__ import annotations

import sys
from pathlib import Path

Expand Down
9 changes: 4 additions & 5 deletions tatsu/collections/orderedset.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
# NOTE: from https://github.com/LuminosoInsight/ordered-set/blob/master/ordered_set.py
from __future__ import annotations

import itertools
from collections.abc import (
Iterable,
Iterator,
Mapping,
MutableSequence,
MutableSet,
Sequence,
)
from typing import Any, TypeVar
from typing import Any, TypeVar, MutableSet, Sequence, Iterable

T = TypeVar('T')

Expand Down Expand Up @@ -81,7 +80,7 @@ def __eq__(self, other: Any) -> bool:
def union(self, *other: Iterable[T]) -> 'OrderedSet[T]':
# do not split `str`
outer = tuple(
[o] if not isinstance(o, set | Mapping | MutableSequence) else o
[o] if not isinstance(o, (set, Mapping, MutableSequence)) else o
for o in other
)
inner = itertools.chain([self], *outer)
Expand Down
2 changes: 1 addition & 1 deletion tatsu/grammars.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,7 +865,7 @@ def _nullable(self):

@staticmethod
def param_repr(p):
if isinstance(p, int | float) or (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 Down
13 changes: 12 additions & 1 deletion tatsu/infos.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import copy
import dataclasses
import re
import sys

from collections.abc import Callable, Mapping
from itertools import starmap
from typing import Any, NamedTuple
Expand All @@ -11,6 +13,12 @@
from .tokenizing import Tokenizer
from .util.unicode_characters import C_DERIVE

if sys.version_info < (3, 10):
import builtins

def zip(*iterables, strict=False):
return builtins.zip(*iterables)


class UndefinedStr(str):
pass
Expand Down Expand Up @@ -234,7 +242,10 @@ class RuleResult(NamedTuple):
newstate: Any


@dataclasses.dataclass(slots=True)
import sys


@dataclasses.dataclass(**({'slots': True} if sys.version_info >= (3, 10) else {}))
class ParseState:
pos: int = 0
ast: AST = dataclasses.field(default_factory=AST)
Expand Down
2 changes: 2 additions & 0 deletions tatsu/mixins/indent.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import io
from contextlib import contextmanager

Expand Down
2 changes: 2 additions & 0 deletions tatsu/ngcodegen/objectmodel.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import builtins
from collections import namedtuple

Expand Down
4 changes: 3 additions & 1 deletion tatsu/ngcodegen/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
# ruff: noqa: C405, COM812, I001, F401, PLR1702, PLC2801, SIM117
from __future__ import annotations
import sys
from pathlib import Path
Expand Down Expand Up @@ -94,7 +96,7 @@ def walk_Grammar(self, grammar: grammars.Grammar):

def walk_Rule(self, rule: grammars.Rule):
def param_repr(p):
if isinstance(p, int | float):
if isinstance(p, (int, float)):
return str(p)
else:
return repr(p.split('::')[0])
Expand Down
4 changes: 2 additions & 2 deletions tatsu/objectmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def with_parent(node):
return node

def children_of(child):
if isinstance(child, weakref.ReferenceType | weakref.ProxyType):
if isinstance(child, (weakref.ReferenceType, weakref.ProxyType)):
return
elif isinstance(child, Node):
yield with_parent(child)
Expand All @@ -121,7 +121,7 @@ def children_of(child):
if name.startswith('_'):
continue
yield from children_of(value)
elif isinstance(child, list | tuple):
elif isinstance(child, (list, tuple)):
yield from (
with_parent(c) for c in child if isinstance(c, Node)
)
Expand Down
2 changes: 2 additions & 0 deletions tatsu/parser.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import re
from typing import Any

Expand Down
2 changes: 2 additions & 0 deletions tatsu/synth.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

from collections.abc import Mapping
from typing import Any

Expand Down
10 changes: 5 additions & 5 deletions tatsu/util/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ def decode_match(match):

def isiter(value):
return isinstance(value, Iterable) and not isinstance(
value, str | bytes | bytearray,
value, (str, bytes, bytearray),
)


Expand Down Expand Up @@ -241,19 +241,19 @@ def timestamp():


def asjson(obj, seen=None): # noqa: PLR0911, PLR0912
if obj is None or isinstance(obj, int | float | str | bool):
if obj is None or isinstance(obj, (int, float, str, bool)):
return obj

if seen is None:
seen = set()
elif id(obj) in seen:
return f'{type(obj).__name__}@{id(obj)}'

if isinstance(obj, Mapping | AsJSONMixin) or isiter(obj):
if isinstance(obj, (Mapping, AsJSONMixin)) or isiter(obj):
seen.add(id(obj))

try:
if isinstance(obj, weakref.ReferenceType | weakref.ProxyType):
if isinstance(obj, (weakref.ReferenceType, weakref.ProxyType)):
return f'{obj.__class__.__name__}@0x{hex(id(obj)).upper()[2:]}'
elif hasattr(obj, '__json__'):
return obj.__json__(seen=seen)
Expand Down Expand Up @@ -304,7 +304,7 @@ def plainjson(obj):
for name, value in obj.items()
if name not in {'__class__', 'parseinfo'}
}
elif isinstance(obj, weakref.ReferenceType | weakref.ProxyType):
elif isinstance(obj, (weakref.ReferenceType, weakref.ProxyType)):
return '@ref'
elif isinstance(obj, str) and obj.startswith('@'):
return '@ref'
Expand Down
2 changes: 1 addition & 1 deletion tatsu/walkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def __init__(self):
)._walker_cache # pylint: disable=no-member

def walk(self, node: Node | list[Node], *args, **kwargs) -> Any:
if isinstance(node, list | tuple):
if isinstance(node, (list, tuple)):
return [self.walk(n, *args, **kwargs) for n in node]

if isinstance(node, Mapping):
Expand Down
2 changes: 1 addition & 1 deletion test/grammar/semantics_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def test_builder_basetype_codegen(self):
self.assertTrue(hasattr(ast, 'a'))
self.assertTrue(hasattr(ast, 'b'))

self.assertTrue(issubclass(D, A | B | C))
self.assertTrue(issubclass(D, (A, B, C)))

def test_optional_attributes(self):
grammar = r"""
Expand Down

0 comments on commit 7cb0a35

Please sign in to comment.