Skip to content

Commit 5abd8df

Browse files
GlassOfWhiskeymr-c
authored andcommitted
Modernize types
1 parent a84864d commit 5abd8df

10 files changed

+101
-98
lines changed

cwl_utils/cwl_v1_0_expression_refactor.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import uuid
88
from collections.abc import MutableSequence, Sequence
99
from contextlib import suppress
10-
from typing import Any, Optional, cast
10+
from typing import Any, cast
1111

1212
from ruamel import yaml
1313
from schema_salad.sourceline import SourceLine
@@ -445,7 +445,7 @@ def find_expressionLib(
445445
if process.requirements:
446446
for req in process.requirements:
447447
if isinstance(req, cwl.InlineJavascriptRequirement):
448-
return cast(Optional[list[str]], copy.deepcopy(req.expressionLib))
448+
return cast(list[str] | None, copy.deepcopy(req.expressionLib))
449449
return None
450450

451451

@@ -1815,9 +1815,9 @@ def traverse_step(
18151815
if not target:
18161816
raise WorkflowException("target not found")
18171817
input_source_id = None
1818-
source_type: None | (list[cwl.InputParameter] | cwl.InputParameter) = (
1819-
None
1820-
)
1818+
source_type: (
1819+
None | MutableSequence[cwl.InputParameter] | cwl.InputParameter
1820+
) = None
18211821
if inp.source:
18221822
if isinstance(inp.source, MutableSequence):
18231823
input_source_id = []
@@ -1915,7 +1915,7 @@ def workflow_step_to_InputParameters(
19151915
param = copy.deepcopy(
19161916
utils.param_for_source_id(parent, sourcenames=inp.source)
19171917
)
1918-
if isinstance(param, list):
1918+
if isinstance(param, MutableSequence):
19191919
for p in param:
19201920
if not p.type_:
19211921
raise WorkflowException(
@@ -1946,7 +1946,7 @@ def replace_step_valueFrom_expr_with_etool(
19461946
original_step_ins: list[cwl.WorkflowStepInput],
19471947
source: str | list[str] | None,
19481948
replace_etool: bool,
1949-
source_type: cwl.InputParameter | list[cwl.InputParameter] | None = None,
1949+
source_type: cwl.InputParameter | MutableSequence[cwl.InputParameter] | None = None,
19501950
) -> None:
19511951
"""Replace a WorkflowStep level 'valueFrom' expression with a sibling ExpressionTool step."""
19521952
if not step_inp.id:

cwl_utils/cwl_v1_1_expression_refactor.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import uuid
88
from collections.abc import MutableSequence, Sequence
99
from contextlib import suppress
10-
from typing import Any, Optional, cast
10+
from typing import Any, cast
1111

1212
from ruamel import yaml
1313
from schema_salad.sourceline import SourceLine
@@ -443,7 +443,7 @@ def find_expressionLib(
443443
if process.requirements:
444444
for req in process.requirements:
445445
if isinstance(req, cwl.InlineJavascriptRequirement):
446-
return cast(Optional[list[str]], copy.deepcopy(req.expressionLib))
446+
return cast(list[str] | None, copy.deepcopy(req.expressionLib))
447447
return None
448448

449449

@@ -1818,7 +1818,11 @@ def traverse_step(
18181818
raise WorkflowException("target not found")
18191819
input_source_id = None
18201820
source_type: None | (
1821-
list[cwl.WorkflowInputParameter] | cwl.WorkflowInputParameter
1821+
MutableSequence[
1822+
cwl.CommandInputParameter | cwl.WorkflowInputParameter
1823+
]
1824+
| cwl.CommandInputParameter
1825+
| cwl.WorkflowInputParameter
18221826
) = None
18231827
if inp.source:
18241828
if isinstance(inp.source, MutableSequence):
@@ -1906,7 +1910,7 @@ def traverse_step(
19061910

19071911
def workflow_step_to_WorkflowInputParameters(
19081912
step_ins: list[cwl.WorkflowStepInput], parent: cwl.Workflow, except_in_id: str
1909-
) -> list[cwl.WorkflowInputParameter]:
1913+
) -> MutableSequence[cwl.CommandInputParameter | cwl.WorkflowInputParameter]:
19101914
"""Create WorkflowInputParameters to match the given WorkflowStep inputs."""
19111915
params = []
19121916
for inp in step_ins:
@@ -1917,7 +1921,7 @@ def workflow_step_to_WorkflowInputParameters(
19171921
param = copy.deepcopy(
19181922
utils.param_for_source_id(parent, sourcenames=inp.source)
19191923
)
1920-
if isinstance(param, list):
1924+
if isinstance(param, MutableSequence):
19211925
for p in param:
19221926
p.id = inp_id
19231927
p.type_ = clean_type_ids(p.type_)
@@ -1941,7 +1945,9 @@ def replace_step_valueFrom_expr_with_etool(
19411945
source: str | list[str] | None,
19421946
replace_etool: bool,
19431947
source_type: None | (
1944-
cwl.WorkflowInputParameter | list[cwl.WorkflowInputParameter]
1948+
cwl.CommandInputParameter
1949+
| cwl.WorkflowInputParameter
1950+
| MutableSequence[cwl.CommandInputParameter | cwl.WorkflowInputParameter]
19451951
) = None,
19461952
) -> None:
19471953
"""Replace a WorkflowStep level 'valueFrom' expression with a sibling ExpressionTool step."""

cwl_utils/cwl_v1_2_expression_refactor.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import uuid
88
from collections.abc import Mapping, MutableSequence, Sequence
99
from contextlib import suppress
10-
from typing import Any, Optional, cast
10+
from typing import Any, cast
1111

1212
from ruamel import yaml
1313
from schema_salad.sourceline import SourceLine
@@ -442,7 +442,7 @@ def find_expressionLib(
442442
if process.requirements:
443443
for req in process.requirements:
444444
if isinstance(req, cwl.InlineJavascriptRequirement):
445-
return cast(Optional[list[str]], copy.deepcopy(req.expressionLib))
445+
return cast(list[str] | None, copy.deepcopy(req.expressionLib))
446446
return None
447447

448448

@@ -1921,7 +1921,11 @@ def traverse_step(
19211921
raise WorkflowException("target not found")
19221922
input_source_id = None
19231923
source_type: None | (
1924-
list[cwl.WorkflowInputParameter] | cwl.WorkflowInputParameter
1924+
MutableSequence[
1925+
cwl.CommandInputParameter | cwl.WorkflowInputParameter
1926+
]
1927+
| cwl.CommandInputParameter
1928+
| cwl.WorkflowInputParameter
19251929
) = None
19261930
if inp.source:
19271931
if isinstance(inp.source, MutableSequence):
@@ -2017,7 +2021,7 @@ def traverse_step(
20172021

20182022
def workflow_step_to_WorkflowInputParameters(
20192023
step_ins: list[cwl.WorkflowStepInput], parent: cwl.Workflow, except_in_id: str
2020-
) -> list[cwl.WorkflowInputParameter]:
2024+
) -> MutableSequence[cwl.CommandInputParameter | cwl.WorkflowInputParameter]:
20212025
"""Create WorkflowInputParameters to match the given WorkflowStep inputs."""
20222026
params = []
20232027
for inp in step_ins:
@@ -2028,7 +2032,7 @@ def workflow_step_to_WorkflowInputParameters(
20282032
param = copy.deepcopy(
20292033
utils.param_for_source_id(parent, sourcenames=inp.source)
20302034
)
2031-
if isinstance(param, list):
2035+
if isinstance(param, MutableSequence):
20322036
for p in param:
20332037
p.id = inp_id
20342038
p.type_ = clean_type_ids(p.type_)
@@ -2052,7 +2056,9 @@ def replace_step_valueFrom_expr_with_etool(
20522056
source: str | list[str] | None,
20532057
replace_etool: bool,
20542058
source_type: None | (
2055-
cwl.WorkflowInputParameter | list[cwl.WorkflowInputParameter]
2059+
cwl.CommandInputParameter
2060+
| cwl.WorkflowInputParameter
2061+
| MutableSequence[cwl.CommandInputParameter | cwl.WorkflowInputParameter]
20562062
) = None,
20572063
) -> None:
20582064
"""Replace a WorkflowStep level 'valueFrom' expression with a sibling ExpressionTool step."""

cwl_utils/expression.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
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, cast
1010

1111
from schema_salad.utils import json_dumps
1212

@@ -293,7 +293,7 @@ def do_eval(
293293
294294
:param timeout: The maximum number of seconds to wait while executing.
295295
"""
296-
runtime = cast(MutableMapping[str, Union[int, str, None]], copy.deepcopy(resources))
296+
runtime = cast(MutableMapping[str, int | str | None], copy.deepcopy(resources))
297297
runtime["tmpdir"] = tmpdir or None
298298
runtime["outdir"] = outdir or None
299299

cwl_utils/expression_refactor.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import sys
99
from collections.abc import Callable, MutableMapping, MutableSequence
1010
from pathlib import Path
11-
from typing import Any, Optional, Protocol, Union
11+
from typing import Any, Protocol
1212

1313
from ruamel.yaml.main import YAML
1414
from ruamel.yaml.scalarstring import walk_tree
@@ -28,9 +28,9 @@
2828
_logger.setLevel(logging.INFO)
2929
_cwlutilslogger.setLevel(100)
3030

31-
save_type = Optional[
32-
Union[MutableMapping[str, Any], MutableSequence[Any], int, float, bool, str]
33-
]
31+
save_type = (
32+
MutableMapping[str, Any] | MutableSequence[Any] | int | float | bool | str | None
33+
)
3434

3535

3636
class saveCWL(Protocol):

cwl_utils/inputs_schema_gen.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from copy import deepcopy
1313
from importlib.resources import files
1414
from pathlib import Path
15-
from typing import Any, TypeGuard, Union
15+
from typing import Any, TypeGuard
1616
from urllib.parse import urlparse
1717

1818
import requests
@@ -61,9 +61,9 @@
6161
}
6262

6363
# Some type hinting
64-
InputType = Union[
65-
InputArraySchema, InputEnumSchema, InputRecordSchema, str, File, Directory
66-
]
64+
InputType = (
65+
InputArraySchema | InputEnumSchema | InputRecordSchema | str | File | Directory
66+
)
6767

6868

6969
# Don't need type checking at runtime

cwl_utils/parser/cwl_v1_0_utils.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from collections.abc import MutableMapping, MutableSequence
66
from io import StringIO
77
from pathlib import Path
8-
from typing import IO, Any, Union, cast
8+
from typing import Any, IO, cast
99
from urllib.parse import urldefrag
1010

1111
from schema_salad.exceptions import ValidationException
@@ -48,12 +48,8 @@ def _compare_records(
4848
_logger.info(
4949
"Record comparison failure for %s and %s\n"
5050
"Did not match fields for %s: %s and %s",
51-
cast(
52-
Union[cwl.InputRecordSchema, cwl.CommandOutputRecordSchema], src
53-
).name,
54-
cast(
55-
Union[cwl.InputRecordSchema, cwl.CommandOutputRecordSchema], sink
56-
).name,
51+
cast(cwl.InputRecordSchema | cwl.CommandOutputRecordSchema, src).name,
52+
cast(cwl.InputRecordSchema | cwl.CommandOutputRecordSchema, sink).name,
5753
key,
5854
srcfields.get(key),
5955
sinkfields.get(key),
@@ -427,7 +423,7 @@ def type_for_source(
427423
"""Determine the type for the given sourcenames."""
428424
scatter_context: list[tuple[int, str] | None] = []
429425
params = param_for_source_id(process, sourcenames, parent, scatter_context)
430-
if not isinstance(params, list):
426+
if not isinstance(params, MutableSequence):
431427
new_type = params.type_
432428
if scatter_context[0] is not None:
433429
if scatter_context[0][1] == "nested_crossproduct":
@@ -474,11 +470,11 @@ def param_for_source_id(
474470
sourcenames: str | list[str],
475471
parent: cwl.Workflow | None = None,
476472
scatter_context: list[tuple[int, str] | None] | None = None,
477-
) -> list[cwl.InputParameter] | cwl.InputParameter:
473+
) -> MutableSequence[cwl.InputParameter] | cwl.InputParameter:
478474
"""Find the process input parameter that matches one of the given sourcenames."""
479475
if isinstance(sourcenames, str):
480476
sourcenames = [sourcenames]
481-
params: list[cwl.InputParameter] = []
477+
params: MutableSequence[cwl.InputParameter] = []
482478
for sourcename in sourcenames:
483479
if not isinstance(process, cwl.Workflow):
484480
for param in process.inputs:

cwl_utils/parser/cwl_v1_1_utils.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from collections.abc import MutableMapping, MutableSequence
66
from io import StringIO
77
from pathlib import Path
8-
from typing import IO, Any, Union, cast
8+
from typing import Any, IO, cast
99
from urllib.parse import urldefrag
1010

1111
from schema_salad.exceptions import ValidationException
@@ -48,12 +48,8 @@ def _compare_records(
4848
_logger.info(
4949
"Record comparison failure for %s and %s\n"
5050
"Did not match fields for %s: %s and %s",
51-
cast(
52-
Union[cwl.InputRecordSchema, cwl.CommandOutputRecordSchema], src
53-
).name,
54-
cast(
55-
Union[cwl.InputRecordSchema, cwl.CommandOutputRecordSchema], sink
56-
).name,
51+
cast(cwl.InputRecordSchema | cwl.CommandOutputRecordSchema, src).name,
52+
cast(cwl.InputRecordSchema | cwl.CommandOutputRecordSchema, sink).name,
5753
key,
5854
srcfields.get(key),
5955
sinkfields.get(key),
@@ -443,7 +439,7 @@ def type_for_source(
443439
"""Determine the type for the given sourcenames."""
444440
scatter_context: list[tuple[int, str] | None] = []
445441
params = param_for_source_id(process, sourcenames, parent, scatter_context)
446-
if not isinstance(params, list):
442+
if not isinstance(params, MutableSequence):
447443
new_type = params.type_
448444
if scatter_context[0] is not None:
449445
if scatter_context[0][1] == "nested_crossproduct":
@@ -491,11 +487,15 @@ def param_for_source_id(
491487
sourcenames: str | list[str],
492488
parent: cwl.Workflow | None = None,
493489
scatter_context: list[tuple[int, str] | None] | None = None,
494-
) -> list[cwl.WorkflowInputParameter] | cwl.WorkflowInputParameter:
490+
) -> (
491+
cwl.CommandInputParameter
492+
| cwl.WorkflowInputParameter
493+
| MutableSequence[cwl.CommandInputParameter | cwl.WorkflowInputParameter]
494+
):
495495
"""Find the process input parameter that matches one of the given sourcenames."""
496496
if isinstance(sourcenames, str):
497497
sourcenames = [sourcenames]
498-
params: list[cwl.WorkflowInputParameter] = []
498+
params: MutableSequence[cwl.CommandInputParameter | cwl.WorkflowInputParameter] = []
499499
for sourcename in sourcenames:
500500
if not isinstance(process, cwl.Workflow):
501501
for param in process.inputs:

cwl_utils/parser/cwl_v1_2_utils.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from collections.abc import MutableMapping, MutableSequence
66
from io import StringIO
77
from pathlib import Path
8-
from typing import IO, Any, Union, cast
8+
from typing import Any, IO, cast
99
from urllib.parse import urldefrag
1010

1111
from schema_salad.exceptions import ValidationException
@@ -48,12 +48,8 @@ def _compare_records(
4848
_logger.info(
4949
"Record comparison failure for %s and %s\n"
5050
"Did not match fields for %s: %s and %s",
51-
cast(
52-
Union[cwl.InputRecordSchema, cwl.CommandOutputRecordSchema], src
53-
).name,
54-
cast(
55-
Union[cwl.InputRecordSchema, cwl.CommandOutputRecordSchema], sink
56-
).name,
51+
cast(cwl.InputRecordSchema | cwl.CommandOutputRecordSchema, src).name,
52+
cast(cwl.InputRecordSchema | cwl.CommandOutputRecordSchema, sink).name,
5753
key,
5854
srcfields.get(key),
5955
sinkfields.get(key),
@@ -225,7 +221,7 @@ def check_all_types(
225221
continue
226222
if sourceField is not None:
227223
if isinstance(sourceField, MutableSequence):
228-
linkMerge = sink.linkMerge or (
224+
linkMerge: str | None = sink.linkMerge or (
229225
"merge_nested" if len(sourceField) > 1 else None
230226
)
231227
if sink.pickValue in ("first_non_null", "the_only_non_null"):
@@ -525,7 +521,7 @@ def type_for_source(
525521
"""Determine the type for the given sourcenames."""
526522
scatter_context: list[tuple[int, str] | None] = []
527523
params = param_for_source_id(process, sourcenames, parent, scatter_context)
528-
if not isinstance(params, list):
524+
if not isinstance(params, MutableSequence):
529525
new_type = params.type_
530526
if scatter_context[0] is not None:
531527
if scatter_context[0][1] == "nested_crossproduct":
@@ -580,11 +576,15 @@ def param_for_source_id(
580576
sourcenames: str | list[str],
581577
parent: cwl.Workflow | None = None,
582578
scatter_context: list[tuple[int, str] | None] | None = None,
583-
) -> list[cwl.WorkflowInputParameter] | cwl.WorkflowInputParameter:
579+
) -> (
580+
cwl.CommandInputParameter
581+
| cwl.WorkflowInputParameter
582+
| MutableSequence[cwl.CommandInputParameter | cwl.WorkflowInputParameter]
583+
):
584584
"""Find the process input parameter that matches one of the given sourcenames."""
585585
if isinstance(sourcenames, str):
586586
sourcenames = [sourcenames]
587-
params: list[cwl.WorkflowInputParameter] = []
587+
params: MutableSequence[cwl.CommandInputParameter | cwl.WorkflowInputParameter] = []
588588
for sourcename in sourcenames:
589589
if not isinstance(process, cwl.Workflow):
590590
for param in process.inputs:

0 commit comments

Comments
 (0)