Skip to content

Commit

Permalink
fix size() given a null input
Browse files Browse the repository at this point in the history
  • Loading branch information
mlin committed Feb 17, 2020
1 parent 7a8ba9f commit 1aa1d3a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
9 changes: 7 additions & 2 deletions WDL/StdLib.py
Original file line number Diff line number Diff line change
Expand Up @@ -657,12 +657,17 @@ def infer_type(self, expr: "Expr.Apply") -> Type.Base:

def _call_eager(self, expr: "Expr.Apply", arguments: List[Value.Base]) -> Value.Base:
# this default implementation attempts os.path.getsize() on the argument(s)
files = arguments[0].coerce(Type.Array(Type.File()))
files = arguments[0].coerce(Type.Array(Type.File(optional=True)))
unit = arguments[1].coerce(Type.String()) if len(arguments) > 1 else None

ans = []
for file in files.value:
ans.append(os.path.getsize(self.stdlib._devirtualize_filename(file.value)))
if isinstance(file, Value.File):
ans.append(os.path.getsize(self.stdlib._devirtualize_filename(file.value)))
elif isinstance(file, Value.Null):
ans.append(0)
else:
assert False
ans = float(sum(ans))

if unit:
Expand Down
2 changes: 2 additions & 0 deletions WDL/Value.py
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ def coerce(self, desired_type: Optional[Type.Base] = None) -> Base:
# had check_quant=False
if isinstance(desired_type, Type.String):
return String("", self.expr)
if isinstance(desired_type, Type.Array) and desired_type.item_type.optional:
return Array(desired_type, [self.coerce(desired_type.item_type)], self.expr)
if self.expr:
raise Error.NullValue(self.expr)
raise Error.InputError("'None' for non-optional input/declaration")
Expand Down
5 changes: 5 additions & 0 deletions tests/test_5stdlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,7 @@ def test_size(self):
version 1.0
task hello {
Array[File] files
File? nullfile
Array[Float] sizes_ = [
size(files[0]),
size(files),
Expand All @@ -301,6 +302,8 @@ def test_size(self):
output {
Array[Float] sizes = flatten([sizes_, [size(files, "GB"), size(files, "Gi")]])
Float size2 = size("alyssa_ben.txt", "KiB")
Float nosize1 = size(nullfile)
Float nosize2 = size([files[0], nullfile])
}
}
""", {"files": [ os.path.join(self._dir, "alyssa.txt"),
Expand All @@ -313,6 +316,8 @@ def test_size(self):
self.assertAlmostEqual(outputs["sizes"][4], 11/1000000000)
self.assertAlmostEqual(outputs["sizes"][5], 11/1073741824)
self.assertAlmostEqual(outputs["size2"], 11/1024)
self.assertEqual(outputs["nosize1"], 0)
self.assertEqual(outputs["nosize2"], 7)

self._test_task(R"""
version 1.0
Expand Down

0 comments on commit 1aa1d3a

Please sign in to comment.