Skip to content

Fix type checking that a source port is assignable to a sink port #106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions cwltool/workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,27 +82,29 @@ def match_types(sinktype, src, iid, inputobj, linkMerge, valueFrom):
else:
raise WorkflowException(u"Unrecognized linkMerge enum '%s'" % linkMerge)
return True
elif valueFrom is not None or are_same_type(src.parameter["type"], sinktype) or sinktype == "Any":
elif valueFrom is not None or can_assign_src_to_sink(src.parameter["type"], sinktype) or sinktype == "Any":
# simply assign the value from state to input
inputobj[iid] = copy.deepcopy(src.value)
return True
return False

def are_same_type(src, sink): # type: (Any, Any) -> bool
def can_assign_src_to_sink(src, sink): # type: (Any, Any) -> bool
"""Check for identical type specifications, ignoring extra keys like inputBinding.
"""
if isinstance(src, dict) and isinstance(sink, dict):
if src["type"] == "array" and sink["type"] == "array":
if 'null' in sink["items"]:
return are_same_type([src["items"]], [it for it in sink["items"] if it != 'null'])
return are_same_type(src["items"], sink["items"])
elif src["type"] == sink["type"]:
return True
else:
return False
return can_assign_src_to_sink(src["items"], sink["items"])
elif isinstance(src, list):
for t in src:
if can_assign_src_to_sink(t, sink):
return True
elif isinstance(sink, list):
for t in sink:
if can_assign_src_to_sink(src, t):
return True
else:
return src == sink

return False

def object_from_state(state, parms, frag_only, supportsMultipleInput, sourceField):
# type: (Dict[unicode, WorkflowStateItem], List[Dict[unicode, Any]], bool, bool, unicode) -> Dict[unicode, Any]
Expand Down
20 changes: 19 additions & 1 deletion tests/test_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import cwltool.expression as expr
import cwltool.factory
import cwltool.process

import cwltool.workflow

class TestParamMatching(unittest.TestCase):

Expand Down Expand Up @@ -150,6 +150,24 @@ def loadref(base, p):
set(("$include", "$schemas", "path")),
loadref), indent=4)

class TestTypeCompare(unittest.TestCase):
def test_typecompare(self):
self.assertTrue(cwltool.workflow.can_assign_src_to_sink(
{'items': ['string', 'null'], 'type': 'array'},
{'items': ['string', 'null'], 'type': 'array'}))

self.assertTrue(cwltool.workflow.can_assign_src_to_sink(
{'items': ['string'], 'type': 'array'},
{'items': ['string', 'null'], 'type': 'array'}))

self.assertTrue(cwltool.workflow.can_assign_src_to_sink(
{'items': ['string', 'null'], 'type': 'array'},
{'items': ['string'], 'type': 'array'}))

self.assertFalse(cwltool.workflow.can_assign_src_to_sink(
{'items': ['string'], 'type': 'array'},
{'items': ['int'], 'type': 'array'}))


if __name__ == '__main__':
unittest.main()