Skip to content

Commit a977372

Browse files
committed
mypy 1.16 type simplifications
1 parent bdcc818 commit a977372

File tree

10 files changed

+34
-32
lines changed

10 files changed

+34
-32
lines changed

cwltool/checker.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def check_types(
5454
def merge_flatten_type(src: SinkType) -> CWLOutputType:
5555
"""Return the merge flattened type of the source type."""
5656
if isinstance(src, MutableSequence):
57-
return [merge_flatten_type(cast(SinkType, t)) for t in src]
57+
return [merge_flatten_type(t) for t in src]
5858
if isinstance(src, MutableMapping) and src.get("type") == "array":
5959
return src
6060
return {"items": src, "type": "array"}
@@ -94,22 +94,20 @@ def can_assign_src_to_sink(src: SinkType, sink: Optional[SinkType], strict: bool
9494
if strict:
9595
return False
9696
return True
97-
return can_assign_src_to_sink(
98-
cast(SinkType, src["type"]), cast(Optional[SinkType], sink["type"]), strict
99-
)
97+
return can_assign_src_to_sink(src["type"], sink["type"], strict)
10098
if isinstance(src, MutableSequence):
10199
if strict:
102100
for this_src in src:
103-
if not can_assign_src_to_sink(cast(SinkType, this_src), sink):
101+
if not can_assign_src_to_sink(this_src, sink):
104102
return False
105103
return True
106104
for this_src in src:
107-
if this_src != "null" and can_assign_src_to_sink(cast(SinkType, this_src), sink):
105+
if this_src != "null" and can_assign_src_to_sink(this_src, sink):
108106
return True
109107
return False
110108
if isinstance(sink, MutableSequence):
111109
for this_sink in sink:
112-
if can_assign_src_to_sink(src, cast(SinkType, this_sink)):
110+
if can_assign_src_to_sink(src, this_sink):
113111
return True
114112
return False
115113
return bool(src == sink)
@@ -257,7 +255,7 @@ def static_checker(
257255
)
258256
+ "\n"
259257
+ SourceLine(sink, "type").makeError(
260-
" with sink '%s' of type %s" % (sink_name, json_dumps(sink["type"]))
258+
" with sink '{}' of type {}".format(sink_name, json_dumps(sink["type"]))
261259
)
262260
)
263261
if linkMerge is not None:

cwltool/command_line_tool.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,7 @@ def _initialworkdir(self, j: Optional[JobBase], builder: Builder) -> None:
501501
if not isinstance(ls_evaluated, MutableSequence):
502502
fail = ls_evaluated
503503
else:
504-
ls_evaluated2 = cast(MutableSequence[Union[None, CWLOutputType]], ls_evaluated)
504+
ls_evaluated2 = ls_evaluated
505505
for entry in ls_evaluated2:
506506
if entry == None: # noqa
507507
if classic_dirent:
@@ -1389,7 +1389,7 @@ def collect_output(
13891389
"Multiple matches for output item that is a single file."
13901390
)
13911391
else:
1392-
result = cast(CWLOutputType, result[0])
1392+
result = result[0]
13931393

13941394
if "secondaryFiles" in schema:
13951395
with SourceLine(schema, "secondaryFiles", WorkflowException, debug):

cwltool/cwlprov/ro.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -674,12 +674,12 @@ def _relativise_files(
674674

675675
for val in structure.values():
676676
try:
677-
self._relativise_files(cast(CWLOutputType, val))
677+
self._relativise_files(val)
678678
except OSError:
679679
pass
680680
return
681681

682682
if isinstance(structure, MutableSequence):
683683
for obj in structure:
684684
# Recurse and rewrite any nested File objects
685-
self._relativise_files(cast(CWLOutputType, obj))
685+
self._relativise_files(obj)

cwltool/main.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def realize_input_schema(
304304
if isinstance(entry["type"], Mapping):
305305
entry["type"] = cast(
306306
CWLOutputType,
307-
realize_input_schema([cast(CWLObjectType, entry["type"])], schema_defs),
307+
realize_input_schema([entry["type"]], schema_defs),
308308
)
309309
if entry["type"] == "array":
310310
items = entry["items"] if not isinstance(entry["items"], str) else [entry["items"]]
@@ -373,7 +373,10 @@ def load_job_order(
373373
content_types=CWL_CONTENT_TYPES,
374374
)
375375

376-
if job_order_object is not None and "http://commonwl.org/cwltool#overrides" in job_order_object:
376+
if (
377+
isinstance(job_order_object, CommentedMap)
378+
and "http://commonwl.org/cwltool#overrides" in job_order_object
379+
):
377380
ov_uri = file_uri(job_order_file or input_basedir)
378381
overrides_list.extend(resolve_overrides(job_order_object, ov_uri, tool_file_uri))
379382
del job_order_object["http://commonwl.org/cwltool#overrides"]

cwltool/pack.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def find_run(
3232
runs.add(d["run"])
3333
find_run(loadref(None, d["run"]), loadref, runs)
3434
for s in d.values():
35-
find_run(s, loadref, runs)
35+
find_run(cast(Union[CWLObjectType, ResolveType], s), loadref, runs)
3636

3737

3838
def find_ids(
@@ -47,7 +47,7 @@ def find_ids(
4747
if i in d and isinstance(d[i], str):
4848
ids.add(cast(str, d[i]))
4949
for s2 in d.values():
50-
find_ids(cast(CWLOutputType, s2), ids)
50+
find_ids(s2, ids)
5151

5252

5353
def replace_refs(d: Any, rewrite: dict[str, str], stem: str, newstem: str) -> None:
@@ -84,7 +84,7 @@ def import_embed(
8484
) -> None:
8585
if isinstance(d, MutableSequence):
8686
for v in d:
87-
import_embed(cast(CWLOutputType, v), seen)
87+
import_embed(v, seen)
8888
elif isinstance(d, MutableMapping):
8989
for n in ("id", "name"):
9090
if n in d:
@@ -100,7 +100,7 @@ def import_embed(
100100
break
101101

102102
for k in sorted(d.keys()):
103-
import_embed(cast(CWLOutputType, d[k]), seen)
103+
import_embed(d[k], seen)
104104

105105

106106
def pack(

cwltool/process.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ def avroize_type(
444444
cast(MutableSequence[CWLOutputType], field_type["items"]), name_prefix
445445
)
446446
else:
447-
field_type["type"] = avroize_type(cast(CWLOutputType, field_type["type"]), name_prefix)
447+
field_type["type"] = avroize_type(field_type["type"], name_prefix)
448448
elif field_type == "File":
449449
return "org.w3id.cwl.cwl.File"
450450
elif field_type == "Directory":
@@ -490,10 +490,10 @@ def var_spool_cwl_detector(
490490
r = True
491491
elif isinstance(obj, MutableMapping):
492492
for mkey, mvalue in obj.items():
493-
r = var_spool_cwl_detector(cast(CWLOutputType, mvalue), obj, mkey) or r
493+
r = var_spool_cwl_detector(mvalue, obj, mkey) or r
494494
elif isinstance(obj, MutableSequence):
495495
for lkey, lvalue in enumerate(obj):
496-
r = var_spool_cwl_detector(cast(CWLOutputType, lvalue), obj, lkey) or r
496+
r = var_spool_cwl_detector(lvalue, obj, lkey) or r
497497
return r
498498

499499

cwltool/secrets.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import uuid
44
from collections.abc import MutableMapping, MutableSequence
5-
from typing import Optional, cast
5+
from typing import Optional
66

77
from .utils import CWLObjectType, CWLOutputType
88

@@ -43,11 +43,11 @@ def has_secret(self, value: CWLOutputType) -> bool:
4343
return True
4444
elif isinstance(value, MutableMapping):
4545
for this_value in value.values():
46-
if self.has_secret(cast(CWLOutputType, this_value)):
46+
if self.has_secret(this_value):
4747
return True
4848
elif isinstance(value, MutableSequence):
4949
for this_value in value:
50-
if self.has_secret(cast(CWLOutputType, this_value)):
50+
if self.has_secret(this_value):
5151
return True
5252
return False
5353

@@ -58,7 +58,7 @@ def retrieve(self, value: CWLOutputType) -> CWLOutputType:
5858
value = value.replace(key, this_value)
5959
return value
6060
elif isinstance(value, MutableMapping):
61-
return {k: self.retrieve(cast(CWLOutputType, v)) for k, v in value.items()}
61+
return {k: self.retrieve(v) for k, v in value.items()}
6262
elif isinstance(value, MutableSequence):
63-
return [self.retrieve(cast(CWLOutputType, v)) for v in value]
63+
return [self.retrieve(v) for v in value]
6464
return value

cwltool/update.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def update_secondaryFiles(
132132
new_seq[index] = update_secondaryFiles(entry)
133133
return new_seq
134134
elif isinstance(t, MutableSequence):
135-
return CommentedSeq([update_secondaryFiles(cast(CWLOutputType, p)) for p in t])
135+
return CommentedSeq([update_secondaryFiles(p) for p in t])
136136
elif isinstance(t, MutableMapping):
137137
return cast(MutableMapping[str, str], t)
138138
elif top:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def _find_package_data(base: str, globs: list[str], root: str = "cwltool") -> li
8989
mypyc_targets = [x for x in all_real_pys if x not in mypyc_skiplist]
9090

9191
# Strip out any test code
92-
mypyc_targets = [x for x in mypyc_targets if not x.startswith(("tests" + os.sep))]
92+
mypyc_targets = [x for x in mypyc_targets if not x.startswith("tests" + os.sep)]
9393

9494
mypyc_targets.sort()
9595

tests/test_context.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
import logging
12
import subprocess
23
import sys
3-
import logging
44
from io import StringIO
5-
from typing import MutableMapping, cast
5+
from pathlib import Path
6+
from typing import cast
7+
from collections.abc import MutableMapping
68

79
from cwltool.context import RuntimeContext
810
from cwltool.factory import Factory
911
from cwltool.utils import CWLObjectType
1012
from cwltool.workflow_job import WorkflowJobStep
11-
from pathlib import Path
1213

1314
from .util import get_data, needs_docker
1415

@@ -49,7 +50,7 @@ def test_workflow_job_step_name_callback() -> None:
4950
def step_name_hook(step: WorkflowJobStep, job: CWLObjectType) -> str:
5051
j1 = cast(MutableMapping[str, CWLObjectType], job)
5152
inp = cast(MutableMapping[str, str], j1.get("revtool_input", j1.get("sorted_input")))
52-
return "%s on %s" % (
53+
return "{} on {}".format(
5354
step.name,
5455
inp.get("basename"),
5556
)

0 commit comments

Comments
 (0)