{"record":{"id":"fc9bc1eb7ca5d0b9","repo":"donnemartin/interactive-coding-challenges","slug":"key-cannot-be-none-fc9bc1","errorCode":null,"errorMessage":"key cannot be None","messagePattern":"key cannot be None","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"graphs_trees/min_heap/min_heap.py","lineNumber":30,"sourceCode":"        return len(self.array)\n\n    def extract_min(self):\n        if not self.array:\n            return None\n        if len(self.array) == 1:\n            return self.array.pop(0)\n        minimum = self.array[0]\n        # Move the last element to the root\n        self.array[0] = self.array.pop(-1)\n        self._bubble_down(index=0)\n        return minimum\n\n    def peek_min(self):\n        return self.array[0] if self.array else None\n\n    def insert(self, key):\n        if key is None:\n            raise TypeError('key cannot be None')\n        self.array.append(key)\n        self._bubble_up(index=len(self.array) - 1)\n\n    def _bubble_up(self, index):\n        if index == 0:\n            return\n        index_parent = (index - 1) // 2\n        if self.array[index] < self.array[index_parent]:\n            # Swap the indices and recurse\n            self.array[index], self.array[index_parent] = \\\n                self.array[index_parent], self.array[index]\n            self._bubble_up(index_parent)\n\n    def _bubble_down(self, index):\n        min_child_index = self._find_smaller_child(index)\n        if min_child_index == -1:\n            return\n        if self.array[index] > self.array[min_child_index]:","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/graphs_trees/min_heap/min_heap.py#L12-L48","documentation":"Raised by MinHeap.insert when key is None. Heap ordering comparisons (via _bubble_up) would raise TypeError against None anyway; this guard gives a clearer, earlier error. None keys are fundamentally unorderable in a min-heap.","triggerScenarios":"Calling heap.insert(None); inserting values from a stream that contains None (e.g. API responses with optional fields).","commonSituations":"Priority queues fed by data with missing priorities; mixing Optional values into heap items after a refactor.","solutions":["Filter None keys before insertion or substitute a large sentinel priority","Fix the producer so priorities are always set (validate at ingest)","If None means 'lowest priority', map it explicitly (e.g. float('inf'))"],"exampleFix":"# before\nheap.insert(item.get('priority'))\n\n# after\npriority = item.get('priority')\nheap.insert(priority if priority is not None else float('inf'))","handlingStrategy":"validation","validationCode":"if key is not None:\n    heap.insert(key)","typeGuard":"def is_heap_key(value) -> bool:\n    return value is not None and isinstance(value, (int, float))","tryCatchPattern":"try:\n    heap.insert(key)\nexcept TypeError:\n    heap.insert(float('inf'))  # or skip","preventionTips":["Map missing priorities to an explicit low-priority sentinel","Validate priorities at ingest time"],"tags":["min-heap","priority-queue","none-check","typeerror"],"backgroundTag":"none-argument-rejected","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}