|
| 1 | +"""Demonstrates which types of objects can be used as dictionary keys in Python.""" |
| 2 | + |
| 3 | +# mypy: ignore-errors |
| 4 | +# ruff: noqa |
| 5 | + |
| 6 | +import unittest |
| 7 | + |
| 8 | + |
| 9 | +class TestDictKey(unittest.TestCase): |
| 10 | + """Test cases demonstrating valid and invalid dictionary keys in Python.""" |
| 11 | + |
| 12 | + def test_immutable_keys(self): |
| 13 | + """Test basic immutable types as dictionary keys. |
| 14 | +
|
| 15 | + Demonstrates that basic immutable types can be used as dictionary keys: |
| 16 | + - strings |
| 17 | + - integers |
| 18 | + - floats |
| 19 | + These types are hashable and their values cannot be changed after creation. |
| 20 | + """ |
| 21 | + test_dict = {"string_key": "value", 42: "value", 3.14: "value"} |
| 22 | + self.assertIn("string_key", test_dict) |
| 23 | + self.assertIn(42, test_dict) |
| 24 | + self.assertIn(3.14, test_dict) |
| 25 | + |
| 26 | + def test_tuple_keys(self): |
| 27 | + """Test tuples as dictionary keys. |
| 28 | +
|
| 29 | + Demonstrates that tuples can be used as dictionary keys because: |
| 30 | + - Tuples are immutable |
| 31 | + - If all elements in the tuple are hashable, the tuple itself is hashable |
| 32 | + - Nested tuples are also valid as long as all nested elements are hashable |
| 33 | + """ |
| 34 | + test_dict = {(1, 2): "value", (1, "two", 3.0): "another value"} |
| 35 | + self.assertIn((1, 2), test_dict) |
| 36 | + self.assertIn((1, "two", 3.0), test_dict) |
| 37 | + |
| 38 | + def test_mutable_keys_fail(self): |
| 39 | + """Test that mutable types cannot be used as dictionary keys. |
| 40 | +
|
| 41 | + Demonstrates that mutable types raise TypeError when used as keys: |
| 42 | + - Lists: mutable sequences |
| 43 | + - Dictionaries: mutable mappings |
| 44 | + - Sets: mutable collections |
| 45 | +
|
| 46 | + This is because mutable objects can change after being used as keys, |
| 47 | + which would break the dictionary's ability to look up values. |
| 48 | + """ |
| 49 | + with self.assertRaises(TypeError): |
| 50 | + test_dict = {[1, 2, 3]: "value"} |
| 51 | + |
| 52 | + with self.assertRaises(TypeError): |
| 53 | + test_dict = {{"key": "value"}: "value"} |
| 54 | + |
| 55 | + with self.assertRaises(TypeError): |
| 56 | + test_dict = {set([1, 2, 3]): "value"} |
| 57 | + |
| 58 | + def test_custom_object_keys(self): |
| 59 | + """Test custom objects as dictionary keys. |
| 60 | +
|
| 61 | + Demonstrates that custom objects can be used as dictionary keys if they: |
| 62 | + 1. Implement __hash__ method: defines how the object should be hashed |
| 63 | + 2. Implement __eq__ method: defines how objects should be compared |
| 64 | +
|
| 65 | + These methods must ensure that: |
| 66 | + - Objects that are equal have the same hash value |
| 67 | + - Hash value remains constant during the object's lifetime |
| 68 | + """ |
| 69 | + |
| 70 | + class CustomKey: |
| 71 | + def __init__(self, value): |
| 72 | + self.value = value |
| 73 | + |
| 74 | + def __hash__(self): |
| 75 | + return hash(self.value) |
| 76 | + |
| 77 | + def __eq__(self, other): |
| 78 | + return isinstance(other, CustomKey) and self.value == other.value |
| 79 | + |
| 80 | + obj1 = CustomKey(1) |
| 81 | + obj2 = CustomKey(2) |
| 82 | + test_dict = {obj1: "Object 1", obj2: "Object 2"} |
| 83 | + |
| 84 | + self.assertIn(obj1, test_dict) |
| 85 | + self.assertIn(obj2, test_dict) |
| 86 | + |
| 87 | + def test_hashability(self): |
| 88 | + """Test hashability of different objects. |
| 89 | +
|
| 90 | + Demonstrates the fundamental requirement for dictionary keys: |
| 91 | + - Only hashable objects can be dictionary keys |
| 92 | + - Hashable means the object has a hash value that never changes |
| 93 | +
|
| 94 | + Built-in immutable types are hashable: |
| 95 | + - strings, numbers, tuples |
| 96 | +
|
| 97 | + Built-in mutable types are not hashable: |
| 98 | + - lists, sets, dictionaries |
| 99 | + """ |
| 100 | + self.assertTrue(hash("test")) |
| 101 | + self.assertTrue(hash((1, 2, 3))) |
| 102 | + |
| 103 | + with self.assertRaises(TypeError): |
| 104 | + hash([1, 2, 3]) |
| 105 | + |
| 106 | + with self.assertRaises(TypeError): |
| 107 | + hash(set([1, 2, 3])) |
0 commit comments