[3.14] gh-138281: Run ruff on Tools/peg_generator (GH-138282) (#138469)

Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
This commit is contained in:
sobolevn 2025-09-04 14:13:36 +03:00 committed by GitHub
parent 2583646288
commit 8e1c2fe3d3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
16 changed files with 251 additions and 237 deletions

View file

@ -1,15 +1,7 @@
from __future__ import annotations
from typing import (
AbstractSet,
Any,
Iterable,
Iterator,
List,
Optional,
Tuple,
Union,
)
from collections.abc import Iterable, Iterator, Set
from typing import Any
class GrammarError(Exception):
@ -34,7 +26,7 @@ def generic_visit(self, node: Iterable[Any], *args: Any, **kwargs: Any) -> Any:
class Grammar:
def __init__(self, rules: Iterable[Rule], metas: Iterable[Tuple[str, Optional[str]]]):
def __init__(self, rules: Iterable[Rule], metas: Iterable[tuple[str, str | None]]):
# Check if there are repeated rules in "rules"
all_rules = {}
for rule in rules:
@ -66,7 +58,7 @@ def __iter__(self) -> Iterator[Rule]:
class Rule:
def __init__(self, name: str, type: Optional[str], rhs: Rhs, memo: Optional[object] = None):
def __init__(self, name: str, type: str | None, rhs: Rhs, memo: object | None = None):
self.name = name
self.type = type
self.rhs = rhs
@ -141,9 +133,9 @@ def __repr__(self) -> str:
class Rhs:
def __init__(self, alts: List[Alt]):
def __init__(self, alts: list[Alt]):
self.alts = alts
self.memo: Optional[Tuple[Optional[str], str]] = None
self.memo: tuple[str | None, str] | None = None
def __str__(self) -> str:
return " | ".join(str(alt) for alt in self.alts)
@ -151,7 +143,7 @@ def __str__(self) -> str:
def __repr__(self) -> str:
return f"Rhs({self.alts!r})"
def __iter__(self) -> Iterator[List[Alt]]:
def __iter__(self) -> Iterator[list[Alt]]:
yield self.alts
@property
@ -165,7 +157,7 @@ def can_be_inlined(self) -> bool:
class Alt:
def __init__(self, items: List[NamedItem], *, icut: int = -1, action: Optional[str] = None):
def __init__(self, items: list[NamedItem], *, icut: int = -1, action: str | None = None):
self.items = items
self.icut = icut
self.action = action
@ -185,12 +177,12 @@ def __repr__(self) -> str:
args.append(f"action={self.action!r}")
return f"Alt({', '.join(args)})"
def __iter__(self) -> Iterator[List[NamedItem]]:
def __iter__(self) -> Iterator[list[NamedItem]]:
yield self.items
class NamedItem:
def __init__(self, name: Optional[str], item: Item, type: Optional[str] = None):
def __init__(self, name: str | None, item: Item, type: str | None = None):
self.name = name
self.item = item
self.type = type
@ -271,7 +263,7 @@ class Repeat:
def __init__(self, node: Plain):
self.node = node
self.memo: Optional[Tuple[Optional[str], str]] = None
self.memo: tuple[str | None, str] | None = None
def __iter__(self) -> Iterator[Plain]:
yield self.node
@ -334,12 +326,12 @@ def __init__(self) -> None:
pass
def __repr__(self) -> str:
return f"Cut()"
return "Cut()"
def __str__(self) -> str:
return f"~"
return "~"
def __iter__(self) -> Iterator[Tuple[str, str]]:
def __iter__(self) -> Iterator[tuple[str, str]]:
yield from ()
def __eq__(self, other: object) -> bool:
@ -347,15 +339,15 @@ def __eq__(self, other: object) -> bool:
return NotImplemented
return True
def initial_names(self) -> AbstractSet[str]:
def initial_names(self) -> Set[str]:
return set()
Plain = Union[Leaf, Group]
Item = Union[Plain, Opt, Repeat, Forced, Lookahead, Rhs, Cut]
RuleName = Tuple[str, Optional[str]]
MetaTuple = Tuple[str, Optional[str]]
MetaList = List[MetaTuple]
RuleList = List[Rule]
NamedItemList = List[NamedItem]
LookaheadOrCut = Union[Lookahead, Cut]
Plain = Leaf | Group
Item = Plain | Opt | Repeat | Forced | Lookahead | Rhs | Cut
RuleName = tuple[str, str | None]
MetaTuple = tuple[str, str | None]
MetaList = list[MetaTuple]
RuleList = list[Rule]
NamedItemList = list[NamedItem]
LookaheadOrCut = Lookahead | Cut