From 99cb02364aedcc340ebbc16f60e3a75c215cbfae Mon Sep 17 00:00:00 2001 From: Romain Dorgueil Date: Sat, 28 Jul 2018 12:33:37 +0100 Subject: [PATCH] types: fixing type casts. --- bonobo/execution/contexts/node.py | 11 +++++------ bonobo/util/collections.py | 20 +++++++++++++++++++- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/bonobo/execution/contexts/node.py b/bonobo/execution/contexts/node.py index c8d7733..13c1ff7 100644 --- a/bonobo/execution/contexts/node.py +++ b/bonobo/execution/contexts/node.py @@ -257,12 +257,11 @@ class NodeExecutionContext(BaseContext, WithStatistics): :param mixed value: message """ for message in messages: - if isinstance(message, Token): - self.input.put(message) - elif self._input_type: - self.input.put(ensure_tuple(message, cls=self._input_type)) - else: - self.input.put(ensure_tuple(message)) + if not isinstance(message, Token): + message = ensure_tuple(message, cls=self._input_type, length=self._input_length) + if self._input_length is None: + self._input_length = len(message) + self.input.put(message) def write_sync(self, *messages): self.write(BEGIN, *messages, END) diff --git a/bonobo/util/collections.py b/bonobo/util/collections.py index 3e46738..1f72e8c 100644 --- a/bonobo/util/collections.py +++ b/bonobo/util/collections.py @@ -7,7 +7,23 @@ class sortedlist(list): bisect.insort(self, x) -def ensure_tuple(tuple_or_mixed, *, cls=tuple): +def _with_length_check(f): + @functools.wraps(f) + def _wrapped(*args, length=None, **kwargs): + nonlocal f + result = f(*args, **kwargs) + if length is not None: + if length != len(result): + raise TypeError( + 'Length check failed, expected {} fields but got {}: {!r}.'.format(length, len(result), result) + ) + return result + + return _wrapped + + +@_with_length_check +def ensure_tuple(tuple_or_mixed, *, cls=None): """ If it's not a tuple, let's make a tuple of one item. Otherwise, not changed. @@ -16,6 +32,8 @@ def ensure_tuple(tuple_or_mixed, *, cls=tuple): :return: tuple """ + if cls is None: + cls = tuple if isinstance(tuple_or_mixed, cls): return tuple_or_mixed