Skip to content

Commit c4e10e3

Browse files
GlassOfWhiskeymr-c
authored andcommitted
Better typing
1 parent 09e2676 commit c4e10e3

File tree

9 files changed

+108
-51
lines changed

9 files changed

+108
-51
lines changed

cwl_utils/cwl_v1_0_expression_refactor.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,7 +1695,9 @@ def cltool_step_outputs_to_workflow_outputs(
16951695
def generate_etool_from_expr2(
16961696
expr: str,
16971697
target: cwl.InputParameter,
1698-
inputs: Sequence[cwl.InputParameter | cwl.CommandInputParameter],
1698+
inputs: Sequence[
1699+
cwl.InputParameter | cwl.CommandInputParameter | cwl.CommandOutputParameter
1700+
],
16991701
self_name: str | None = None,
17001702
process: cwl.CommandLineTool | cwl.ExpressionTool | None = None,
17011703
extra_processes: None | (
@@ -1816,7 +1818,10 @@ def traverse_step(
18161818
raise WorkflowException("target not found")
18171819
input_source_id = None
18181820
source_type: (
1819-
None | MutableSequence[cwl.InputParameter] | cwl.InputParameter
1821+
None
1822+
| MutableSequence[cwl.InputParameter | cwl.CommandOutputParameter]
1823+
| cwl.InputParameter
1824+
| cwl.CommandOutputParameter
18201825
) = None
18211826
if inp.source:
18221827
if isinstance(inp.source, MutableSequence):
@@ -1904,7 +1909,7 @@ def traverse_step(
19041909

19051910
def workflow_step_to_InputParameters(
19061911
step_ins: list[cwl.WorkflowStepInput], parent: cwl.Workflow, except_in_id: str
1907-
) -> list[cwl.InputParameter]:
1912+
) -> list[cwl.InputParameter | cwl.CommandOutputParameter]:
19081913
"""Create InputParameters to match the given WorkflowStep inputs."""
19091914
params = []
19101915
for inp in step_ins:
@@ -1946,7 +1951,12 @@ def replace_step_valueFrom_expr_with_etool(
19461951
original_step_ins: list[cwl.WorkflowStepInput],
19471952
source: str | list[str] | None,
19481953
replace_etool: bool,
1949-
source_type: cwl.InputParameter | MutableSequence[cwl.InputParameter] | None = None,
1954+
source_type: (
1955+
cwl.InputParameter
1956+
| cwl.CommandOutputParameter
1957+
| MutableSequence[cwl.InputParameter | cwl.CommandOutputParameter]
1958+
| None
1959+
) = None,
19501960
) -> None:
19511961
"""Replace a WorkflowStep level 'valueFrom' expression with a sibling ExpressionTool step."""
19521962
if not step_inp.id:

cwl_utils/cwl_v1_1_expression_refactor.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,7 +1697,11 @@ def cltool_step_outputs_to_workflow_outputs(
16971697
def generate_etool_from_expr2(
16981698
expr: str,
16991699
target: cwl.CommandInputParameter | cwl.WorkflowInputParameter,
1700-
inputs: Sequence[cwl.WorkflowInputParameter | cwl.CommandInputParameter],
1700+
inputs: Sequence[
1701+
cwl.WorkflowInputParameter
1702+
| cwl.CommandInputParameter
1703+
| cwl.CommandOutputParameter
1704+
],
17011705
self_name: str | None = None,
17021706
process: cwl.CommandLineTool | cwl.ExpressionTool | None = None,
17031707
extra_processes: None | (
@@ -1819,9 +1823,12 @@ def traverse_step(
18191823
input_source_id = None
18201824
source_type: None | (
18211825
MutableSequence[
1822-
cwl.CommandInputParameter | cwl.WorkflowInputParameter
1826+
cwl.CommandInputParameter
1827+
| cwl.CommandOutputParameter
1828+
| cwl.WorkflowInputParameter
18231829
]
18241830
| cwl.CommandInputParameter
1831+
| cwl.CommandOutputParameter
18251832
| cwl.WorkflowInputParameter
18261833
) = None
18271834
if inp.source:
@@ -1910,7 +1917,9 @@ def traverse_step(
19101917

19111918
def workflow_step_to_WorkflowInputParameters(
19121919
step_ins: list[cwl.WorkflowStepInput], parent: cwl.Workflow, except_in_id: str
1913-
) -> MutableSequence[cwl.CommandInputParameter | cwl.WorkflowInputParameter]:
1920+
) -> MutableSequence[
1921+
cwl.CommandInputParameter | cwl.CommandOutputParameter | cwl.WorkflowInputParameter
1922+
]:
19141923
"""Create WorkflowInputParameters to match the given WorkflowStep inputs."""
19151924
params = []
19161925
for inp in step_ins:
@@ -1946,8 +1955,13 @@ def replace_step_valueFrom_expr_with_etool(
19461955
replace_etool: bool,
19471956
source_type: None | (
19481957
cwl.CommandInputParameter
1958+
| cwl.CommandOutputParameter
19491959
| cwl.WorkflowInputParameter
1950-
| MutableSequence[cwl.CommandInputParameter | cwl.WorkflowInputParameter]
1960+
| MutableSequence[
1961+
cwl.CommandInputParameter
1962+
| cwl.CommandOutputParameter
1963+
| cwl.WorkflowInputParameter
1964+
]
19511965
) = None,
19521966
) -> None:
19531967
"""Replace a WorkflowStep level 'valueFrom' expression with a sibling ExpressionTool step."""

cwl_utils/cwl_v1_2_expression_refactor.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1800,7 +1800,11 @@ def cltool_step_outputs_to_workflow_outputs(
18001800
def generate_etool_from_expr2(
18011801
expr: str,
18021802
target: cwl.CommandInputParameter | cwl.WorkflowInputParameter,
1803-
inputs: Sequence[cwl.WorkflowInputParameter | cwl.CommandInputParameter],
1803+
inputs: Sequence[
1804+
cwl.WorkflowInputParameter
1805+
| cwl.CommandInputParameter
1806+
| cwl.CommandOutputParameter
1807+
],
18041808
self_name: str | None = None,
18051809
process: cwl.CommandLineTool | cwl.ExpressionTool | None = None,
18061810
extra_processes: None | (
@@ -1922,9 +1926,12 @@ def traverse_step(
19221926
input_source_id = None
19231927
source_type: None | (
19241928
MutableSequence[
1925-
cwl.CommandInputParameter | cwl.WorkflowInputParameter
1929+
cwl.CommandInputParameter
1930+
| cwl.CommandOutputParameter
1931+
| cwl.WorkflowInputParameter
19261932
]
19271933
| cwl.CommandInputParameter
1934+
| cwl.CommandOutputParameter
19281935
| cwl.WorkflowInputParameter
19291936
) = None
19301937
if inp.source:
@@ -2021,7 +2028,9 @@ def traverse_step(
20212028

20222029
def workflow_step_to_WorkflowInputParameters(
20232030
step_ins: list[cwl.WorkflowStepInput], parent: cwl.Workflow, except_in_id: str
2024-
) -> MutableSequence[cwl.CommandInputParameter | cwl.WorkflowInputParameter]:
2031+
) -> MutableSequence[
2032+
cwl.CommandInputParameter | cwl.CommandOutputParameter | cwl.WorkflowInputParameter
2033+
]:
20252034
"""Create WorkflowInputParameters to match the given WorkflowStep inputs."""
20262035
params = []
20272036
for inp in step_ins:
@@ -2057,8 +2066,13 @@ def replace_step_valueFrom_expr_with_etool(
20572066
replace_etool: bool,
20582067
source_type: None | (
20592068
cwl.CommandInputParameter
2069+
| cwl.CommandOutputParameter
20602070
| cwl.WorkflowInputParameter
2061-
| MutableSequence[cwl.CommandInputParameter | cwl.WorkflowInputParameter]
2071+
| MutableSequence[
2072+
cwl.CommandInputParameter
2073+
| cwl.CommandOutputParameter
2074+
| cwl.WorkflowInputParameter
2075+
]
20622076
) = None,
20632077
) -> None:
20642078
"""Replace a WorkflowStep level 'valueFrom' expression with a sibling ExpressionTool step."""

cwl_utils/expression.py

Lines changed: 13 additions & 19 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

@@ -21,6 +20,15 @@
2120
)
2221
from cwl_utils.utils import bytes2str_in_dicts
2322

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

2533
def _convert_dumper(string: str) -> str:
2634
return f"{json.dumps(string)} + "
@@ -293,9 +301,7 @@ def do_eval(
293301
294302
:param timeout: The maximum number of seconds to wait while executing.
295303
"""
296-
runtime = cast(MutableMapping[str, int | str | None], copy.deepcopy(resources))
297-
runtime["tmpdir"] = tmpdir or None
298-
runtime["outdir"] = outdir or None
304+
runtime = resources | {"tmpdir": tmpdir or None, "outdir": outdir or None}
299305

300306
rootvars = cast(
301307
CWLParameterContext,
@@ -319,19 +325,7 @@ def do_eval(
319325
fullJS=fullJS,
320326
jslib=jslib,
321327
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-
),
328+
escaping_behavior=1 if cwlVersion in OLD_ESCAPE_CWL_VERSIONS else 2,
335329
**kwargs,
336330
)
337331

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)