Skip to content

Commit d89d660

Browse files
ValueArray.__init__ should raise error when it can't parse unit (#55)
2 parents 5c99679 + 2447965 commit d89d660

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

test/test_value_array.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,12 @@ def test_pick_roundtrip() -> None:
254254
x = value * unit
255255
s = pickle.dumps(x)
256256
assert all(x == pickle.loads(s))
257+
258+
259+
class _InvalidUnit:
260+
pass
261+
262+
263+
def test_invalid_unit_raises_error() -> None:
264+
with pytest.raises(ValueError):
265+
_ = tu.ValueArray([1, 2], _InvalidUnit())

tunits/core/cython/with_unit_value_array.pyx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ class ValueArray(WithUnit):
4646
the items.
4747
"""
4848
if unit is not None:
49-
unit = _try_interpret_as_with_unit(unit)
49+
parsed_unit = _try_interpret_as_with_unit(unit)
50+
if parsed_unit is None:
51+
raise ValueError("Bad WithUnit scaling value: " + repr(unit))
52+
unit = parsed_unit
5053

5154
# If the items have units, we're supposed to extract a shared unit.
5255
data = np.asarray(data)

0 commit comments

Comments
 (0)