From 47f28946f60b2295b329e5c7bd2bdd92cbf554be Mon Sep 17 00:00:00 2001 From: Christopher Haster Date: Thu, 14 Nov 2024 00:09:20 -0600 Subject: [PATCH] scripts: csv.py: Enforced matching types in ternary branches So in: $ ./scripts/csv.py input.csv -fa='b?c:d' c and d must have matching types or else an error is raised. This requires an explicit definition for the ternary operator since it's a special case in that the type of b does not matter. Compare to a 3-arg max call: $ ./scripts/csv.py input.csv -fa='int(b)?float(c):float(d)' # ok $ ./scripts/csv.py input.csv -fa='max(int(b),float(c),float(d))' # error --- scripts/csv.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/scripts/csv.py b/scripts/csv.py index 455cc945..4e5e4724 100755 --- a/scripts/csv.py +++ b/scripts/csv.py @@ -989,7 +989,11 @@ class RExpr: class IfElse(Expr): """b if a is non-zero, otherwise c""" def type(self, types={}): - return self.b.type(types) + t = self.b.type(types) + u = self.c.type(types) + if t != u: + raise RExpr.Error("mismatched types? %r" % self) + return t def fold(self, types={}): return self.b.fold(types)