Skip to content

Commit 7d686d0

Browse files
Better typing
1 parent 5e47ab7 commit 7d686d0

File tree

9 files changed

+66
-46
lines changed

9 files changed

+66
-46
lines changed

cwl_utils/cwl_v1_0_expression_refactor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def get_expression(
109109
requirements=[],
110110
outdir="",
111111
tmpdir="",
112-
resources={},
112+
resources=CWLRuntimeParameterContext(),
113113
)
114114
except (WorkflowException, JavascriptException):
115115
if (
@@ -801,7 +801,7 @@ def process_workflow_reqs_and_hints(
801801
requirements=[],
802802
outdir="",
803803
tmpdir="",
804-
resources={},
804+
resources=CWLRuntimeParameterContext(),
805805
)
806806
modified = True
807807
if is_file_or_directory(expr_result):

cwl_utils/cwl_v1_1_expression_refactor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def get_expression(
109109
requirements=[],
110110
outdir="",
111111
tmpdir="",
112-
resources={},
112+
resources=CWLRuntimeParameterContext(),
113113
)
114114
except (WorkflowException, JavascriptException):
115115
if (
@@ -803,7 +803,7 @@ def process_workflow_reqs_and_hints(
803803
requirements=[],
804804
outdir="",
805805
tmpdir="",
806-
resources={},
806+
resources=CWLRuntimeParameterContext(),
807807
)
808808
modified = True
809809
if is_file_or_directory(expr_result):

cwl_utils/cwl_v1_2_expression_refactor.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def get_expression(
108108
requirements=[],
109109
outdir="",
110110
tmpdir="",
111-
resources={},
111+
resources=CWLRuntimeParameterContext(),
112112
)
113113
except (WorkflowException, JavascriptException):
114114
if (
@@ -899,7 +899,7 @@ def process_workflow_reqs_and_hints(
899899
requirements=[],
900900
outdir="",
901901
tmpdir="",
902-
resources={},
902+
resources=CWLRuntimeParameterContext(),
903903
)
904904
modified = True
905905
if (

cwl_utils/expression.py

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
# SPDX-License-Identifier: Apache-2.0
22
"""CWL Expression parsing."""
33
import asyncio
4-
import copy
54
import inspect
65
import json
7-
from collections.abc import Awaitable, MutableMapping
6+
from collections.abc import Awaitable, Container
87
from enum import Enum
9-
from typing import Any, cast
8+
from typing import Any, Final, cast
109

1110
from schema_salad.utils import json_dumps
1211

@@ -17,10 +16,20 @@
1716
CWLObjectType,
1817
CWLOutputType,
1918
CWLParameterContext,
19+
CWLRuntimeParameterContext,
2020
is_cwl_parameter_context_key,
2121
)
2222
from cwl_utils.utils import bytes2str_in_dicts
2323

24+
OLD_ESCAPE_CWL_VERSIONS: Final[Container[str]] = (
25+
"v1.0",
26+
"v1.1.0-dev1",
27+
"v1.1",
28+
"v1.2.0-dev1",
29+
"v1.2.0-dev2",
30+
"v1.2.0-dev3",
31+
)
32+
2433

2534
def _convert_dumper(string: str) -> str:
2635
return f"{json.dumps(string)} + "
@@ -281,7 +290,7 @@ def do_eval(
281290
requirements: list[CWLObjectType],
282291
outdir: str | None,
283292
tmpdir: str | None,
284-
resources: dict[str, float | int],
293+
resources: CWLRuntimeParameterContext,
285294
context: CWLOutputType | None = None,
286295
timeout: float = default_timeout,
287296
strip_whitespace: bool = True,
@@ -293,9 +302,7 @@ def do_eval(
293302
294303
:param timeout: The maximum number of seconds to wait while executing.
295304
"""
296-
runtime = cast(MutableMapping[str, int | str | None], copy.deepcopy(resources))
297-
runtime["tmpdir"] = tmpdir or None
298-
runtime["outdir"] = outdir or None
305+
runtime = resources | {"tmpdir": tmpdir or None, "outdir": outdir or None}
299306

300307
rootvars = cast(
301308
CWLParameterContext,
@@ -319,19 +326,7 @@ def do_eval(
319326
fullJS=fullJS,
320327
jslib=jslib,
321328
strip_whitespace=strip_whitespace,
322-
escaping_behavior=(
323-
1
324-
if cwlVersion
325-
in (
326-
"v1.0",
327-
"v1.1.0-dev1",
328-
"v1.1",
329-
"v1.2.0-dev1",
330-
"v1.2.0-dev2",
331-
"v1.2.0-dev3",
332-
)
333-
else 2
334-
),
329+
escaping_behavior=1 if cwlVersion in OLD_ESCAPE_CWL_VERSIONS else 2,
335330
**kwargs,
336331
)
337332

cwl_utils/parser/cwl_v1_0_utils.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ def _compare_type(type1: Any, type2: Any) -> bool:
7575
case MutableSequence(), MutableSequence():
7676
if len(type1) != len(type2):
7777
return False
78-
for t1 in type1:
79-
if not any(_compare_type(t1, t2) for t2 in type2):
78+
for t3 in type1:
79+
if not any(_compare_type(t3, t2) for t2 in type2):
8080
return False
8181
return True
8282
return bool(type1 == type2)
@@ -470,11 +470,15 @@ def param_for_source_id(
470470
sourcenames: str | list[str],
471471
parent: cwl.Workflow | None = None,
472472
scatter_context: list[tuple[int, str] | None] | None = None,
473-
) -> MutableSequence[cwl.InputParameter] | cwl.InputParameter:
473+
) -> (
474+
cwl.InputParameter
475+
| cwl.CommandOutputParameter
476+
| MutableSequence[cwl.InputParameter | cwl.CommandOutputParameter]
477+
):
474478
"""Find the process input parameter that matches one of the given sourcenames."""
475479
if isinstance(sourcenames, str):
476480
sourcenames = [sourcenames]
477-
params: MutableSequence[cwl.InputParameter] = []
481+
params: MutableSequence[cwl.InputParameter | cwl.CommandOutputParameter] = []
478482
for sourcename in sourcenames:
479483
if not isinstance(process, cwl.Workflow):
480484
for param in process.inputs:

cwl_utils/parser/cwl_v1_1_utils.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ def _compare_type(type1: Any, type2: Any) -> bool:
7575
case MutableSequence(), MutableSequence():
7676
if len(type1) != len(type2):
7777
return False
78-
for t1 in type1:
79-
if not any(_compare_type(t1, t2) for t2 in type2):
78+
for t3 in type1:
79+
if not any(_compare_type(t3, t2) for t2 in type2):
8080
return False
8181
return True
8282
return bool(type1 == type2)
@@ -489,13 +489,22 @@ def param_for_source_id(
489489
scatter_context: list[tuple[int, str] | None] | None = None,
490490
) -> (
491491
cwl.CommandInputParameter
492+
| cwl.CommandOutputParameter
492493
| cwl.WorkflowInputParameter
493-
| MutableSequence[cwl.CommandInputParameter | cwl.WorkflowInputParameter]
494+
| MutableSequence[
495+
cwl.CommandInputParameter
496+
| cwl.CommandOutputParameter
497+
| cwl.WorkflowInputParameter
498+
]
494499
):
495500
"""Find the process input parameter that matches one of the given sourcenames."""
496501
if isinstance(sourcenames, str):
497502
sourcenames = [sourcenames]
498-
params: MutableSequence[cwl.CommandInputParameter | cwl.WorkflowInputParameter] = []
503+
params: MutableSequence[
504+
cwl.CommandInputParameter
505+
| cwl.CommandOutputParameter
506+
| cwl.WorkflowInputParameter
507+
] = []
499508
for sourcename in sourcenames:
500509
if not isinstance(process, cwl.Workflow):
501510
for param in process.inputs:

cwl_utils/parser/cwl_v1_2_utils.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ def _compare_type(type1: Any, type2: Any) -> bool:
7575
case MutableSequence(), MutableSequence():
7676
if len(type1) != len(type2):
7777
return False
78-
for t1 in type1:
79-
if not any(_compare_type(t1, t2) for t2 in type2):
78+
for t3 in type1:
79+
if not any(_compare_type(t3, t2) for t2 in type2):
8080
return False
8181
return True
8282
return bool(type1 == type2)
@@ -578,13 +578,22 @@ def param_for_source_id(
578578
scatter_context: list[tuple[int, str] | None] | None = None,
579579
) -> (
580580
cwl.CommandInputParameter
581+
| cwl.CommandOutputParameter
581582
| cwl.WorkflowInputParameter
582-
| MutableSequence[cwl.CommandInputParameter | cwl.WorkflowInputParameter]
583+
| MutableSequence[
584+
cwl.CommandInputParameter
585+
| cwl.CommandOutputParameter
586+
| cwl.WorkflowInputParameter
587+
]
583588
):
584589
"""Find the process input parameter that matches one of the given sourcenames."""
585590
if isinstance(sourcenames, str):
586591
sourcenames = [sourcenames]
587-
params: MutableSequence[cwl.CommandInputParameter | cwl.WorkflowInputParameter] = []
592+
params: MutableSequence[
593+
cwl.CommandInputParameter
594+
| cwl.CommandOutputParameter
595+
| cwl.WorkflowInputParameter
596+
] = []
588597
for sourcename in sourcenames:
589598
if not isinstance(process, cwl.Workflow):
590599
for param in process.inputs:

cwl_utils/parser/utils.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,23 +427,31 @@ def param_for_source_id(
427427
scatter_context: list[tuple[int, str] | None] | None = None,
428428
) -> (
429429
(
430-
MutableSequence[cwl_utils.parser.cwl_v1_0.InputParameter]
430+
MutableSequence[
431+
cwl_utils.parser.cwl_v1_0.InputParameter
432+
| cwl_utils.parser.cwl_v1_0.CommandOutputParameter
433+
]
431434
| cwl_utils.parser.cwl_v1_0.InputParameter
435+
| cwl_utils.parser.cwl_v1_0.CommandOutputParameter
432436
)
433437
| (
434438
MutableSequence[
435439
cwl_utils.parser.cwl_v1_1.CommandInputParameter
440+
| cwl_utils.parser.cwl_v1_1.CommandOutputParameter
436441
| cwl_utils.parser.cwl_v1_1.WorkflowInputParameter
437442
]
438443
| cwl_utils.parser.cwl_v1_1.CommandInputParameter
444+
| cwl_utils.parser.cwl_v1_1.CommandOutputParameter
439445
| cwl_utils.parser.cwl_v1_1.WorkflowInputParameter
440446
)
441447
| (
442448
MutableSequence[
443449
cwl_utils.parser.cwl_v1_2.CommandInputParameter
450+
| cwl_utils.parser.cwl_v1_2.CommandOutputParameter
444451
| cwl_utils.parser.cwl_v1_2.WorkflowInputParameter
445452
]
446453
| cwl_utils.parser.cwl_v1_2.CommandInputParameter
454+
| cwl_utils.parser.cwl_v1_2.CommandOutputParameter
447455
| cwl_utils.parser.cwl_v1_2.WorkflowInputParameter
448456
)
449457
):

pyproject.toml

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,9 @@ dependencies = [
113113
"types-setuptools>=57.4.0",
114114
]
115115
require-runtime-dependencies = true
116-
exclude = [
117-
"/create_cwl_from_objects.py",
118-
"/load_cwl_by_path.py",
119-
"/cwl_utils/tests",
120-
"/cwl_utils/testdata",
121-
]
122116
include = [
123-
"/cwl_utils/parser",
117+
"/cwl_utils/parser",
118+
"/cwl_utils/expression.py"
124119
]
125120

126121
[tool.hatch.envs.test]

0 commit comments

Comments
 (0)