Skip to content

Commit a3cc4fc

Browse files
committed
mypy: warn_unreachable = True
1 parent bb6fe86 commit a3cc4fc

File tree

14 files changed

+85
-112
lines changed

14 files changed

+85
-112
lines changed

cwltool/builder.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ def __init__(
182182
container_engine: str,
183183
) -> None:
184184
"""Initialize this Builder."""
185+
super().__init__()
185186
self.job = job
186187
self.files = files
187188
self.bindings = bindings
@@ -730,9 +731,8 @@ def do_eval(
730731
resources = self.resources
731732
if self.resources and "cores" in self.resources:
732733
cores = resources["cores"]
733-
if not isinstance(cores, str):
734-
resources = copy.copy(resources)
735-
resources["cores"] = int(math.ceil(cores))
734+
resources = copy.copy(resources)
735+
resources["cores"] = int(math.ceil(cores))
736736

737737
return expression.do_eval(
738738
ex,

cwltool/command_line_tool.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -468,9 +468,9 @@ def _initialworkdir(self, j: JobBase, builder: Builder) -> None:
468468
return
469469
debug = _logger.isEnabledFor(logging.DEBUG)
470470
cwl_version = cast(Optional[str], self.metadata.get(ORIGINAL_CWLVERSION, None))
471-
classic_dirent = cwl_version and ORDERED_VERSIONS.index(
472-
cwl_version
473-
) < ORDERED_VERSIONS.index("v1.2.0-dev2")
471+
classic_dirent: bool = cwl_version is not None and (
472+
ORDERED_VERSIONS.index(cwl_version) < ORDERED_VERSIONS.index("v1.2.0-dev2")
473+
)
474474
classic_listing = cwl_version and ORDERED_VERSIONS.index(
475475
cwl_version
476476
) < ORDERED_VERSIONS.index("v1.1.0-dev1")
@@ -486,8 +486,19 @@ def _initialworkdir(self, j: JobBase, builder: Builder) -> None:
486486
if not isinstance(ls_evaluated, MutableSequence):
487487
fail = ls_evaluated
488488
else:
489-
for entry in ls_evaluated:
490-
if isinstance(entry, MutableSequence):
489+
ls_evaluated2 = cast(
490+
MutableSequence[Union[None, CWLOutputType]], ls_evaluated
491+
)
492+
for entry in ls_evaluated2:
493+
if entry == None: # noqa
494+
if classic_dirent:
495+
fail = entry
496+
fail_suffix = (
497+
" Dirent.entry cannot return 'null' before CWL "
498+
"v1.2. Please consider using 'cwl-upgrader' to "
499+
"upgrade your document to CWL version v1.2."
500+
)
501+
elif isinstance(entry, MutableSequence):
491502
if classic_listing:
492503
raise SourceLine(
493504
initialWorkdir, "listing", WorkflowException, debug
@@ -518,19 +529,11 @@ def _initialworkdir(self, j: JobBase, builder: Builder) -> None:
518529
and (
519530
"class" in entry
520531
and (entry["class"] == "File" or "Directory")
521-
or "entry" in "entry"
532+
or "entry" in entry
522533
)
523534
)
524535
):
525536
fail = entry
526-
elif entry is None:
527-
if classic_dirent:
528-
fail = entry
529-
fail_suffix = (
530-
" Dirent.entry cannot return 'null' before CWL "
531-
"v1.2. Please consider using 'cwl-upgrader' to "
532-
"upgrade your document to CWL version v1.2."
533-
)
534537
if fail is not False:
535538
message = (
536539
"Expression in a 'InitialWorkdirRequirement.listing' field "
@@ -602,7 +605,7 @@ def _initialworkdir(self, j: JobBase, builder: Builder) -> None:
602605
"File",
603606
"Directory",
604607
):
605-
et["entry"] = entry
608+
et["entry"] = cast(CWLOutputType, entry)
606609
else:
607610
if isinstance(entry, str):
608611
et["entry"] = entry

cwltool/context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def __init__(self, kwargs: Optional[Dict[str, Any]] = None) -> None:
5858
self.loader = None # type: Optional[Loader]
5959
self.avsc_names = None # type: Optional[Names]
6060
self.disable_js_validation = False # type: bool
61-
self.js_hint_options_file = None
61+
self.js_hint_options_file: Optional[str] = None
6262
self.do_validate = True # type: bool
6363
self.enable_dev = False # type: bool
6464
self.strict = True # type: bool

cwltool/docker.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,7 @@ def create_runtime(
427427

428428
if runtimeContext.strict_memory_limit and not user_space_docker_cmd:
429429
ram = self.builder.resources["ram"]
430-
if not isinstance(ram, str):
431-
runtime.append("--memory=%dm" % ram)
430+
runtime.append("--memory=%dm" % ram)
432431
elif not user_space_docker_cmd:
433432
res_req, _ = self.builder.get_requirement("ResourceRequirement")
434433
if res_req and ("ramMin" in res_req or "ramMax" in res_req):

cwltool/executors.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from .provenance_profile import ProvenanceProfile
3434
from .task_queue import TaskQueue
3535
from .update import ORIGINAL_CWLVERSION
36-
from .utils import CWLObjectType, JobsType
36+
from .utils import CWLObjectType, CWLOutputType, JobsType
3737
from .workflow import Workflow
3838
from .workflow_job import WorkflowJob, WorkflowJobStep
3939

@@ -327,11 +327,9 @@ def _runner(self, job, runtime_context, TMPDIR_LOCK):
327327
with runtime_context.workflow_eval_lock:
328328
if isinstance(job, JobBase):
329329
ram = job.builder.resources["ram"]
330-
if not isinstance(ram, str):
331-
self.allocated_ram -= ram
330+
self.allocated_ram -= ram
332331
cores = job.builder.resources["cores"]
333-
if not isinstance(cores, str):
334-
self.allocated_cores -= cores
332+
self.allocated_cores -= cores
335333
runtime_context.workflow_eval_lock.notifyAll()
336334

337335
def run_job(
@@ -355,9 +353,7 @@ def run_job(
355353
if isinstance(job, JobBase):
356354
ram = job.builder.resources["ram"]
357355
cores = job.builder.resources["cores"]
358-
if (not isinstance(ram, str) and ram > self.max_ram) or (
359-
not isinstance(cores, str) and cores > self.max_cores
360-
):
356+
if ram > self.max_ram or cores > self.max_cores:
361357
_logger.error(
362358
'Job "%s" cannot be run, requests more resources (%s) '
363359
"than available on this host (max ram %d, max cores %d",
@@ -372,11 +368,8 @@ def run_job(
372368
return
373369

374370
if (
375-
not isinstance(ram, str)
376-
and self.allocated_ram + ram > self.max_ram
377-
) or (
378-
not isinstance(cores, str)
379-
and self.allocated_cores + cores > self.max_cores
371+
self.allocated_ram + ram > self.max_ram
372+
or self.allocated_cores + cores > self.max_cores
380373
):
381374
_logger.debug(
382375
'Job "%s" cannot run yet, resources (%s) are not '
@@ -394,11 +387,9 @@ def run_job(
394387

395388
if isinstance(job, JobBase):
396389
ram = job.builder.resources["ram"]
397-
if not isinstance(ram, str):
398-
self.allocated_ram += ram
390+
self.allocated_ram += ram
399391
cores = job.builder.resources["cores"]
400-
if not isinstance(cores, str):
401-
self.allocated_cores += cores
392+
self.allocated_cores += cores
402393
self.taskqueue.add(
403394
functools.partial(self._runner, job, runtime_context, TMPDIR_LOCK),
404395
runtime_context.workflow_eval_lock,

cwltool/expression.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ def evaluator(
209209
match = param_re.match(ex)
210210

211211
expression_parse_exception = None
212-
expression_parse_succeeded = False
213212

214213
if match is not None:
215214
first_symbol = match.group(1)
@@ -228,10 +227,8 @@ def evaluator(
228227
)
229228
except WorkflowException as werr:
230229
expression_parse_exception = werr
231-
else:
232-
expression_parse_succeeded = True
233230

234-
if fullJS and not expression_parse_succeeded:
231+
if fullJS:
235232
return execjs(
236233
ex,
237234
jslib,

cwltool/job.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ def terminate(): # type: () -> None
992992
if tm is not None:
993993
tm.cancel()
994994

995-
if isinstance(stdin, IOBase) and hasattr(stdin, "close"):
995+
if isinstance(stdin, IO) and hasattr(stdin, "close"):
996996
stdin.close()
997997

998998
if stdout is not sys.stderr and hasattr(stdout, "close"):

cwltool/main.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -239,9 +239,9 @@ def generate_example_input(
239239

240240

241241
def realize_input_schema(
242-
input_types: MutableSequence[CWLObjectType],
242+
input_types: MutableSequence[Union[str, CWLObjectType]],
243243
schema_defs: MutableMapping[str, CWLObjectType],
244-
) -> MutableSequence[CWLObjectType]:
244+
) -> MutableSequence[Union[str, CWLObjectType]]:
245245
"""Replace references to named typed with the actual types."""
246246
for index, entry in enumerate(input_types):
247247
if isinstance(entry, str):
@@ -251,32 +251,33 @@ def realize_input_schema(
251251
input_type_name = entry
252252
if input_type_name in schema_defs:
253253
entry = input_types[index] = schema_defs[input_type_name]
254-
if isinstance(entry, Mapping):
254+
if isinstance(entry, MutableMapping):
255255
if isinstance(entry["type"], str) and "#" in entry["type"]:
256256
_, input_type_name = entry["type"].split("#")
257257
if input_type_name in schema_defs:
258-
input_types[index]["type"] = cast(
258+
entry["type"] = cast(
259259
CWLOutputAtomType,
260260
realize_input_schema(
261261
cast(
262-
MutableSequence[CWLObjectType],
262+
MutableSequence[Union[str, CWLObjectType]],
263263
schema_defs[input_type_name],
264264
),
265265
schema_defs,
266266
),
267267
)
268268
if isinstance(entry["type"], MutableSequence):
269-
input_types[index]["type"] = cast(
269+
entry["type"] = cast(
270270
CWLOutputAtomType,
271271
realize_input_schema(
272-
cast(MutableSequence[CWLObjectType], entry["type"]), schema_defs
272+
cast(MutableSequence[Union[str, CWLObjectType]], entry["type"]),
273+
schema_defs,
273274
),
274275
)
275276
if isinstance(entry["type"], Mapping):
276-
input_types[index]["type"] = cast(
277+
entry["type"] = cast(
277278
CWLOutputAtomType,
278279
realize_input_schema(
279-
[cast(CWLObjectType, input_types[index]["type"])], schema_defs
280+
[cast(CWLObjectType, entry["type"])], schema_defs
280281
),
281282
)
282283
if entry["type"] == "array":
@@ -285,17 +286,20 @@ def realize_input_schema(
285286
if not isinstance(entry["items"], str)
286287
else [entry["items"]]
287288
)
288-
input_types[index]["items"] = cast(
289+
entry["items"] = cast(
289290
CWLOutputAtomType,
290291
realize_input_schema(
291-
cast(MutableSequence[CWLObjectType], items), schema_defs
292+
cast(MutableSequence[Union[str, CWLObjectType]], items),
293+
schema_defs,
292294
),
293295
)
294296
if entry["type"] == "record":
295-
input_types[index]["fields"] = cast(
297+
entry["fields"] = cast(
296298
CWLOutputAtomType,
297299
realize_input_schema(
298-
cast(MutableSequence[CWLObjectType], entry["fields"]),
300+
cast(
301+
MutableSequence[Union[str, CWLObjectType]], entry["fields"]
302+
),
299303
schema_defs,
300304
),
301305
)
@@ -305,8 +309,11 @@ def realize_input_schema(
305309
def generate_input_template(tool: Process) -> CWLObjectType:
306310
"""Generate an example input object for the given CWL process."""
307311
template = ruamel.yaml.comments.CommentedMap()
308-
for inp in realize_input_schema(tool.tool["inputs"], tool.schemaDefs):
309-
name = shortname(cast(str, inp["id"]))
312+
for inp in cast(
313+
List[MutableMapping[str, str]],
314+
realize_input_schema(tool.tool["inputs"], tool.schemaDefs),
315+
):
316+
name = shortname(inp["id"])
310317
value, comment = generate_example_input(inp["type"], inp.get("default", None))
311318
template.insert(0, name, value, comment)
312319
return template
@@ -454,7 +461,7 @@ def init_job_order(
454461
job_order_object = {}
455462
job_order_object[shortname(inp["id"])] = inp["default"]
456463

457-
if job_order_object is None:
464+
if len(job_order_object) == 0:
458465
if process.tool["inputs"]:
459466
if toolparser is not None:
460467
print(f"\nOptions for {args.workflow} ")
@@ -697,6 +704,7 @@ def setup_loadingContext(
697704
runtimeContext: RuntimeContext,
698705
args: argparse.Namespace,
699706
) -> LoadingContext:
707+
"""Prepare a LoadingContext from the given arguments."""
700708
if loadingContext is None:
701709
loadingContext = LoadingContext(vars(args))
702710
loadingContext.singularity = runtimeContext.singularity
@@ -1277,12 +1285,7 @@ def loc_to_path(obj: CWLObjectType) -> None:
12771285
# Unsetting the Generation from final output object
12781286
visit_class(out, ("File",), MutationManager().unset_generation)
12791287

1280-
if isinstance(out, str):
1281-
stdout.write(out)
1282-
else:
1283-
stdout.write(
1284-
json_dumps(out, indent=4, ensure_ascii=False, default=str)
1285-
)
1288+
stdout.write(json_dumps(out, indent=4, ensure_ascii=False, default=str))
12861289
stdout.write("\n")
12871290
if hasattr(stdout, "flush"):
12881291
stdout.flush()

cwltool/provenance.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -959,13 +959,8 @@ def _relativise_files(
959959
relative_path = self.add_data_file(fp)
960960
checksum = PurePosixPath(relative_path).name
961961
structure["checksum"] = f"{SHA1}${checksum}"
962-
if relative_path is not None:
963-
# RO-relative path as new location
964-
structure["location"] = str(PurePosixPath("..") / relative_path)
965-
else:
966-
_logger.warning(
967-
"Could not determine RO path for file %s", structure
968-
)
962+
# RO-relative path as new location
963+
structure["location"] = str(PurePosixPath("..") / relative_path)
969964
if "path" in structure:
970965
del structure["path"]
971966

cwltool/provenance_profile.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,16 @@
66
from io import BytesIO
77
from pathlib import PurePath, PurePosixPath
88
from socket import getfqdn
9-
from typing import List, MutableMapping, MutableSequence, Optional, Tuple, Union, cast
9+
from typing import (
10+
Any,
11+
List,
12+
MutableMapping,
13+
MutableSequence,
14+
Optional,
15+
Tuple,
16+
Union,
17+
cast,
18+
)
1019

1120
from prov.identifier import Identifier
1221
from prov.model import PROV, PROV_LABEL, PROV_TYPE, PROV_VALUE, ProvDocument, ProvEntity
@@ -486,7 +495,7 @@ def declare_string(self, value: str) -> Tuple[ProvEntity, str]:
486495
) # type: ProvEntity
487496
return entity, checksum
488497

489-
def declare_artefact(self, value: Optional[CWLOutputType]) -> ProvEntity:
498+
def declare_artefact(self, value: Any) -> ProvEntity:
490499
"""Create data artefact entities for all file objects."""
491500
if value is None:
492501
# FIXME: If this can happen in CWL, we'll

0 commit comments

Comments
 (0)