{"record":{"id":"f4c1967f6573eca9","repo":"donnemartin/interactive-coding-challenges","slug":"key-cannot-be-none-f4c196","errorCode":null,"errorMessage":"key cannot be None","messagePattern":"key cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"graphs_trees/min_heap/min_heap_solution.ipynb","lineNumber":238,"sourceCode":"    \"        return len(self.array)\\n\",\n    \"\\n\",\n    \"    def extract_min(self):\\n\",\n    \"        if not self.array:\\n\",\n    \"            return None\\n\",\n    \"        if len(self.array) == 1:\\n\",\n    \"            return self.array.pop(0)\\n\",\n    \"        minimum = self.array[0]\\n\",\n    \"        # Move the last element to the root\\n\",\n    \"        self.array[0] = self.array.pop(-1)\\n\",\n    \"        self._bubble_down(index=0)\\n\",\n    \"        return minimum\\n\",\n    \"\\n\",\n    \"    def peek_min(self):\\n\",\n    \"        return self.array[0] if self.array else None\\n\",\n    \"\\n\",\n    \"    def insert(self, key):\\n\",\n    \"        if key is None:\\n\",\n    \"            raise TypeError('key cannot be None')\\n\",\n    \"        self.array.append(key)\\n\",\n    \"        self._bubble_up(index=len(self.array) - 1)\\n\",\n    \"\\n\",\n    \"    def _bubble_up(self, index):\\n\",\n    \"        if index == 0:\\n\",\n    \"            return\\n\",\n    \"        index_parent = (index - 1) // 2\\n\",\n    \"        if self.array[index] < self.array[index_parent]:\\n\",\n    \"            # Swap the indices and recurse\\n\",\n    \"            self.array[index], self.array[index_parent] = \\\\\\n\",\n    \"                self.array[index_parent], self.array[index]\\n\",\n    \"            self._bubble_up(index_parent)\\n\",\n    \"\\n\",\n    \"    def _bubble_down(self, index):\\n\",\n    \"        min_child_index = self._find_smaller_child(index)\\n\",\n    \"        if min_child_index == -1:\\n\",\n    \"            return\\n\",\n    \"        if self.array[index] > self.array[min_child_index]:\\n\",","sourceCodeStart":220,"sourceCodeEnd":256,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/graphs_trees/min_heap/min_heap_solution.ipynb#L220-L256","documentation":"MinHeap.insert raises TypeError('key cannot be None') because None cannot be meaningfully ordered against other heap elements during _bubble_up comparisons. The guard clause rejects None keys up front rather than failing later with a cryptic comparison error.","triggerScenarios":"Calling heap.insert(None), or passing a value that resolves to None such as a function default, a dict .get() miss, or a parsed field that is absent.","commonSituations":"Feeding the heap from external data (JSON, CSV, DB rows) where a field is missing and surfaces as None; using None as a placeholder for 'no priority' instead of +inf/-inf.","solutions":["Filter or replace None values before inserting (skip them, or use float('inf') as a sentinel)","Audit the data source producing the None key","Catch TypeError if None input is expected and should be skipped"],"exampleFix":"// before\nheap.insert(data.get('priority'))  # TypeError when missing\n// after\nvalue = data.get('priority')\nif value is not None:\n    heap.insert(value)","handlingStrategy":"validation","validationCode":"if key is None:\n    raise ValueError('priority missing')\nheap.insert(key)","typeGuard":"def is_insertable(key) -> bool:\n    return key is not None","tryCatchPattern":"try:\n    heap.insert(key)\nexcept TypeError:\n    pass  # skip None keys","preventionTips":["Sanitize records from external sources before heap insertion","Use float('inf') sentinels instead of None for 'lowest priority'"],"tags":["heap","none-guard","python"],"backgroundTag":"none-argument-guard","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}