Skip to content

Commit e1beae2

Browse files
committed
expression-refactor: fix detection of non-string Sequences
1 parent 8d13b61 commit e1beae2

File tree

4 files changed

+76
-86
lines changed

4 files changed

+76
-86
lines changed

cwl_utils/cwl_v1_0_expression_refactor.py

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
CWLParameterContext,
2626
CWLRuntimeParameterContext,
2727
is_file_or_directory,
28+
is_sequence,
2829
)
2930

3031

@@ -144,7 +145,7 @@ def _clean_type_ids(
144145
cwltype: InputTypeSchemas | CommandOutputTypeSchemas,
145146
) -> None:
146147
if isinstance(cwltype, cwl.ArraySchema):
147-
if isinstance(cwltype.items, Sequence):
148+
if is_sequence(cwltype.items):
148149
for item in cwltype.items:
149150
if hasattr(item, "id"):
150151
item.id = item.id.split("#")[-1]
@@ -173,7 +174,7 @@ def clean_type_ids(
173174
) -> AnyTypeSchema:
174175
"""Simplify type identifiers."""
175176
result = copy.deepcopy(cwltype)
176-
if isinstance(result, Sequence):
177+
if is_sequence(result):
177178
for item in result:
178179
_clean_type_ids(item)
179180
else:
@@ -190,7 +191,7 @@ def _has_expression(string: str) -> bool:
190191

191192

192193
def has_expression(field: str | Sequence[str]) -> bool:
193-
if isinstance(field, Sequence):
194+
if is_sequence(field):
194195
for entry in field:
195196
if _has_expression(entry):
196197
return True
@@ -284,7 +285,7 @@ def _plain_input_schema_to_clt_input_schema(
284285
def plain_input_schema_to_clt_input_schema(
285286
input_type: InputTypeSchemas,
286287
) -> CommandInputTypeSchemas:
287-
if isinstance(input_type, Sequence):
288+
if is_sequence(input_type):
288289
return [
289290
_plain_input_schema_to_clt_input_schema(input_type_item)
290291
for input_type_item in input_type
@@ -324,7 +325,7 @@ def _plain_input_schema_to_plain_output_schema(
324325
def plain_input_schema_to_plain_output_schema(
325326
input_type: InputTypeSchemas,
326327
) -> OutputTypeSchemas:
327-
if isinstance(input_type, Sequence):
328+
if is_sequence(input_type):
328329
return [
329330
_plain_input_schema_to_plain_output_schema(input_type_item)
330331
for input_type_item in input_type
@@ -364,7 +365,7 @@ def _plain_output_type_to_clt_output_type(
364365
def plain_output_type_to_clt_output_type(
365366
output_type: OutputTypeSchemas,
366367
) -> CommandOutputTypeSchemas:
367-
if isinstance(output_type, Sequence):
368+
if is_sequence(output_type):
368369
return [
369370
_plain_output_type_to_clt_output_type(output_type_item)
370371
for output_type_item in output_type
@@ -605,11 +606,11 @@ def generate_etool_from_expr(
605606
self_type = target
606607
assert self_type is not None
607608
new_type: InputTypeSchemas
608-
if isinstance(self_type, Sequence):
609+
if is_sequence(self_type):
609610
new_type_list: MutableSequence[BasicInputTypeSchemas] = []
610611
for entry in self_type:
611612
clean_type = clean_type_ids(entry.type_)
612-
if isinstance(clean_type, Sequence):
613+
if is_sequence(clean_type):
613614
new_type_list.extend(clean_type)
614615
elif clean_type is None:
615616
pass
@@ -621,27 +622,21 @@ def generate_etool_from_expr(
621622
inputs.append(
622623
cwl.InputParameter(
623624
id="self",
624-
label=self_type.label if not isinstance(self_type, list) else None,
625+
label=self_type.label if not is_sequence(self_type) else None,
625626
secondaryFiles=(
626-
self_type.secondaryFiles
627-
if not isinstance(self_type, list)
628-
else None
627+
self_type.secondaryFiles if not is_sequence(self_type) else None
629628
),
630629
streamable=(
631-
self_type.streamable if not isinstance(self_type, list) else None
630+
self_type.streamable if not is_sequence(self_type) else None
632631
),
633-
doc=self_type.doc if not isinstance(self_type, list) else None,
634-
format=self_type.format if not isinstance(self_type, list) else None,
632+
doc=self_type.doc if not is_sequence(self_type) else None,
633+
format=(self_type.format if not is_sequence(self_type) else None),
635634
type_=new_type,
636635
extension_fields=(
637-
self_type.extension_fields
638-
if not isinstance(self_type, list)
639-
else None
636+
self_type.extension_fields if not is_sequence(self_type) else None
640637
),
641638
loadingOptions=(
642-
self_type.loadingOptions
643-
if not isinstance(self_type, list)
644-
else None
639+
self_type.loadingOptions if not is_sequence(self_type) else None
645640
),
646641
)
647642
)
@@ -1782,7 +1777,7 @@ def traverse_CommandLineTool(
17821777
sub_wf_inputs = process_inputs_to_etool_inputs(clt)
17831778
orig_step_inputs = copy.deepcopy(step.in_)
17841779
for orig_step_input in orig_step_inputs:
1785-
if isinstance(orig_step_input.source, Sequence):
1780+
if is_sequence(orig_step_input.source):
17861781
new_orig_step_input_source = list(orig_step_input.source)
17871782
for index, source in enumerate(orig_step_input.source):
17881783
new_orig_step_input_source[index] = source.split("#")[
@@ -2238,7 +2233,7 @@ def traverse_step(
22382233
)
22392234
else:
22402235
scattered_source_type = utils.type_for_source(parent, source)
2241-
if isinstance(scattered_source_type, list):
2236+
if is_sequence(scattered_source_type):
22422237
for stype in scattered_source_type:
22432238
self.append(example_input(stype.type_))
22442239
else:
@@ -2264,7 +2259,7 @@ def traverse_step(
22642259
source_id = source.split("#")[-1]
22652260
input_source_id.append(source_id)
22662261
temp_type = utils.type_for_source(step.run, source_id, parent)
2267-
if isinstance(temp_type, list):
2262+
if is_sequence(temp_type):
22682263
for ttype in temp_type:
22692264
if ttype not in source_types:
22702265
source_types.append(ttype)
@@ -2347,7 +2342,7 @@ def workflow_step_to_InputParameters(
23472342
param = copy.deepcopy(
23482343
utils.param_for_source_id(parent, sourcenames=inp.source)
23492344
)
2350-
if isinstance(param, Sequence):
2345+
if is_sequence(param):
23512346
for p in param:
23522347
if not p.type_:
23532348
raise WorkflowException(

cwl_utils/cwl_v1_1_expression_refactor.py

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
CWLParameterContext,
2626
CWLRuntimeParameterContext,
2727
is_file_or_directory,
28+
is_sequence,
2829
)
2930

3031

@@ -146,7 +147,7 @@ def _clean_type_ids(
146147
cwltype: InputTypeSchemas | CommandOutputTypeSchemas,
147148
) -> None:
148149
if isinstance(cwltype, cwl.ArraySchema):
149-
if isinstance(cwltype.items, Sequence):
150+
if is_sequence(cwltype.items):
150151
for item in cwltype.items:
151152
if hasattr(item, "id"):
152153
item.id = item.id.split("#")[-1]
@@ -175,7 +176,7 @@ def clean_type_ids(
175176
) -> AnyTypeSchema:
176177
"""Simplify type identifiers."""
177178
result = copy.deepcopy(cwltype)
178-
if isinstance(result, Sequence):
179+
if is_sequence(result):
179180
for item in result:
180181
_clean_type_ids(item)
181182
else:
@@ -192,7 +193,7 @@ def _has_expression(string: str) -> bool:
192193

193194

194195
def has_expression(field: str | Sequence[str]) -> bool:
195-
if isinstance(field, Sequence):
196+
if is_sequence(field):
196197
for entry in field:
197198
if _has_expression(entry):
198199
return True
@@ -286,7 +287,7 @@ def _plain_input_schema_to_clt_input_schema(
286287
def plain_input_schema_to_clt_input_schema(
287288
input_type: InputTypeSchemas,
288289
) -> CommandInputTypeSchemas:
289-
if isinstance(input_type, Sequence):
290+
if is_sequence(input_type):
290291
return [
291292
_plain_input_schema_to_clt_input_schema(input_type_item)
292293
for input_type_item in input_type
@@ -324,7 +325,7 @@ def _plain_input_schema_to_plain_output_schema(
324325
def plain_input_schema_to_plain_output_schema(
325326
input_type: InputTypeSchemas,
326327
) -> OutputTypeSchemas:
327-
if isinstance(input_type, Sequence):
328+
if is_sequence(input_type):
328329
return [
329330
_plain_input_schema_to_plain_output_schema(input_type_item)
330331
for input_type_item in input_type
@@ -362,7 +363,7 @@ def _plain_output_type_to_clt_output_type(
362363
def plain_output_type_to_clt_output_type(
363364
output_type: OutputTypeSchemas,
364365
) -> CommandOutputTypeSchemas:
365-
if isinstance(output_type, Sequence):
366+
if is_sequence(output_type):
366367
return [
367368
_plain_output_type_to_clt_output_type(output_type_item)
368369
for output_type_item in output_type
@@ -605,11 +606,11 @@ def generate_etool_from_expr(
605606
self_type = target
606607
assert self_type is not None
607608
new_type: InputTypeSchemas
608-
if isinstance(self_type, Sequence):
609+
if is_sequence(self_type):
609610
new_type_list: MutableSequence[BasicInputTypeSchemas] = []
610611
for entry in self_type:
611612
clean_type = clean_type_ids(entry.type_)
612-
if isinstance(clean_type, Sequence):
613+
if is_sequence(clean_type):
613614
new_type_list.extend(clean_type)
614615
elif clean_type is None:
615616
pass
@@ -621,27 +622,21 @@ def generate_etool_from_expr(
621622
inputs.append(
622623
cwl.WorkflowInputParameter(
623624
id="self",
624-
label=self_type.label if not isinstance(self_type, list) else None,
625+
label=self_type.label if not is_sequence(self_type) else None,
625626
secondaryFiles=(
626-
self_type.secondaryFiles
627-
if not isinstance(self_type, list)
628-
else None
627+
self_type.secondaryFiles if not is_sequence(self_type) else None
629628
),
630629
streamable=(
631-
self_type.streamable if not isinstance(self_type, list) else None
630+
self_type.streamable if not is_sequence(self_type) else None
632631
),
633-
doc=self_type.doc if not isinstance(self_type, list) else None,
634-
format=self_type.format if not isinstance(self_type, list) else None,
632+
doc=self_type.doc if not is_sequence(self_type) else None,
633+
format=(self_type.format if not is_sequence(self_type) else None),
635634
type_=new_type,
636635
extension_fields=(
637-
self_type.extension_fields
638-
if not isinstance(self_type, list)
639-
else None
636+
self_type.extension_fields if not is_sequence(self_type) else None
640637
),
641638
loadingOptions=(
642-
self_type.loadingOptions
643-
if not isinstance(self_type, list)
644-
else None
639+
self_type.loadingOptions if not is_sequence(self_type) else None
645640
),
646641
)
647642
)
@@ -1795,7 +1790,7 @@ def traverse_CommandLineTool(
17951790
sub_wf_inputs = process_inputs_to_etool_inputs(clt)
17961791
orig_step_inputs = copy.deepcopy(step.in_)
17971792
for orig_step_input in orig_step_inputs:
1798-
if isinstance(orig_step_input.source, Sequence):
1793+
if is_sequence(orig_step_input.source):
17991794
new_orig_step_input_source = list(orig_step_input.source)
18001795
for index, source in enumerate(orig_step_input.source):
18011796
new_orig_step_input_source[index] = source.split("#")[
@@ -2253,7 +2248,7 @@ def traverse_step(
22532248
)
22542249
else:
22552250
scattered_source_type = utils.type_for_source(parent, source)
2256-
if isinstance(scattered_source_type, list):
2251+
if is_sequence(scattered_source_type):
22572252
for stype in scattered_source_type:
22582253
self.append(example_input(stype.type_))
22592254
else:
@@ -2281,7 +2276,7 @@ def traverse_step(
22812276
source_id = source.split("#")[-1]
22822277
input_source_id.append(source_id)
22832278
temp_type = utils.type_for_source(step.run, source_id, parent)
2284-
if isinstance(temp_type, list):
2279+
if is_sequence(temp_type):
22852280
for ttype in temp_type:
22862281
if ttype not in source_types:
22872282
source_types.append(ttype)
@@ -2364,7 +2359,7 @@ def workflow_step_to_WorkflowInputParameters(
23642359
param = copy.deepcopy(
23652360
utils.param_for_source_id(parent, sourcenames=inp.source)
23662361
)
2367-
if isinstance(param, Sequence):
2362+
if is_sequence(param):
23682363
for p in param:
23692364
if not p.type_:
23702365
raise WorkflowException(

0 commit comments

Comments
 (0)