Skip to content

Commit 998b1e5

Browse files
committed
freshen cwltool/update.py
1 parent a574f47 commit 998b1e5

File tree

1 file changed

+55
-45
lines changed

1 file changed

+55
-45
lines changed

cwltool/update.py

Lines changed: 55 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import copy
22
from functools import partial
33
from typing import (
4-
Any,
54
Callable,
65
Dict,
76
List,
@@ -10,6 +9,7 @@
109
Optional,
1110
Tuple,
1211
Union,
12+
cast,
1313
)
1414

1515
from ruamel.yaml.comments import CommentedMap, CommentedSeq
@@ -18,18 +18,19 @@
1818
from schema_salad.sourceline import SourceLine
1919

2020
from .loghandler import _logger
21-
from .utils import aslist, visit_class, visit_field
21+
from .utils import CWLObjectType, CWLOutputType, aslist, visit_class, visit_field
2222

2323

24-
def v1_1to1_2(doc, loader, baseuri): # pylint: disable=unused-argument
25-
# type: (Any, Loader, str) -> Tuple[Any, str]
24+
def v1_1to1_2(
25+
doc: CommentedMap, loader: Loader, baseuri: str
26+
) -> Tuple[CommentedMap, str]: # pylint: disable=unused-argument
2627
"""Public updater for v1.1 to v1.2"""
2728

2829
doc = copy.deepcopy(doc)
2930

3031
upd = doc
3132
if isinstance(upd, MutableMapping) and "$graph" in upd:
32-
upd = upd["$graph"]
33+
upd = cast(CommentedMap, upd["$graph"])
3334
for proc in aslist(upd):
3435
if "cwlVersion" in proc:
3536
del proc["cwlVersion"]
@@ -38,8 +39,8 @@ def v1_1to1_2(doc, loader, baseuri): # pylint: disable=unused-argument
3839

3940

4041
def v1_0to1_1(
41-
doc: Any, loader: Loader, baseuri: str
42-
) -> Tuple[Any, str]: # pylint: disable=unused-argument
42+
doc: CommentedMap, loader: Loader, baseuri: str
43+
) -> Tuple[CommentedMap, str]: # pylint: disable=unused-argument
4344
"""Public updater for v1.0 to v1.1."""
4445
doc = copy.deepcopy(doc)
4546

@@ -52,29 +53,31 @@ def v1_0to1_1(
5253
"http://commonwl.org/cwltool#LoadListingRequirement": "LoadListingRequirement",
5354
}
5455

55-
def rewrite_requirements(t: MutableMapping[str, Any]) -> None:
56+
def rewrite_requirements(t: CWLObjectType) -> None:
5657
if "requirements" in t:
57-
for r in t["requirements"]:
58+
for r in cast(MutableSequence[CWLObjectType], t["requirements"]):
5859
if isinstance(r, MutableMapping):
59-
if r["class"] in rewrite:
60-
r["class"] = rewrite[r["class"]]
60+
cls = cast(str, r["class"])
61+
if cls in rewrite:
62+
r["class"] = rewrite[cls]
6163
else:
6264
raise ValidationException(
6365
"requirements entries must be dictionaries: {} {}.".format(
6466
type(r), r
6567
)
6668
)
6769
if "hints" in t:
68-
for r in t["hints"]:
70+
for r in cast(MutableSequence[CWLObjectType], t["hints"]):
6971
if isinstance(r, MutableMapping):
70-
if r["class"] in rewrite:
71-
r["class"] = rewrite[r["class"]]
72+
cls = cast(str, r["class"])
73+
if cls in rewrite:
74+
r["class"] = rewrite[cls]
7275
else:
7376
raise ValidationException(
7477
"hints entries must be dictionaries: {} {}.".format(type(r), r)
7578
)
7679
if "steps" in t:
77-
for s in t["steps"]:
80+
for s in cast(MutableSequence[CWLObjectType], t["steps"]):
7881
if isinstance(s, MutableMapping):
7982
rewrite_requirements(s)
8083
else:
@@ -83,25 +86,27 @@ def rewrite_requirements(t: MutableMapping[str, Any]) -> None:
8386
)
8487

8588
def update_secondaryFiles(t, top=False):
86-
# type: (Any, bool) -> Union[MutableSequence[MutableMapping[str, str]], MutableMapping[str, str]]
89+
# type: (CWLOutputType, bool) -> Union[MutableSequence[MutableMapping[str, str]], MutableMapping[str, str]]
8790
if isinstance(t, CommentedSeq):
8891
new_seq = copy.deepcopy(t)
8992
for index, entry in enumerate(t):
9093
new_seq[index] = update_secondaryFiles(entry)
9194
return new_seq
9295
elif isinstance(t, MutableSequence):
93-
return CommentedSeq([update_secondaryFiles(p) for p in t])
96+
return CommentedSeq(
97+
[update_secondaryFiles(cast(CWLOutputType, p)) for p in t]
98+
)
9499
elif isinstance(t, MutableMapping):
95-
return t
100+
return cast(MutableMapping[str, str], t)
96101
elif top:
97102
return CommentedSeq([CommentedMap([("pattern", t)])])
98103
else:
99104
return CommentedMap([("pattern", t)])
100105

101-
def fix_inputBinding(t): # type: (Dict[str, Any]) -> None
102-
for i in t["inputs"]:
106+
def fix_inputBinding(t: CWLObjectType) -> None:
107+
for i in cast(MutableSequence[CWLObjectType], t["inputs"]):
103108
if "inputBinding" in i:
104-
ib = i["inputBinding"]
109+
ib = cast(CWLObjectType, i["inputBinding"])
105110
for k in list(ib.keys()):
106111
if k != "loadContents":
107112
_logger.warning(
@@ -118,7 +123,7 @@ def fix_inputBinding(t): # type: (Dict[str, Any]) -> None
118123

119124
upd = doc
120125
if isinstance(upd, MutableMapping) and "$graph" in upd:
121-
upd = upd["$graph"]
126+
upd = cast(CommentedMap, upd["$graph"])
122127
for proc in aslist(upd):
123128
proc.setdefault("hints", CommentedSeq())
124129
proc["hints"].insert(
@@ -136,33 +141,35 @@ def fix_inputBinding(t): # type: (Dict[str, Any]) -> None
136141
return (doc, "v1.1")
137142

138143

139-
def v1_1_0dev1to1_1(doc, loader, baseuri): # pylint: disable=unused-argument
140-
# type: (Any, Loader, str) -> Tuple[Any, str]
144+
def v1_1_0dev1to1_1(
145+
doc: CommentedMap, loader: Loader, baseuri: str
146+
) -> Tuple[CommentedMap, str]: # pylint: disable=unused-argument
141147
return (doc, "v1.1")
142148

143149

144-
def v1_2_0dev1todev2(doc, loader, baseuri): # pylint: disable=unused-argument
145-
# type: (Any, Loader, str) -> Tuple[Any, str]
150+
def v1_2_0dev1todev2(
151+
doc: CommentedMap, loader: Loader, baseuri: str
152+
) -> Tuple[CommentedMap, str]: # pylint: disable=unused-argument
146153
return (doc, "v1.2.0-dev2")
147154

148155

149156
def v1_2_0dev2todev3(
150-
doc: Any, loader: Loader, baseuri: str
151-
) -> Tuple[Any, str]: # pylint: disable=unused-argument
157+
doc: CommentedMap, loader: Loader, baseuri: str
158+
) -> Tuple[CommentedMap, str]: # pylint: disable=unused-argument
152159
"""Public updater for v1.2.0-dev2 to v1.2.0-dev3"""
153160
doc = copy.deepcopy(doc)
154161

155-
def update_pickvalue(t: Any) -> None:
156-
for step in t["steps"]:
157-
for inp in step["in"]:
162+
def update_pickvalue(t: CWLObjectType) -> None:
163+
for step in cast(MutableSequence[CWLObjectType], t["steps"]):
164+
for inp in cast(MutableSequence[CWLObjectType], step["in"]):
158165
if "pickValue" in inp:
159166
if inp["pickValue"] == "only_non_null":
160167
inp["pickValue"] = "the_only_non_null"
161168

162169
visit_class(doc, "Workflow", update_pickvalue)
163170
upd = doc
164171
if isinstance(upd, MutableMapping) and "$graph" in upd:
165-
upd = upd["$graph"]
172+
upd = cast(CommentedMap, upd["$graph"])
166173
for proc in aslist(upd):
167174
if "cwlVersion" in proc:
168175
del proc["cwlVersion"]
@@ -172,14 +179,14 @@ def update_pickvalue(t: Any) -> None:
172179
UPDATES = {
173180
u"v1.0": v1_0to1_1,
174181
u"v1.1": v1_1to1_2,
175-
} # type: Dict[str, Optional[Callable[[Any, Loader, str], Tuple[Any, str]]]]
182+
} # type: Dict[str, Optional[Callable[[CommentedMap, Loader, str], Tuple[CommentedMap, str]]]]
176183

177184
DEVUPDATES = {
178185
u"v1.1.0-dev1": v1_1_0dev1to1_1,
179186
u"v1.2.0-dev1": v1_2_0dev1todev2,
180187
u"v1.2.0-dev2": v1_2_0dev2todev3,
181188
u"v1.2.0-dev3": None,
182-
} # type: Dict[str, Optional[Callable[[Any, Loader, str], Tuple[Any, str]]]]
189+
} # type: Dict[str, Optional[Callable[[CommentedMap, Loader, str], Tuple[CommentedMap, str]]]]
183190

184191

185192
ALLUPDATES = UPDATES.copy()
@@ -190,18 +197,16 @@ def update_pickvalue(t: Any) -> None:
190197
ORIGINAL_CWLVERSION = "http://commonwl.org/cwltool#original_cwlVersion"
191198

192199

193-
def identity(doc, loader, baseuri): # pylint: disable=unused-argument
194-
# type: (Any, Loader, str) -> Tuple[Any, Union[str, str]]
200+
def identity(
201+
doc: CommentedMap, loader: Loader, baseuri: str
202+
) -> Tuple[CommentedMap, str]: # pylint: disable=unused-argument
195203
"""Default, do-nothing, CWL document upgrade function."""
196-
return (doc, doc["cwlVersion"])
204+
return (doc, cast(str, doc["cwlVersion"]))
197205

198206

199207
def checkversion(
200-
doc, # type: Union[CommentedSeq, CommentedMap]
201-
metadata, # type: CommentedMap
202-
enable_dev, # type: bool
203-
):
204-
# type: (...) -> Tuple[CommentedMap, str]
208+
doc: Union[CommentedSeq, CommentedMap], metadata: CommentedMap, enable_dev: bool,
209+
) -> Tuple[CommentedMap, str]:
205210
"""Check the validity of the version of the give CWL document.
206211
207212
Returns the document and the validated version string.
@@ -251,15 +256,20 @@ def checkversion(
251256
return (cdoc, version)
252257

253258

254-
def update(doc, loader, baseuri, enable_dev, metadata):
255-
# type: (Union[CommentedSeq, CommentedMap], Loader, str, bool, Any) -> CommentedMap
259+
def update(
260+
doc: Union[CommentedSeq, CommentedMap],
261+
loader: Loader,
262+
baseuri: str,
263+
enable_dev: bool,
264+
metadata: CommentedMap,
265+
) -> CommentedMap:
256266

257267
(cdoc, version) = checkversion(doc, metadata, enable_dev)
258268
originalversion = copy.copy(version)
259269

260270
nextupdate = (
261271
identity
262-
) # type: Optional[Callable[[Any, Loader, str], Tuple[Any, str]]]
272+
) # type: Optional[Callable[[CommentedMap, Loader, str], Tuple[CommentedMap, str]]]
263273

264274
while nextupdate:
265275
(cdoc, version) = nextupdate(cdoc, loader, baseuri)

0 commit comments

Comments
 (0)