From eb83d0327e71303fc754f12a4e8cea1838f2bc8b Mon Sep 17 00:00:00 2001 From: Romain Dorgueil Date: Sun, 2 Jun 2019 16:29:13 +0200 Subject: [PATCH] Fixes cast problem when type changed but we got NOT_MODIFIED token. --- bonobo/execution/contexts/node.py | 2 ++ bonobo/nodes/io/csv.py | 4 ++-- bonobo/structs/graphs.py | 10 +--------- bonobo/util/collections.py | 14 ++++++++++++++ 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/bonobo/execution/contexts/node.py b/bonobo/execution/contexts/node.py index 2a4294c..814f8e6 100644 --- a/bonobo/execution/contexts/node.py +++ b/bonobo/execution/contexts/node.py @@ -342,6 +342,8 @@ class NodeExecutionContext(BaseContext, WithStatistics): # TODO: parse flags to check constraints are respected (like not modified alone, etc.) if F_NOT_MODIFIED in _flags: + if self._output_type: + return ensure_tuple(_input, cls=self._output_type) return _input if F_INHERIT in _flags: diff --git a/bonobo/nodes/io/csv.py b/bonobo/nodes/io/csv.py index 6fd0c5e..00cc126 100644 --- a/bonobo/nodes/io/csv.py +++ b/bonobo/nodes/io/csv.py @@ -6,7 +6,7 @@ from bonobo.constants import NOT_MODIFIED from bonobo.nodes.io.base import FileHandler from bonobo.nodes.io.file import FileReader, FileWriter from bonobo.util import ensure_tuple -from bonobo.util.collections import tuple_or_const +from bonobo.util.collections import coalesce, tuple_or_const class CsvHandler(FileHandler): @@ -105,7 +105,7 @@ class CsvWriter(FileWriter, CsvHandler): def write(self, file, context, *values, fs): context.setdefault("lineno", 0) - fields = context.get_input_fields() if self.fields is None else self.fields + fields = coalesce(self.fields, context.get_input_fields()) if not context.lineno: context.writer = self.writer_factory(file) diff --git a/bonobo/structs/graphs.py b/bonobo/structs/graphs.py index ea1ccf9..103ee06 100644 --- a/bonobo/structs/graphs.py +++ b/bonobo/structs/graphs.py @@ -8,19 +8,11 @@ from graphviz.dot import Digraph from bonobo.constants import BEGIN from bonobo.util import get_name +from bonobo.util.collections import coalesce GraphRange = namedtuple("GraphRange", ["graph", "input", "output"]) -def coalesce(*values): - if not len(values): - raise ValueError("Cannot coalesce an empty list of arguments.") - for value in values: - if value is not None: - return value - return values[-1] - - class GraphCursor: @property def input(self): diff --git a/bonobo/util/collections.py b/bonobo/util/collections.py index bd00954..e573f26 100644 --- a/bonobo/util/collections.py +++ b/bonobo/util/collections.py @@ -105,3 +105,17 @@ iterator-returning function to force return value to be a tuple. >>> my_generator() (1, 2, 3) """ + + +def coalesce(*values): + """ + Returns the first argument which is not None, or None if all arguments are None. + + """ + + if not len(values): + raise ValueError("Cannot coalesce an empty list of arguments.") + for value in values: + if value is not None: + return value + return None