Skip to content

Commit 7afbd15

Browse files
Add CWLParameterContext as a TypedDict
This commit further modernizes the CWL type system. It also adds a `TypedDict` class to represent the CWL parameter context used for expression evaluation.
1 parent 09eae9f commit 7afbd15

File tree

5 files changed

+80
-80
lines changed

5 files changed

+80
-80
lines changed

cwl_utils/cwl_v1_0_expression_refactor.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
import cwl_utils.parser.cwl_v1_0_utils as utils
1818
from cwl_utils.errors import JavascriptException, WorkflowException
1919
from cwl_utils.expression import do_eval, interpolate
20-
from cwl_utils.types import CWLObjectType, CWLOutputType
20+
from cwl_utils.types import (
21+
CWLObjectType,
22+
CWLOutputType,
23+
CWLParameterContext,
24+
CWLRuntimeParameterContext,
25+
)
2126

2227

2328
def expand_stream_shortcuts(process: cwl.CommandLineTool) -> cwl.CommandLineTool:
@@ -85,14 +90,9 @@ def get_expression(
8590
if string.strip().startswith("${"):
8691
return string
8792
if "$(" in string:
88-
runtime: CWLObjectType = {
89-
"cores": 0,
90-
"ram": 0,
91-
"outdir": "/root",
92-
"tmpdir": "/tmp", # nosec
93-
"outdirSize": 0,
94-
"tmpdirSize": 0,
95-
}
93+
runtime = CWLRuntimeParameterContext(
94+
cores=0, ram=0, outdir="/root", tmpdir="/tmp", outdirSize=0, tmpdirSize=0
95+
)
9696
try:
9797
do_eval(
9898
string,
@@ -114,11 +114,9 @@ def get_expression(
114114
str,
115115
interpolate(
116116
scan=string,
117-
rootvars={
118-
"inputs": inputs,
119-
"context": self,
120-
"runtime": runtime,
121-
},
117+
rootvars=CWLParameterContext(
118+
inputs=inputs, self=self, runtime=runtime
119+
),
122120
fullJS=True,
123121
escaping_behavior=2,
124122
convert_to_expression=True,

cwl_utils/cwl_v1_1_expression_refactor.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
import cwl_utils.parser.cwl_v1_1_utils as utils
1818
from cwl_utils.errors import JavascriptException, WorkflowException
1919
from cwl_utils.expression import do_eval, interpolate
20-
from cwl_utils.types import CWLObjectType, CWLOutputType
20+
from cwl_utils.types import (
21+
CWLObjectType,
22+
CWLOutputType,
23+
CWLParameterContext,
24+
CWLRuntimeParameterContext,
25+
)
2126

2227

2328
def expand_stream_shortcuts(process: cwl.CommandLineTool) -> cwl.CommandLineTool:
@@ -85,14 +90,9 @@ def get_expression(
8590
if string.strip().startswith("${"):
8691
return string
8792
if "$(" in string:
88-
runtime: CWLObjectType = {
89-
"cores": 0,
90-
"ram": 0,
91-
"outdir": "/root",
92-
"tmpdir": "/tmp", # nosec
93-
"outdirSize": 0,
94-
"tmpdirSize": 0,
95-
}
93+
runtime = CWLRuntimeParameterContext(
94+
cores=0, ram=0, outdir="/root", tmpdir="/tmp", outdirSize=0, tmpdirSize=0
95+
)
9696
try:
9797
do_eval(
9898
string,
@@ -114,11 +114,9 @@ def get_expression(
114114
str,
115115
interpolate(
116116
scan=string,
117-
rootvars={
118-
"inputs": inputs,
119-
"context": self,
120-
"runtime": runtime,
121-
},
117+
rootvars=CWLParameterContext(
118+
inputs=inputs, self=self, runtime=runtime
119+
),
122120
fullJS=True,
123121
escaping_behavior=2,
124122
convert_to_expression=True,

cwl_utils/cwl_v1_2_expression_refactor.py

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@
1717
import cwl_utils.parser.cwl_v1_2_utils as utils
1818
from cwl_utils.errors import JavascriptException, WorkflowException
1919
from cwl_utils.expression import do_eval, interpolate
20-
from cwl_utils.types import CWLObjectType, CWLOutputType
20+
from cwl_utils.types import (
21+
CWLObjectType,
22+
CWLOutputType,
23+
CWLParameterContext,
24+
CWLRuntimeParameterContext,
25+
)
2126

2227

2328
def expand_stream_shortcuts(process: cwl.CommandLineTool) -> cwl.CommandLineTool:
@@ -85,14 +90,9 @@ def get_expression(
8590
if string.strip().startswith("${"):
8691
return string
8792
if "$(" in string:
88-
runtime: CWLObjectType = {
89-
"cores": 0,
90-
"ram": 0,
91-
"outdir": "/root",
92-
"tmpdir": "/tmp", # nosec
93-
"outdirSize": 0,
94-
"tmpdirSize": 0,
95-
}
93+
runtime = CWLRuntimeParameterContext(
94+
cores=0, ram=0, outdir="/root", tmpdir="/tmp", outdirSize=0, tmpdirSize=0
95+
)
9696
try:
9797
do_eval(
9898
string,
@@ -114,11 +114,9 @@ def get_expression(
114114
str,
115115
interpolate(
116116
scan=string,
117-
rootvars={
118-
"inputs": inputs,
119-
"context": self,
120-
"runtime": runtime,
121-
},
117+
rootvars=CWLParameterContext(
118+
inputs=inputs, self=self, runtime=runtime
119+
),
122120
fullJS=True,
123121
escaping_behavior=2,
124122
convert_to_expression=True,

cwl_utils/expression.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@
66
import json
77
from collections.abc import Awaitable, MutableMapping
88
from enum import Enum
9-
from typing import Any, Union, cast
9+
from typing import Any, Literal, Union, cast
1010

1111
from schema_salad.utils import json_dumps
1212

1313
from cwl_utils.errors import JavascriptException, SubstitutionError, WorkflowException
1414
from cwl_utils.loghandler import _logger
1515
from cwl_utils.sandboxjs import JSEngine, default_timeout, get_js_engine, param_re
16-
from cwl_utils.types import CWLObjectType, CWLOutputType
16+
from cwl_utils.types import CWLObjectType, CWLOutputType, CWLParameterContext
1717
from cwl_utils.utils import bytes2str_in_dicts
1818

1919

@@ -108,7 +108,7 @@ def scanner(scan: str) -> tuple[int, int] | None:
108108
def evaluator(
109109
js_engine: JSEngine,
110110
ex: str,
111-
obj: CWLObjectType,
111+
obj: CWLParameterContext,
112112
jslib: str,
113113
fullJS: bool,
114114
**kwargs: Any,
@@ -133,7 +133,7 @@ def evaluator(
133133
js_engine.regex_eval(
134134
first_symbol,
135135
ex[first_symbol_end:-1],
136-
cast(CWLOutputType, obj[first_symbol]),
136+
cast(CWLOutputType, obj[first_symbol]), # type: ignore[literal-required]
137137
**kwargs,
138138
),
139139
)
@@ -144,7 +144,7 @@ def evaluator(
144144
js_engine.regex_eval(
145145
first_symbol,
146146
ex[first_symbol_end:-1],
147-
cast(CWLOutputType, obj[first_symbol]),
147+
cast(CWLOutputType, obj[first_symbol]), # type: ignore[literal-required]
148148
**kwargs,
149149
),
150150
)
@@ -174,7 +174,7 @@ def evaluator(
174174

175175
def interpolate(
176176
scan: str,
177-
rootvars: CWLObjectType,
177+
rootvars: CWLParameterContext,
178178
jslib: str = "",
179179
fullJS: bool = False,
180180
strip_whitespace: bool = True,
@@ -258,7 +258,7 @@ def dump(string: str) -> str:
258258
return "".join(parts)
259259

260260

261-
def jshead(engine_config: list[str], rootvars: CWLObjectType) -> str:
261+
def jshead(engine_config: list[str], rootvars: CWLParameterContext) -> str:
262262
"""Make sure all the byte strings are converted to str in `rootvars` dict."""
263263
return "\n".join(
264264
engine_config
@@ -293,7 +293,7 @@ def do_eval(
293293
runtime["outdir"] = outdir or None
294294

295295
rootvars = cast(
296-
CWLObjectType,
296+
CWLParameterContext,
297297
bytes2str_in_dicts({"inputs": jobinput, "self": context, "runtime": runtime}),
298298
)
299299

cwl_utils/types.py

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
# From https://github.com/rabix/sbpack/blob/b8404a0859ffcbe1edae6d8f934e51847b003320/sbpack/lib.py
33
"""Shared Python type definitions for commons JSON like CWL objects."""
44
from collections.abc import MutableMapping, MutableSequence
5-
from typing import Any, Optional, Union
5+
from typing import TypeAlias, TypedDict
66

7-
built_in_types = [
7+
built_in_types: list[str] = [
88
"null",
99
"boolean",
1010
"int",
@@ -21,31 +21,37 @@
2121
]
2222

2323

24-
CWLOutputAtomType = Union[
25-
None,
26-
bool,
27-
str,
28-
int,
29-
float,
30-
MutableSequence[
31-
Union[
32-
None, bool, str, int, float, MutableSequence[Any], MutableMapping[str, Any]
33-
]
34-
],
35-
MutableMapping[
36-
str,
37-
Union[
38-
None, bool, str, int, float, MutableSequence[Any], MutableMapping[str, Any]
39-
],
40-
],
41-
]
42-
CWLOutputType = Union[
43-
bool,
44-
str,
45-
int,
46-
float,
47-
MutableSequence[CWLOutputAtomType],
48-
MutableMapping[str, CWLOutputAtomType],
49-
]
50-
CWLObjectType = MutableMapping[str, Optional[CWLOutputType]]
51-
SinkType = Union[CWLOutputType, CWLObjectType]
24+
CWLOutputAtomType: TypeAlias = (
25+
None
26+
| bool
27+
| str
28+
| int
29+
| float
30+
| MutableSequence["CWLOutputAtomType"]
31+
| MutableMapping[str, "CWLOutputAtomType"]
32+
)
33+
CWLOutputType: TypeAlias = (
34+
bool
35+
| str
36+
| int
37+
| float
38+
| MutableSequence[CWLOutputAtomType]
39+
| MutableMapping[str, CWLOutputAtomType]
40+
)
41+
CWLObjectType: TypeAlias = MutableMapping[str, CWLOutputType | None]
42+
SinkType: TypeAlias = CWLOutputType | CWLObjectType
43+
44+
45+
class CWLRuntimeParameterContext(TypedDict, total=False):
46+
outdir: str
47+
tmpdir: str
48+
cores: float | str
49+
ram: float | str
50+
outdirSize: float | str
51+
tmpdirSize: float | str
52+
53+
54+
class CWLParameterContext(TypedDict, total=False):
55+
inputs: CWLObjectType
56+
self: CWLOutputType | None
57+
runtime: CWLRuntimeParameterContext

0 commit comments

Comments
 (0)