Fixed #28629 -- Made tree.Node instances hashable.

Regression in 508b5debfb which
added Node.__eq__().
This commit is contained in:
Mariusz Felisiak 2017-09-28 18:07:19 +02:00 committed by Tim Graham
parent 44f08422c8
commit fc6528b25a
2 changed files with 13 additions and 0 deletions

View File

@ -70,6 +70,9 @@ class Node:
self.children == other.children
)
def __hash__(self):
return hash((self.__class__, self.connector, self.negated) + tuple(self.children))
def add(self, data, conn_type, squash=True):
"""
Combine this tree and the data represented by data using the

View File

@ -19,6 +19,16 @@ class NodeTests(unittest.TestCase):
"<Node: (DEFAULT: ('a', 1), ('b', 2))>")
self.assertEqual(repr(self.node2), "<Node: (DEFAULT: )>")
def test_hash(self):
node3 = Node(self.node1_children, negated=True)
node4 = Node(self.node1_children, connector='OTHER')
node5 = Node(self.node1_children)
self.assertNotEqual(hash(self.node1), hash(self.node2))
self.assertNotEqual(hash(self.node1), hash(node3))
self.assertNotEqual(hash(self.node1), hash(node4))
self.assertEqual(hash(self.node1), hash(node5))
self.assertEqual(hash(self.node2), hash(Node()))
def test_len(self):
self.assertEqual(len(self.node1), 2)
self.assertEqual(len(self.node2), 0)