Skip to content

Commit

Permalink
ValueArray.__init__ should raise error when it can't parse unit (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
NoureldinYosri authored Mar 7, 2025
2 parents 5c99679 + 2447965 commit d89d660
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
9 changes: 9 additions & 0 deletions test/test_value_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,12 @@ def test_pick_roundtrip() -> None:
x = value * unit
s = pickle.dumps(x)
assert all(x == pickle.loads(s))


class _InvalidUnit:
pass


def test_invalid_unit_raises_error() -> None:
with pytest.raises(ValueError):
_ = tu.ValueArray([1, 2], _InvalidUnit())
5 changes: 4 additions & 1 deletion tunits/core/cython/with_unit_value_array.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ class ValueArray(WithUnit):
the items.
"""
if unit is not None:
unit = _try_interpret_as_with_unit(unit)
parsed_unit = _try_interpret_as_with_unit(unit)
if parsed_unit is None:
raise ValueError("Bad WithUnit scaling value: " + repr(unit))
unit = parsed_unit

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

0 comments on commit d89d660

Please sign in to comment.