Skip to content

Commit

Permalink
Deligate equality to other if conversion to Unit fails. (#150)
Browse files Browse the repository at this point in the history
* Punt equality to other if conversion to Unit fails.

* Add unit tests for not implemented equality.
  • Loading branch information
mrshannon authored and bjlittle committed Jul 24, 2019
1 parent 898d2c7 commit 8d0274a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
5 changes: 4 additions & 1 deletion cf_units/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1731,7 +1731,10 @@ def __eq__(self, other):
True
"""
other = as_unit(other)
try:
other = as_unit(other)
except ValueError:
return NotImplemented

# Compare category (i.e. unknown, no_unit, etc.).
if self.category != other.category:
Expand Down
8 changes: 8 additions & 0 deletions cf_units/tests/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,10 @@ def test_unknown_no_unit(self):
v = Unit('no_unit')
self.assertNotEqual(u, v)

def test_not_implemented(self):
u = Unit('meter')
self.assertFalse(u == {})


class Test_non_equality(unittest.TestCase):

Expand Down Expand Up @@ -695,6 +699,10 @@ def test_no_unit(self):
u = Unit('no_unit')
self.assertFalse(u != 'no_unit')

def test_not_implemented(self):
u = Unit('meter')
self.assertNotEqual(u, {})


class Test_convert(unittest.TestCase):

Expand Down

0 comments on commit 8d0274a

Please sign in to comment.