Skip to content

Commit 5b91fa2

Browse files
fix
1 parent b27cba3 commit 5b91fa2

3 files changed

Lines changed: 9 additions & 20 deletions

File tree

test/cython/test_with_unit.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import numpy as np
1919
import pytest
2020

21-
from tunits.core import raw_WithUnit, raw_UnitArray, WithUnit
21+
from tunits.core import raw_WithUnit, raw_UnitArray, WithUnit, NotTUnitsLikeError
2222

2323
from tunits import UnitMismatchError, ValueArray, Value
2424
from test.test_utils import frac, conv, val
@@ -85,7 +85,6 @@ def test_abs() -> None:
8585

8686
def test_equality() -> None:
8787
equivalence_groups: list[list[Any]] = [
88-
[""],
8988
["other types"],
9089
[list],
9190
[None],
@@ -488,21 +487,11 @@ def test_get_item() -> None:
488487
v = val(2, conv(numer=3, denom=5, exp10=7), mps, kph)
489488

490489
# Wrong kinds of index (unit array, slice).
491-
with pytest.raises(TypeError):
490+
with pytest.raises(UnitMismatchError):
492491
_ = u[mps]
493-
with pytest.raises(TypeError):
492+
with pytest.raises(NotTUnitsLikeError):
494493
_ = u[1:2]
495494

496-
# Safety against dimensionless unit ambiguity.
497-
_ = u[u]
498-
with pytest.raises(TypeError):
499-
_ = u[1.0]
500-
with pytest.raises(TypeError):
501-
_ = u[1.0]
502-
with pytest.raises(TypeError):
503-
_ = u[1]
504-
with pytest.raises(TypeError):
505-
_ = u[2 * v / v]
506495
assert u[v / v] == 10
507496

508497
# Wrong unit.

test/test_value.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,7 @@ def test_division() -> None:
177177
def test_get_item() -> None:
178178
from tunits.units import ns, s
179179

180-
with pytest.raises(TypeError):
181-
_ = (ns / s)[2 * s / ns]
182-
with pytest.raises(TypeError):
183-
_ = (ns / s)[Value(3, '')]
180+
assert (ns / s)[Value(3, '')] == pytest.approx(1 / 3 * 1e-9)
184181
assert Value(1, '')[Value(1, '')] == 1
185182
assert Value(1, '')[ns / s] == 10**9
186183

tunits/core/cython/with_unit.pyx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,9 +582,12 @@ cdef class WithUnit:
582582
except NotTUnitsLikeError:
583583
return NotImplemented
584584
except TypeError:
585-
unit_val = _try_interpret_as_with_unit(str(key), True)
585+
try:
586+
unit_val = _try_interpret_as_with_unit(str(key), True)
587+
except:
588+
raise NotTUnitsLikeError("Bad unit key: " + repr(key))
586589
if unit_val is None:
587-
return NotImplemented
590+
raise NotTUnitsLikeError("Bad unit key: " + repr(key))
588591
if self.base_units != unit_val.base_units:
589592
raise UnitMismatchError("'%s' doesn't match '%s'." %
590593
(self, key))

0 commit comments

Comments
 (0)