Skip to content

Commit 05ba832

Browse files
committed
type cleanups
1 parent 2653450 commit 05ba832

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

cwlupgrader/main.py

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
import logging
77
import stat
88
import sys
9-
from collections.abc import MutableMapping, MutableSequence, Sequence
10-
from io import StringIO
9+
from collections.abc import MutableSequence, Sequence
1110
from pathlib import Path
12-
from typing import Any, Callable, Dict, List, Optional, Set, Union
11+
from typing import Any, Callable, Dict, List, MutableMapping, Optional, Set, Union
1312

1413
import ruamel.yaml
1514
from ruamel.yaml.comments import CommentedMap # for consistent sort order
@@ -73,9 +72,9 @@ def run(args: argparse.Namespace) -> int:
7372

7473

7574
def load_cwl_document(path: str) -> Any:
76-
yaml = ruamel.yaml.YAML(typ="rt")
75+
yaml = ruamel.yaml.main.YAML(typ="rt")
7776
yaml.allow_duplicate_keys = True
78-
yaml.preserve_quotes = True
77+
yaml.preserve_quotes = True # type: ignore
7978
with open(path) as entry:
8079
document = yaml.load(entry)
8180
add_lc_filename(document, entry.name)
@@ -98,7 +97,7 @@ def write_cwl_document(document: Any, name: str, dirname: str) -> None:
9897
def process_imports(
9998
document: Any, imports: Set[str], updater: Callable[[Any, str], Any], outdir: str
10099
) -> None:
101-
if isinstance(document, MutableMapping):
100+
if isinstance(document, CommentedMap):
102101
for key, value in document.items():
103102
if key == "$import":
104103
if value not in imports:
@@ -120,39 +119,39 @@ def process_imports(
120119
process_imports(entry, imports, updater, outdir)
121120

122121

123-
def v1_0_to_v1_1(document: Dict[str, Any], outdir: str) -> Dict[str, Any]:
122+
def v1_0_to_v1_1(document: CommentedMap, outdir: str) -> CommentedMap:
124123
"""CWL v1.0.x to v1.1 transformation loop."""
125124
_v1_0_to_v1_1(document, outdir)
126125
if isinstance(document, MutableMapping):
127126
for key, value in document.items():
128127
with SourceLine(document, key, Exception):
129-
if isinstance(value, Dict):
128+
if isinstance(value, CommentedMap):
130129
document[key] = _v1_0_to_v1_1(value, outdir)
131130
elif isinstance(value, list):
132131
for index, entry in enumerate(value):
133-
if isinstance(entry, Dict):
132+
if isinstance(entry, CommentedMap):
134133
value[index] = _v1_0_to_v1_1(entry, outdir)
135134
document["cwlVersion"] = "v1.1"
136135
return sort_v1_0(document)
137136

138137

139-
def draft3_to_v1_0(document: Dict[str, Any], outdir: str) -> Dict[str, Any]:
138+
def draft3_to_v1_0(document: CommentedMap, outdir: str) -> Dict[str, Any]:
140139
"""Transformation loop."""
141140
_draft3_to_v1_0(document, outdir)
142141
if isinstance(document, MutableMapping):
143142
for key, value in document.items():
144143
with SourceLine(document, key, Exception):
145-
if isinstance(value, Dict):
144+
if isinstance(value, CommentedMap):
146145
document[key] = _draft3_to_v1_0(value, outdir)
147146
elif isinstance(value, list):
148147
for index, entry in enumerate(value):
149-
if isinstance(entry, Dict):
148+
if isinstance(entry, CommentedMap):
150149
value[index] = _draft3_to_v1_0(entry, outdir)
151150
document["cwlVersion"] = "v1.0"
152151
return sort_v1_0(document)
153152

154153

155-
def _draft3_to_v1_0(document: Dict[str, Any], outdir: str) -> Dict[str, Any]:
154+
def _draft3_to_v1_0(document: CommentedMap, outdir: str) -> Dict[str, Any]:
156155
"""Inner loop for transforming draft-3 to v1.0."""
157156
if "class" in document:
158157
if document["class"] == "Workflow":
@@ -192,7 +191,7 @@ def _draft3_to_v1_0(document: Dict[str, Any], outdir: str) -> Dict[str, Any]:
192191
}
193192

194193

195-
def _v1_0_to_v1_1(document: Dict[str, Any], outdir: str) -> Dict[str, Any]:
194+
def _v1_0_to_v1_1(document: CommentedMap, outdir: str) -> Dict[str, Any]:
196195
"""Inner loop for transforming draft-3 to v1.0."""
197196
if "class" in document:
198197
if document["class"] == "Workflow":
@@ -204,7 +203,7 @@ def _v1_0_to_v1_1(document: Dict[str, Any], outdir: str) -> Dict[str, Any]:
204203
for index, entry in enumerate(steps):
205204
with SourceLine(steps, index, Exception):
206205
upgrade_v1_0_hints_and_reqs(entry)
207-
if "run" in entry and isinstance(entry["run"], Dict):
206+
if "run" in entry and isinstance(entry["run"], CommentedMap):
208207
process = entry["run"]
209208
_v1_0_to_v1_1(process, outdir)
210209
if "cwlVersion" in process:
@@ -215,18 +214,21 @@ def _v1_0_to_v1_1(document: Dict[str, Any], outdir: str) -> Dict[str, Any]:
215214
entry = steps[step_name]
216215
upgrade_v1_0_hints_and_reqs(entry)
217216
if "run" in entry:
218-
if isinstance(entry["run"], Dict):
217+
if isinstance(entry["run"], CommentedMap):
219218
process = entry["run"]
220219
_v1_0_to_v1_1(process, outdir)
221220
if "cwlVersion" in process:
222221
del process["cwlVersion"]
223222
elif isinstance(entry["run"], str):
224223
path = Path(document.lc.filename).parent / entry["run"]
225-
process = v1_0__to_v1_1(load_cwl_document(path))
224+
process = v1_0_to_v1_1(
225+
load_cwl_document(str(path)), outdir
226+
)
226227
write_cwl_document(process, path.name, outdir)
227228
else:
228229
raise Exception(
229-
"'run' entry was neither a CWL Process nor a path to one: %s.",
230+
"'run' entry was neither a CWL Process nor "
231+
"a path to one: %s.",
230232
entry["run"],
231233
)
232234
elif document["class"] == "CommandLineTool":
@@ -456,7 +458,7 @@ def input_output_clean(document: Dict[str, Any]) -> None:
456458
document[param_type] = new_section
457459

458460

459-
def array_type_raise_sf(param: Dict[str, Any]) -> None:
461+
def array_type_raise_sf(param: MutableMapping[str, Any]) -> None:
460462
"""Move up draft-3 secondaryFile specs on File members in Arrays."""
461463
typ = param["type"]
462464
if isinstance(typ, MutableSequence):
@@ -544,7 +546,7 @@ def clean_secondary_files(document: Dict[str, Any]) -> None:
544546
).replace(".path", ".location")
545547

546548

547-
def sort_v1_0(document: Dict[str, Any]) -> Dict[str, Any]:
549+
def sort_v1_0(document: Dict[str, Any]) -> CommentedMap:
548550
"""Sort the sections of the CWL document in a more meaningful order."""
549551
keyorder = [
550552
"cwlVersion",

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
ruamel.yaml >= 0.14.12, < 0.16.13
22
typing
3+
schema-salad

0 commit comments

Comments
 (0)