Convert legacy string formatting to f-strings (#4685)
* the changes * Update driver.py
This commit is contained in:
parent
e5e5dad792
commit
7987951e24
@ -77,7 +77,7 @@ def blackify(base_branch: str, black_command: str, logger: logging.Logger) -> in
|
|||||||
git("commit", "--allow-empty", "-aqC", commit)
|
git("commit", "--allow-empty", "-aqC", commit)
|
||||||
|
|
||||||
for commit in commits:
|
for commit in commits:
|
||||||
git("branch", "-qD", "%s-black" % commit)
|
git("branch", "-qD", f"{commit}-black")
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
@ -28,16 +28,16 @@ def escape(m: re.Match[str]) -> str:
|
|||||||
if tail.startswith("x"):
|
if tail.startswith("x"):
|
||||||
hexes = tail[1:]
|
hexes = tail[1:]
|
||||||
if len(hexes) < 2:
|
if len(hexes) < 2:
|
||||||
raise ValueError("invalid hex string escape ('\\%s')" % tail)
|
raise ValueError(f"invalid hex string escape ('\\{tail}')")
|
||||||
try:
|
try:
|
||||||
i = int(hexes, 16)
|
i = int(hexes, 16)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise ValueError("invalid hex string escape ('\\%s')" % tail) from None
|
raise ValueError(f"invalid hex string escape ('\\{tail}')") from None
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
i = int(tail, 8)
|
i = int(tail, 8)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
raise ValueError("invalid octal string escape ('\\%s')" % tail) from None
|
raise ValueError(f"invalid octal string escape ('\\{tail}')") from None
|
||||||
return chr(i)
|
return chr(i)
|
||||||
|
|
||||||
|
|
||||||
|
@ -140,7 +140,7 @@ def calcfirst(self, name: str) -> None:
|
|||||||
if label in self.first:
|
if label in self.first:
|
||||||
fset = self.first[label]
|
fset = self.first[label]
|
||||||
if fset is None:
|
if fset is None:
|
||||||
raise ValueError("recursion for rule %r" % name)
|
raise ValueError(f"recursion for rule {name!r}")
|
||||||
else:
|
else:
|
||||||
self.calcfirst(label)
|
self.calcfirst(label)
|
||||||
fset = self.first[label]
|
fset = self.first[label]
|
||||||
@ -155,8 +155,8 @@ def calcfirst(self, name: str) -> None:
|
|||||||
for symbol in itsfirst:
|
for symbol in itsfirst:
|
||||||
if symbol in inverse:
|
if symbol in inverse:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"rule %s is ambiguous; %s is in the first sets of %s as well"
|
f"rule {name} is ambiguous; {symbol} is in the first sets of"
|
||||||
" as %s" % (name, symbol, label, inverse[symbol])
|
f" {label} as well as {inverse[symbol]}"
|
||||||
)
|
)
|
||||||
inverse[symbol] = label
|
inverse[symbol] = label
|
||||||
self.first[name] = totalset
|
self.first[name] = totalset
|
||||||
@ -237,16 +237,16 @@ def dump_nfa(self, name: str, start: "NFAState", finish: "NFAState") -> None:
|
|||||||
j = len(todo)
|
j = len(todo)
|
||||||
todo.append(next)
|
todo.append(next)
|
||||||
if label is None:
|
if label is None:
|
||||||
print(" -> %d" % j)
|
print(f" -> {j}")
|
||||||
else:
|
else:
|
||||||
print(" %s -> %d" % (label, j))
|
print(f" {label} -> {j}")
|
||||||
|
|
||||||
def dump_dfa(self, name: str, dfa: Sequence["DFAState"]) -> None:
|
def dump_dfa(self, name: str, dfa: Sequence["DFAState"]) -> None:
|
||||||
print("Dump of DFA for", name)
|
print("Dump of DFA for", name)
|
||||||
for i, state in enumerate(dfa):
|
for i, state in enumerate(dfa):
|
||||||
print(" State", i, state.isfinal and "(final)" or "")
|
print(" State", i, state.isfinal and "(final)" or "")
|
||||||
for label, next in sorted(state.arcs.items()):
|
for label, next in sorted(state.arcs.items()):
|
||||||
print(" %s -> %d" % (label, dfa.index(next)))
|
print(f" {label} -> {dfa.index(next)}")
|
||||||
|
|
||||||
def simplify_dfa(self, dfa: list["DFAState"]) -> None:
|
def simplify_dfa(self, dfa: list["DFAState"]) -> None:
|
||||||
# This is not theoretically optimal, but works well enough.
|
# This is not theoretically optimal, but works well enough.
|
||||||
@ -330,15 +330,12 @@ def parse_atom(self) -> tuple["NFAState", "NFAState"]:
|
|||||||
return a, z
|
return a, z
|
||||||
else:
|
else:
|
||||||
self.raise_error(
|
self.raise_error(
|
||||||
"expected (...) or NAME or STRING, got %s/%s", self.type, self.value
|
f"expected (...) or NAME or STRING, got {self.type}/{self.value}"
|
||||||
)
|
)
|
||||||
raise AssertionError
|
|
||||||
|
|
||||||
def expect(self, type: int, value: Optional[Any] = None) -> str:
|
def expect(self, type: int, value: Optional[Any] = None) -> str:
|
||||||
if self.type != type or (value is not None and self.value != value):
|
if self.type != type or (value is not None and self.value != value):
|
||||||
self.raise_error(
|
self.raise_error(f"expected {type}/{value}, got {self.type}/{self.value}")
|
||||||
"expected %s/%s, got %s/%s", type, value, self.type, self.value
|
|
||||||
)
|
|
||||||
value = self.value
|
value = self.value
|
||||||
self.gettoken()
|
self.gettoken()
|
||||||
return value
|
return value
|
||||||
@ -350,12 +347,7 @@ def gettoken(self) -> None:
|
|||||||
self.type, self.value, self.begin, self.end, self.line = tup
|
self.type, self.value, self.begin, self.end, self.line = tup
|
||||||
# print token.tok_name[self.type], repr(self.value)
|
# print token.tok_name[self.type], repr(self.value)
|
||||||
|
|
||||||
def raise_error(self, msg: str, *args: Any) -> NoReturn:
|
def raise_error(self, msg: str) -> NoReturn:
|
||||||
if args:
|
|
||||||
try:
|
|
||||||
msg = msg % args
|
|
||||||
except Exception:
|
|
||||||
msg = " ".join([msg] + list(map(str, args)))
|
|
||||||
raise SyntaxError(
|
raise SyntaxError(
|
||||||
msg, (str(self.filename), self.end[0], self.end[1], self.line)
|
msg, (str(self.filename), self.end[0], self.end[1], self.line)
|
||||||
)
|
)
|
||||||
|
@ -206,9 +206,7 @@ def printtoken(
|
|||||||
) -> None: # for testing
|
) -> None: # for testing
|
||||||
(srow, scol) = srow_col
|
(srow, scol) = srow_col
|
||||||
(erow, ecol) = erow_col
|
(erow, ecol) = erow_col
|
||||||
print(
|
print(f"{srow},{scol}-{erow},{ecol}:\t{tok_name[type]}\t{token!r}")
|
||||||
"%d,%d-%d,%d:\t%s\t%s" % (srow, scol, erow, ecol, tok_name[type], repr(token))
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__": # testing
|
if __name__ == "__main__": # testing
|
||||||
|
@ -268,11 +268,7 @@ def __init__(
|
|||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
"""Return a canonical string representation."""
|
"""Return a canonical string representation."""
|
||||||
assert self.type is not None
|
assert self.type is not None
|
||||||
return "{}({}, {!r})".format(
|
return f"{self.__class__.__name__}({type_repr(self.type)}, {self.children!r})"
|
||||||
self.__class__.__name__,
|
|
||||||
type_repr(self.type),
|
|
||||||
self.children,
|
|
||||||
)
|
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
"""
|
"""
|
||||||
@ -421,10 +417,9 @@ def __repr__(self) -> str:
|
|||||||
from .pgen2.token import tok_name
|
from .pgen2.token import tok_name
|
||||||
|
|
||||||
assert self.type is not None
|
assert self.type is not None
|
||||||
return "{}({}, {!r})".format(
|
return (
|
||||||
self.__class__.__name__,
|
f"{self.__class__.__name__}({tok_name.get(self.type, self.type)},"
|
||||||
tok_name.get(self.type, self.type),
|
f" {self.value!r})"
|
||||||
self.value,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def __str__(self) -> str:
|
def __str__(self) -> str:
|
||||||
@ -527,7 +522,7 @@ def __repr__(self) -> str:
|
|||||||
args = [type_repr(self.type), self.content, self.name]
|
args = [type_repr(self.type), self.content, self.name]
|
||||||
while args and args[-1] is None:
|
while args and args[-1] is None:
|
||||||
del args[-1]
|
del args[-1]
|
||||||
return "{}({})".format(self.__class__.__name__, ", ".join(map(repr, args)))
|
return f"{self.__class__.__name__}({', '.join(map(repr, args))})"
|
||||||
|
|
||||||
def _submatch(self, node, results=None) -> bool:
|
def _submatch(self, node, results=None) -> bool:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
Loading…
Reference in New Issue
Block a user