mirror of
				https://github.com/django/django.git
				synced 2025-10-31 01:25:32 +00:00 
			
		
		
		
	[2.0.x] Fixed #28629 -- Made tree.Node instances hashable.
Regression in508b5debfbwhich added Node.__eq__(). Backport offc6528b25afrom master
This commit is contained in:
		
				
					committed by
					
						 Tim Graham
						Tim Graham
					
				
			
			
				
	
			
			
			
						parent
						
							aff2df2427
						
					
				
				
					commit
					8368d5a400
				
			| @@ -70,6 +70,9 @@ class Node: | |||||||
|             return self.children == other.children |             return self.children == other.children | ||||||
|         return False |         return False | ||||||
|  |  | ||||||
|  |     def __hash__(self): | ||||||
|  |         return hash((self.__class__, self.connector, self.negated) + tuple(self.children)) | ||||||
|  |  | ||||||
|     def add(self, data, conn_type, squash=True): |     def add(self, data, conn_type, squash=True): | ||||||
|         """ |         """ | ||||||
|         Combine this tree and the data represented by data using the |         Combine this tree and the data represented by data using the | ||||||
|   | |||||||
| @@ -19,6 +19,16 @@ class NodeTests(unittest.TestCase): | |||||||
|                          "<Node: (DEFAULT: ('a', 1), ('b', 2))>") |                          "<Node: (DEFAULT: ('a', 1), ('b', 2))>") | ||||||
|         self.assertEqual(repr(self.node2), "<Node: (DEFAULT: )>") |         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): |     def test_len(self): | ||||||
|         self.assertEqual(len(self.node1), 2) |         self.assertEqual(len(self.node1), 2) | ||||||
|         self.assertEqual(len(self.node2), 0) |         self.assertEqual(len(self.node2), 0) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user