Bugfix: if you define a custom __eq__, you must define a __hash__ too
Also, on Python 3 the stock object.__hash__ raises an error ("unhashable type"), and we have code that uses Host instances as dict keys.pull/4420/head
parent
a2bc6b4b26
commit
0624797375
|
@ -41,6 +41,9 @@ class Host:
|
||||||
def __ne__(self, other):
|
def __ne__(self, other):
|
||||||
return not self.__eq__(other)
|
return not self.__eq__(other)
|
||||||
|
|
||||||
|
def __hash__(self):
|
||||||
|
return hash(self.name)
|
||||||
|
|
||||||
def serialize(self):
|
def serialize(self):
|
||||||
groups = []
|
groups = []
|
||||||
for group in self.groups:
|
for group in self.groups:
|
||||||
|
|
|
@ -33,3 +33,6 @@ class TestHost(unittest.TestCase):
|
||||||
# __ne__ is a separate method
|
# __ne__ is a separate method
|
||||||
self.assertFalse(self.hostA != Host('a'))
|
self.assertFalse(self.hostA != Host('a'))
|
||||||
|
|
||||||
|
def test_hashability(self):
|
||||||
|
# equality implies the hash values are the same
|
||||||
|
self.assertEqual(hash(self.hostA), hash(Host('a')))
|
||||||
|
|
Loading…
Reference in New Issue