Skip to content

Commit 8d7e75d

Browse files
committed
CWL exprs in ResourceRequirement fields can return floats in CWL v1.2+
1 parent 5f3b605 commit 8d7e75d

File tree

4 files changed

+91
-12
lines changed

4 files changed

+91
-12
lines changed

cwltool/process.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
from .pathmapper import MapperEnt, PathMapper
5959
from .secrets import SecretStore
6060
from .stdfsaccess import StdFsAccess
61-
from .update import INTERNAL_VERSION, ORIGINAL_CWLVERSION
61+
from .update import INTERNAL_VERSION, ORDERED_VERSIONS, ORIGINAL_CWLVERSION
6262
from .utils import (
6363
CWLObjectType,
6464
CWLOutputAtomType,
@@ -518,12 +518,17 @@ def eval_resource(
518518
) -> Optional[Union[str, int, float]]:
519519
if isinstance(resource_req, str) and expression.needs_parsing(resource_req):
520520
result = builder.do_eval(resource_req)
521+
if isinstance(result, float):
522+
if ORDERED_VERSIONS.index(builder.cwlVersion) >= ORDERED_VERSIONS.index("v1.2.0-dev4"):
523+
return result
524+
raise WorkflowException(
525+
"Floats are not valid in resource requirement expressions prior "
526+
f"to CWL v1.2: {resource_req} returned {result}."
527+
)
521528
if isinstance(result, (str, int)) or result is None:
522529
return result
523530
raise WorkflowException(
524-
"Got incorrect return type {} from resource expression evaluation of {}.".format(
525-
type(result), resource_req
526-
)
531+
f"Got incorrect return type {type(result)} from resource expression evaluation of {resource_req}."
527532
)
528533
return resource_req
529534

@@ -979,15 +984,17 @@ def evalResources(
979984
continue
980985
mn = mx = None # type: Optional[Union[int, float]]
981986
if rsc.get(a + "Min"):
982-
mn = cast(
983-
Union[int, float],
984-
eval_resource(builder, cast(Union[str, int, float], rsc[a + "Min"])),
985-
)
987+
with SourceLine(rsc, f"{a}Min", WorkflowException, runtimeContext.debug):
988+
mn = cast(
989+
Union[int, float],
990+
eval_resource(builder, cast(Union[str, int, float], rsc[a + "Min"])),
991+
)
986992
if rsc.get(a + "Max"):
987-
mx = cast(
988-
Union[int, float],
989-
eval_resource(builder, cast(Union[str, int, float], rsc[a + "Max"])),
990-
)
993+
with SourceLine(rsc, f"{a}Max", WorkflowException, runtimeContext.debug):
994+
mx = cast(
995+
Union[int, float],
996+
eval_resource(builder, cast(Union[str, int, float], rsc[a + "Max"])),
997+
)
991998
if mn is None:
992999
mn = mx
9931000
elif mx is None:

tests/test_examples.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1799,3 +1799,33 @@ def test_validate_optional_src_with_mandatory_sink() -> None:
17991799
stderr = re.sub(r"\s\s+", " ", stderr)
18001800
assert 'Source \'opt_file\' of type ["null", "File"] may be incompatible' in stderr
18011801
assert "with sink 'r' of type \"File\"" in stderr
1802+
1803+
1804+
def test_res_req_expr_float_1_0() -> None:
1805+
"""Confirm expected error when returning a float value from a ResourceRequirement expr in CWL v1.0."""
1806+
exit_code, stdout, stderr = get_main_output(
1807+
[
1808+
get_data("tests/wf/resreq_expr_float_v1_0.cwl"),
1809+
"--input_bam",
1810+
get_data("tests/wf/whale.txt"),
1811+
]
1812+
)
1813+
assert exit_code == 1
1814+
stderr = re.sub(r"\s\s+", " ", stderr)
1815+
assert "Floats are not valid in resource requirement expressions" in stderr
1816+
assert "prior to CWL v1.2" in stderr
1817+
assert "$((2 * inputs.input_bam.size) / 3.14159) returned" in stderr
1818+
1819+
1820+
def test_res_req_expr_float_1_2() -> None:
1821+
"""Confirm no error when returning a float value from a ResourceRequirement expr in CWL v1.0."""
1822+
exit_code, stdout, stderr = get_main_output(
1823+
[
1824+
get_data("tests/wf/resreq_expr_float_v1_2.cwl"),
1825+
"--input_bam",
1826+
get_data("tests/wf/whale.txt"),
1827+
]
1828+
)
1829+
assert exit_code == 0, stderr
1830+
assert json.loads(stdout)["result"]["outdirSize"] >= 708
1831+
assert json.loads(stdout)["result"]["tmpdirSize"] >= 708

tests/wf/resreq_expr_float_v1_0.cwl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
cwlVersion: v1.0
2+
class: CommandLineTool
3+
requirements:
4+
- class: InlineJavascriptRequirement
5+
- class: ResourceRequirement
6+
tmpdirMin: $((2 * inputs.input_bam.size) / 3.14159)
7+
outdirMin: $((2 * inputs.input_bam.size) / 3.14159)
8+
9+
inputs:
10+
input_bam: File
11+
12+
arguments:
13+
- |
14+
{"result": $(runtime) }
15+
16+
stdout: cwl.output.json
17+
18+
outputs:
19+
result: Any
20+
21+
baseCommand: [echo]

tests/wf/resreq_expr_float_v1_2.cwl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
cwlVersion: v1.2
2+
class: CommandLineTool
3+
requirements:
4+
- class: InlineJavascriptRequirement
5+
- class: ResourceRequirement
6+
tmpdirMin: $((2 * inputs.input_bam.size) / 3.14159)
7+
outdirMin: $((2 * inputs.input_bam.size) / 3.14159)
8+
9+
inputs:
10+
input_bam: File
11+
12+
arguments:
13+
- |
14+
{"result": $(runtime) }
15+
16+
stdout: cwl.output.json
17+
18+
outputs:
19+
result: Any
20+
21+
baseCommand: [echo]

0 commit comments

Comments
 (0)