(core) Don't swallow TypeErrors in functions like SUM

Summary: Math functions like SUM which call `_chain` were catching `TypeError`s raised by the iterable arguments themselves, e.g. `SUM(r.A / r.B for r in $group)` where `r.A / r.B` raises a `TypeError` would silently return wrong results. This diff narrows the `try/catch` to only check whether the argument is iterable as intended, but not catch errors from the process of iterating.

Test Plan: Added Python unit test.

Reviewers: jarek

Reviewed By: jarek

Differential Revision: https://phab.getgrist.com/D3679
pull/282/head
Alex Hall 2 years ago
parent caef8bae22
commit aa88c156e6

@ -21,10 +21,12 @@ import roman
def _chain(*values_or_iterables):
for v in values_or_iterables:
try:
for x in v:
yield x
v = iter(v)
except TypeError:
yield v
else:
for x in v:
yield x
# Iterates through iterable or other arguments, skipping non-numeric ones.

@ -80,3 +80,9 @@ class TestUuid(unittest.TestCase):
self.check_uuids(1) # because of the `random.seed(0)` in `check_uuids()`
finally:
uuid.uuid4 = v4
class TestChain(unittest.TestCase):
def test_chain_type_error(self):
with self.assertRaises(TypeError):
functions.SUM(x / "2" for x in [1, 2, 3])

Loading…
Cancel
Save