Skip to content

Commit 8d0274a

Browse files
mrshannonbjlittle
authored andcommitted
Deligate equality to other if conversion to Unit fails. (#150)
* Punt equality to other if conversion to Unit fails. * Add unit tests for not implemented equality.
1 parent 898d2c7 commit 8d0274a

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

cf_units/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1731,7 +1731,10 @@ def __eq__(self, other):
17311731
True
17321732
17331733
"""
1734-
other = as_unit(other)
1734+
try:
1735+
other = as_unit(other)
1736+
except ValueError:
1737+
return NotImplemented
17351738

17361739
# Compare category (i.e. unknown, no_unit, etc.).
17371740
if self.category != other.category:

cf_units/tests/test_unit.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -667,6 +667,10 @@ def test_unknown_no_unit(self):
667667
v = Unit('no_unit')
668668
self.assertNotEqual(u, v)
669669

670+
def test_not_implemented(self):
671+
u = Unit('meter')
672+
self.assertFalse(u == {})
673+
670674

671675
class Test_non_equality(unittest.TestCase):
672676

@@ -695,6 +699,10 @@ def test_no_unit(self):
695699
u = Unit('no_unit')
696700
self.assertFalse(u != 'no_unit')
697701

702+
def test_not_implemented(self):
703+
u = Unit('meter')
704+
self.assertNotEqual(u, {})
705+
698706

699707
class Test_convert(unittest.TestCase):
700708

0 commit comments

Comments
 (0)