chore: update typing to be generic on function args

Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
This commit is contained in:
Henry Schreiner
2023-04-18 13:06:17 -04:00
parent 6d038dfbdf
commit 8c5f89c035
12 changed files with 48 additions and 39 deletions
+7 -5
View File
@@ -1,7 +1,7 @@
from __future__ import annotations
import subprocess
from collections.abc import Sequence
from collections.abc import Iterable, Mapping, Sequence
from dataclasses import dataclass
from typing import Callable, Dict, List # noqa: TID251
@@ -11,7 +11,7 @@ import bashlex
EnvironmentExecutor = Callable[[List[str], Dict[str, str]], str]
def local_environment_executor(command: list[str], env: dict[str, str]) -> str:
def local_environment_executor(command: Sequence[str], env: Mapping[str, str]) -> str:
return subprocess.run(command, env=env, text=True, stdout=subprocess.PIPE, check=True).stdout
@@ -23,7 +23,7 @@ class NodeExecutionContext:
def evaluate(
value: str, environment: dict[str, str], executor: EnvironmentExecutor | None = None
value: str, environment: Mapping[str, str], executor: EnvironmentExecutor | None = None
) -> str:
if not value:
# empty string evaluates to empty string
@@ -41,7 +41,9 @@ def evaluate(
return evaluate_node(
value_word_node,
context=NodeExecutionContext(
environment=environment, input=value, executor=executor or local_environment_executor
environment=dict(environment),
input=value,
executor=executor or local_environment_executor,
),
)
@@ -106,7 +108,7 @@ def evaluate_nodes_as_compound_command(
def evaluate_nodes_as_simple_command(
nodes: list[bashlex.ast.node], context: NodeExecutionContext
nodes: Iterable[bashlex.ast.node], context: NodeExecutionContext
) -> str:
command = [evaluate_node(part, context=context) for part in nodes]
return context.executor(command, context.environment)