Skip to content

Commit 239499d

Browse files
committed
remove asserts
1 parent 25d8446 commit 239499d

File tree

13 files changed

+71
-74
lines changed

13 files changed

+71
-74
lines changed

cwltool/builder.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,13 +207,12 @@ def bind_input(self,
207207
value_from_expression = False
208208
if "inputBinding" in schema and isinstance(schema["inputBinding"], MutableMapping):
209209
binding = CommentedMap(schema["inputBinding"].items())
210-
assert binding is not None
211210

212211
bp = list(aslist(lead_pos))
213212
if "position" in binding:
214213
position = binding["position"]
215-
if isinstance(position, str): # no need to test the CWL Version
216-
# the schema for v1.0 only allow ints
214+
if isinstance(position, str): # no need to test the CWL Version
215+
# the schema for v1.0 only allow ints
217216
binding['position'] = self.do_eval(position, context=datum)
218217
bp.append(binding['position'])
219218
else:
@@ -238,7 +237,6 @@ def bind_input(self,
238237
avsc = self.names.get_name(t["name"], "")
239238
if not avsc:
240239
avsc = make_avsc_object(convert_to_dict(t), self.names)
241-
assert avsc is not None
242240
if validate.validate(avsc, datum):
243241
schema = copy.deepcopy(schema)
244242
schema["type"] = t
@@ -379,7 +377,6 @@ def tostr(self, value): # type: (Any) -> Text
379377
# docker_req is none only when there is no dockerRequirement
380378
# mentioned in hints and Requirement
381379
path = docker_windows_path_adjust(value["path"])
382-
assert path is not None
383380
return path
384381
return value["path"]
385382
else:
@@ -424,8 +421,7 @@ def generate_arg(self, binding): # type: (Dict[Text, Any]) -> List[Text]
424421
if sep:
425422
args.extend([prefix, self.tostr(j)])
426423
else:
427-
assert prefix is not None
428-
args.append(prefix + self.tostr(j))
424+
args.append("" if not prefix else prefix + self.tostr(j))
429425

430426
return [a for a in args if a is not None]
431427

cwltool/command_line_tool.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,8 @@ def revmap_file(builder, outdir, f):
157157
if "basename" not in f:
158158
f["basename"] = os.path.basename(path)
159159

160-
assert builder.pathmapper is not None
160+
if not builder.pathmapper:
161+
raise ValueError("Do not call revmap_file using a builder that doesn't have a pathmapper.")
161162
revmap_f = builder.pathmapper.reversemap(path)
162163

163164
if revmap_f and not builder.pathmapper.mapper(revmap_f[0]).type.startswith("Writable"):
@@ -204,7 +205,8 @@ def check_adjust(builder, file_o):
204205
doesn't reach everything in builder.bindings
205206
"""
206207

207-
assert builder.pathmapper is not None
208+
if not builder.pathmapper:
209+
raise ValueError("Do not call check_adjust using a builder that doesn't have a pathmapper.")
208210
file_o["path"] = docker_windows_path_adjust(
209211
builder.pathmapper.mapper(file_o["location"])[1])
210212
dn, bn = os.path.split(file_o["path"])
@@ -478,24 +480,24 @@ def rm_pending_output_callback(output_callbacks, jobcachepending,
478480
if self.tool.get("stdin"):
479481
with SourceLine(self.tool, "stdin", validate.ValidationException, debug):
480482
j.stdin = builder.do_eval(self.tool["stdin"])
481-
assert j.stdin is not None
482-
reffiles.append({"class": "File", "path": j.stdin})
483+
if j.stdin:
484+
reffiles.append({"class": "File", "path": j.stdin})
483485

484486
if self.tool.get("stderr"):
485487
with SourceLine(self.tool, "stderr", validate.ValidationException, debug):
486488
j.stderr = builder.do_eval(self.tool["stderr"])
487-
assert j.stderr is not None
488-
if os.path.isabs(j.stderr) or ".." in j.stderr:
489-
raise validate.ValidationException(
490-
"stderr must be a relative path, got '%s'" % j.stderr)
489+
if j.stderr:
490+
if os.path.isabs(j.stderr) or ".." in j.stderr:
491+
raise validate.ValidationException(
492+
"stderr must be a relative path, got '%s'" % j.stderr)
491493

492494
if self.tool.get("stdout"):
493495
with SourceLine(self.tool, "stdout", validate.ValidationException, debug):
494496
j.stdout = builder.do_eval(self.tool["stdout"])
495-
assert j.stdout is not None
496-
if os.path.isabs(j.stdout) or ".." in j.stdout or not j.stdout:
497-
raise validate.ValidationException(
498-
"stdout must be a relative path, got '%s'" % j.stdout)
497+
if j.stdout:
498+
if os.path.isabs(j.stdout) or ".." in j.stdout or not j.stdout:
499+
raise validate.ValidationException(
500+
"stdout must be a relative path, got '%s'" % j.stdout)
499501

500502
if debug:
501503
_logger.debug(u"[job %s] command line bindings is %s", j.name,

cwltool/cwlrdf.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ def printrdf(wflow, ctx, style): # type: (Process, ContextType, Text) -> Text
2727
rdf = gather(wflow, ctx).serialize(format=style, encoding='utf-8')
2828
if not rdf:
2929
return u""
30-
assert rdf is not None
3130
return rdf.decode('utf-8')
3231

3332

cwltool/docker.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ def get_image(docker_requirement, # type: Dict[Text, Text]
155155
else:
156156
loadproc = subprocess.Popen(cmd, stdin=subprocess.PIPE,
157157
stdout=sys.stderr)
158-
assert loadproc.stdin is not None
158+
assert loadproc.stdin is not None # nosec
159159
_logger.info(u"Sending GET request to %s", docker_requirement["dockerLoad"])
160160
req = requests.get(docker_requirement["dockerLoad"], stream=True)
161161
size = 0

cwltool/executors.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -167,17 +167,17 @@ def run_jobs(self,
167167
self.output_dirs.add(job.outdir)
168168
if runtime_context.research_obj is not None:
169169
if not isinstance(process, Workflow):
170-
runtime_context.prov_obj = process.provenance_object
170+
prov_obj = process.provenance_object
171171
else:
172-
runtime_context.prov_obj = job.prov_obj
173-
assert runtime_context.prov_obj
174-
runtime_context.prov_obj.evaluate(
175-
process, job, job_order_object,
176-
runtime_context.research_obj)
177-
process_run_id =\
178-
runtime_context.prov_obj.record_process_start(
179-
process, job)
180-
runtime_context = runtime_context.copy()
172+
prov_obj = job.prov_obj
173+
if prov_obj:
174+
runtime_context.prov_obj = prov_obj
175+
prov_obj.evaluate(
176+
process, job, job_order_object,
177+
runtime_context.research_obj)
178+
process_run_id =\
179+
prov_obj.record_process_start(process, job)
180+
runtime_context = runtime_context.copy()
181181
runtime_context.process_run_id = process_run_id
182182
job.run(runtime_context)
183183
else:

cwltool/expression.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -261,8 +261,8 @@ def do_eval(ex, # type: Union[Text, Dict]
261261
): # type: (...) -> Any
262262

263263
runtime = copy.deepcopy(resources) # type: Dict[str, Any]
264-
runtime["tmpdir"] = docker_windows_path_adjust(tmpdir)
265-
runtime["outdir"] = docker_windows_path_adjust(outdir)
264+
runtime["tmpdir"] = docker_windows_path_adjust(tmpdir) if tmpdir else None
265+
runtime["outdir"] = docker_windows_path_adjust(outdir) if outdir else None
266266

267267
rootvars = {
268268
u"inputs": jobinput,
@@ -274,8 +274,7 @@ def do_eval(ex, # type: Union[Text, Dict]
274274
if six.PY3:
275275
rootvars = bytes2str_in_dicts(rootvars) # type: ignore
276276

277-
if needs_parsing(ex):
278-
assert isinstance(ex, string_types)
277+
if isinstance(ex, string_types) and needs_parsing(ex):
279278
fullJS = False
280279
jslib = u""
281280
for r in reversed(requirements):

cwltool/job.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -257,10 +257,14 @@ def _execute(self,
257257
u' 2> %s' % os.path.join(self.outdir, self.stderr) if self.stderr else '')
258258
if self.joborder is not None and runtimeContext.research_obj is not None:
259259
job_order = self.joborder
260-
assert runtimeContext.process_run_id is not None
261-
assert runtimeContext.prov_obj is not None
262-
runtimeContext.prov_obj.used_artefacts(
263-
job_order, runtimeContext.process_run_id, str(self.name))
260+
if runtimeContext.process_run_id is not None \
261+
and runtimeContext.prov_obj is not None:
262+
runtimeContext.prov_obj.used_artefacts(
263+
job_order, runtimeContext.process_run_id, str(self.name))
264+
else:
265+
_logger.warning("research_obj set but one of process_run_id "
266+
"or prov_obj is missing from runtimeContext: "
267+
"{}". format(runtimeContext))
264268
outputs = {} # type: MutableMapping[Text,Any]
265269
try:
266270
stdin_path = None
@@ -323,10 +327,13 @@ def _execute(self,
323327
processStatus = "permanentFail"
324328

325329
if 'listing' in self.generatefiles:
326-
assert self.generatemapper is not None
327-
relink_initialworkdir(
328-
self.generatemapper, self.outdir, self.builder.outdir,
329-
inplace_update=self.inplace_update)
330+
if self.generatemapper:
331+
relink_initialworkdir(
332+
self.generatemapper, self.outdir, self.builder.outdir,
333+
inplace_update=self.inplace_update)
334+
else:
335+
raise ValueError("'lsiting' in self.generatefiles but no "
336+
"generatemapper was setup.")
330337

331338
outputs = self.collect_outputs(self.outdir, rcode)
332339
outputs = bytes2str_in_dicts(outputs) # type: ignore

cwltool/load_tool.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,18 @@ def fetch_document(argsworkflow, # type: Union[Text, Dict[Text, Any]]
108108
if loadingContext.loader is None:
109109
loadingContext.loader = default_loader(loadingContext.fetcher_constructor)
110110

111-
uri = None # type: Optional[Text]
112-
workflowobj = None # type: Optional[CommentedMap]
113111
if isinstance(argsworkflow, string_types):
114112
uri, fileuri = resolve_tool_uri(argsworkflow,
115113
resolver=loadingContext.resolver,
116114
document_loader=loadingContext.loader)
117115
workflowobj = loadingContext.loader.fetch(fileuri)
118-
elif isinstance(argsworkflow, dict):
116+
return loadingContext, workflowobj, uri
117+
if isinstance(argsworkflow, dict):
119118
uri = argsworkflow["id"] if argsworkflow.get("id") else "_:" + Text(uuid.uuid4())
120119
workflowobj = cast(CommentedMap, cmap(argsworkflow, fn=uri))
121120
loadingContext.loader.idx[uri] = workflowobj
122-
else:
123-
raise ValidationException("Must be URI or object: '%s'" % argsworkflow)
124-
assert workflowobj is not None
125-
126-
return loadingContext, workflowobj, uri
121+
return loadingContext, workflowobj, uri
122+
raise ValidationException("Must be URI or object: '%s'" % argsworkflow)
127123

128124

129125
def _convert_stdstreams_to_files(workflowobj):

cwltool/main.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -645,18 +645,18 @@ def formatTime(self, record, datefmt=None):
645645
loadingContext, workflowobj, uri = fetch_document(
646646
uri, loadingContext)
647647

648-
assert loadingContext.loader is not None
649-
650-
if args.print_deps:
648+
if args.print_deps and loadingContext.loader:
651649
printdeps(workflowobj, loadingContext.loader, stdout,
652-
args.relative_deps, uri)
650+
args.relative_deps, uri)
653651
return 0
654652

655653
loadingContext, uri \
656654
= resolve_and_validate_document(loadingContext, workflowobj, uri,
657655
preprocess_only=(args.print_pre or args.pack),
658656
skip_schemas=args.skip_schemas)
659-
assert loadingContext.loader is not None
657+
658+
if loadingContext.loader is None:
659+
raise Exception("Impossible code path.")
660660
processobj, metadata = loadingContext.loader.resolve_ref(uri)
661661
processobj = cast(CommentedMap, processobj)
662662
if args.pack:
@@ -789,7 +789,6 @@ def my_represent_none(self, data): # pylint: disable=unused-argument
789789
runtimeContext.select_resources = executor.select_resources
790790
else:
791791
executor = SingleJobExecutor()
792-
assert executor is not None
793792

794793
try:
795794
runtimeContext.basedir = input_basedir
@@ -865,12 +864,14 @@ def loc_to_path(obj):
865864

866865
finally:
867866
if args and runtimeContext and runtimeContext.research_obj \
868-
and workflowobj:
867+
and workflowobj and loadingContext:
869868
research_obj = runtimeContext.research_obj
870-
assert loadingContext is not None
871-
assert loadingContext.loader is not None
872-
prov_dependencies = prov_deps(workflowobj, loadingContext.loader, uri)
873-
research_obj.generate_snapshot(prov_dependencies)
869+
if loadingContext.loader is not None:
870+
research_obj.generate_snapshot(prov_deps(
871+
workflowobj, loadingContext.loader, uri))
872+
else:
873+
_logger.warning("Unable to generate provenance snapshot "
874+
" due to missing loadingContext.loader.")
874875
if prov_log_handler is not None:
875876
# Stop logging so we won't half-log adding ourself to RO
876877
_logger.debug(u"[provenance] Closing provenance log file %s",

cwltool/provenance.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -501,14 +501,13 @@ def declare_file(self, value):
501501
if value["class"] != "File":
502502
raise ValueError("Must have class:File: %s" % value)
503503
# Need to determine file hash aka RO filename
504-
entity = None # type: Optional[ProvEntity]
504+
entity = None # type: Optional[ProvEntity]
505505
checksum = None
506506
if 'checksum' in value:
507507
csum = value['checksum']
508508
(method, checksum) = csum.split("$", 1)
509-
assert checksum
510509
if method == SHA1 and \
511-
self.research_object.has_data_file(checksum):
510+
self.research_object.has_data_file(checksum):
512511
entity = self.document.entity("data:" + checksum)
513512

514513
if not entity and 'location' in value:
@@ -530,7 +529,7 @@ def declare_file(self, value):
530529
entity, checksum = self.declare_string(value["contents"])
531530

532531
# By here one of them should have worked!
533-
if not entity:
532+
if not entity or not checksum:
534533
raise ValueError("class:File but missing checksum/location/content: %r" % value)
535534

536535

@@ -564,8 +563,6 @@ def declare_file(self, value):
564563
sec_entity, file_entity,
565564
other_attributes={PROV["type"]: CWLPROV["SecondaryFile"]})
566565

567-
assert entity
568-
assert checksum
569566
return file_entity, entity, checksum
570567

571568
def declare_directory(self, value): # type: (MutableMapping) -> ProvEntity
@@ -603,7 +600,7 @@ def declare_directory(self, value): # type: (MutableMapping) -> ProvEntity
603600
# a later call to this method will sort that
604601
is_empty = True
605602

606-
if not "listing" in value:
603+
if "listing" not in value:
607604
fsaccess = StdFsAccess("")
608605
get_listing(fsaccess, value)
609606
for entry in value.get("listing", []):

0 commit comments

Comments
 (0)