tweak: Use Generic to support 3.11

This commit is contained in:
2026-05-31 06:31:46 +10:00
parent 4734265f2e
commit 733f3e3ae6
+43 -27
View File
@@ -1,4 +1,14 @@
from typing import Any, Awaitable, Callable, Concatenate, ParamSpec, TypeVar, Union, TYPE_CHECKING from typing import (
Any,
Generic,
Awaitable,
Callable,
Concatenate,
ParamSpec,
TypeVar,
Union,
TYPE_CHECKING,
)
from functools import wraps, reduce from functools import wraps, reduce
import operator import operator
@@ -22,9 +32,9 @@ __all__ = (
"ternary_term", "ternary_term",
"run_function", "run_function",
"cascade_term", "cascade_term",
'ExecutorTerm', "ExecutorTerm",
'LateTerm', "LateTerm",
'lazy_term', "lazy_term",
) )
P = ParamSpec("P") P = ParamSpec("P")
@@ -37,7 +47,7 @@ T2 = TypeVar("T2")
TermResult = TypeVar("TermResult") TermResult = TypeVar("TermResult")
class ExecutorTerm[TermResult]: class ExecutorTerm(Generic[TermResult]):
# May add more here for initial parsing context # May add more here for initial parsing context
def __init__(self, *args, node_text: str | None = None, **kwargs): def __init__(self, *args, node_text: str | None = None, **kwargs):
@@ -145,7 +155,7 @@ async def cmdarg_term(ctx, argn: int, greedy: bool) -> str:
if greedy: if greedy:
return " ".join(args[argn:]) return " ".join(args[argn:])
elif argn >= len(args): elif argn >= len(args):
return '' return ""
else: else:
return args[argn] return args[argn]
@@ -158,33 +168,36 @@ async def cascade_term(ctx, *terms: ExecutorTerm):
return eval return eval
return "" return ""
@lazy_term @lazy_term
async def command_shell(ctx, component_term: ExecutorTerm): async def command_shell(ctx, component_term: ExecutorTerm):
if isinstance(component_term, ExecutorTerm): if isinstance(component_term, ExecutorTerm):
result = await component_term(ctx) result = await component_term(ctx)
else: else:
logger.warning( logger.warning(f"command_shell LazyTerm stringifying non-term {component_term}")
f"command_shell LazyTerm stringifying non-term {component_term}"
)
result = component_term result = component_term
return str(result) return str(result)
@lazy_term @lazy_term
async def define_equal(ctx, var: str, value: ExecutorTerm): async def define_equal(ctx, var: str, value: ExecutorTerm):
ctx.this_scope[var] = await value.execute(ctx) ctx.this_scope[var] = await value.execute(ctx)
return None return None
@lazy_term @lazy_term
async def define_walrus(ctx, var: str, value: ExecutorTerm): async def define_walrus(ctx, var: str, value: ExecutorTerm):
result = await value.execute(ctx) result = await value.execute(ctx)
ctx.this_scope[var] = result ctx.this_scope[var] = result
return result return result
@lazy_term @lazy_term
async def define_term(ctx, var: str, value: ExecutorTerm): async def define_term(ctx, var: str, value: ExecutorTerm):
ctx.this_scope[var] = value ctx.this_scope[var] = value
return None return None
@lazy_term @lazy_term
async def run_function(ctx, var: ExecutorTerm, *args: ExecutorTerm): async def run_function(ctx, var: ExecutorTerm, *args: ExecutorTerm):
varlue = await var.execute(ctx) varlue = await var.execute(ctx)
@@ -196,13 +209,14 @@ async def run_function(ctx, var: ExecutorTerm, *args: ExecutorTerm):
else: else:
return await varlue(ctx, *argues) return await varlue(ctx, *argues)
@lazy_term @lazy_term
async def get_var(ctx, root: str | ExecutorTerm, *attrs: str): async def get_var(ctx, root: str | ExecutorTerm, *attrs: str):
if isinstance(root, ExecutorTerm): if isinstance(root, ExecutorTerm):
value = await root.execute(ctx) value = await root.execute(ctx)
name = [root.text or ''] name = [root.text or ""]
elif root not in ctx.scope: elif root not in ctx.scope:
return ' '.join((root, *attrs)) return " ".join((root, *attrs))
else: else:
value = ctx.scope[root] value = ctx.scope[root]
name = [root] name = [root]
@@ -213,39 +227,41 @@ async def get_var(ctx, root: str | ExecutorTerm, *attrs: str):
for attr in attrs: for attr in attrs:
try: try:
value = getattr(value, '_exec_attr_'+attr) value = getattr(value, "_exec_attr_" + attr)
name.append(attr) name.append(attr)
except AttributeError: except AttributeError:
raise CommandValueError(f"{'.'.join(name)} has no attribute {attr}") raise CommandValueError(f"{'.'.join(name)} has no attribute {attr}")
return value return value
@lazy_term @lazy_term
async def compare(ctx, term1: ExecutorTerm, op: str, term2: ExecutorTerm): async def compare(ctx, term1: ExecutorTerm, op: str, term2: ExecutorTerm):
opmap = { opmap = {
'==': operator.eq, "==": operator.eq,
'!=': operator.ne, "!=": operator.ne,
'>': operator.gt, ">": operator.gt,
'>=': operator.ge, ">=": operator.ge,
'<': operator.lt, "<": operator.lt,
'<=': operator.le, "<=": operator.le,
} }
value1 = await term1.execute(ctx) value1 = await term1.execute(ctx)
value2 = await term2.execute(ctx) value2 = await term2.execute(ctx)
oppy = opmap[op] oppy = opmap[op]
return oppy(value1, value2) return oppy(value1, value2)
@lazy_term @lazy_term
async def binop(ctx, op: str, *terms): async def binop(ctx, op: str, *terms):
opmap = { opmap = {
'||': operator.or_, "||": operator.or_,
'&&': operator.and_, "&&": operator.and_,
'+': operator.add, "+": operator.add,
'-': operator.sub, "-": operator.sub,
'*': operator.mul, "*": operator.mul,
'**': operator.pow, "**": operator.pow,
'/': operator.truediv, "/": operator.truediv,
'//': operator.floordiv, "//": operator.floordiv,
'%': operator.mod, "%": operator.mod,
} }
# Left associative # Left associative
oppy = opmap[op] oppy = opmap[op]