1
1
from __future__ import annotations
2
2
3
3
import subprocess
4
+ from collections .abc import Iterable , Mapping , Sequence
4
5
from dataclasses import dataclass
5
- from typing import Callable , Dict , List , Sequence
6
+ from typing import Callable , Dict , List # noqa: TID251
6
7
7
8
import bashlex
8
9
9
10
# a function that takes a command and the environment, and returns the result
10
11
EnvironmentExecutor = Callable [[List [str ], Dict [str , str ]], str ]
11
12
12
13
13
- def local_environment_executor (command : list [str ], env : dict [str , str ]) -> str :
14
+ def local_environment_executor (command : Sequence [str ], env : Mapping [str , str ]) -> str :
14
15
return subprocess .run (command , env = env , text = True , stdout = subprocess .PIPE , check = True ).stdout
15
16
16
17
@@ -22,7 +23,7 @@ class NodeExecutionContext:
22
23
23
24
24
25
def evaluate (
25
- value : str , environment : dict [str , str ], executor : EnvironmentExecutor | None = None
26
+ value : str , environment : Mapping [str , str ], executor : EnvironmentExecutor | None = None
26
27
) -> str :
27
28
if not value :
28
29
# empty string evaluates to empty string
@@ -40,7 +41,9 @@ def evaluate(
40
41
return evaluate_node (
41
42
value_word_node ,
42
43
context = NodeExecutionContext (
43
- environment = environment , input = value , executor = executor or local_environment_executor
44
+ environment = dict (environment ),
45
+ input = value ,
46
+ executor = executor or local_environment_executor ,
44
47
),
45
48
)
46
49
@@ -105,7 +108,7 @@ def evaluate_nodes_as_compound_command(
105
108
106
109
107
110
def evaluate_nodes_as_simple_command (
108
- nodes : list [bashlex .ast .node ], context : NodeExecutionContext
111
+ nodes : Iterable [bashlex .ast .node ], context : NodeExecutionContext
109
112
) -> str :
110
113
command = [evaluate_node (part , context = context ) for part in nodes ]
111
114
return context .executor (command , context .environment )
0 commit comments