donnemartin/interactive-coding-challenges · error · TypeError

key cannot be None

Error message

key cannot be None

What it means

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.

Source

Thrown at graphs_trees/min_heap/min_heap_solution.ipynb:238

    "        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]:\n",

View on GitHub (pinned to 358f2cc604)

Solutions

  1. Filter or replace None values before inserting (skip them, or use float('inf') as a sentinel)
  2. Audit the data source producing the None key
  3. Catch TypeError if None input is expected and should be skipped

Example fix

// before
heap.insert(data.get('priority'))  # TypeError when missing
// after
value = data.get('priority')
if value is not None:
    heap.insert(value)
Defensive patterns

Strategy: validation

Validate before calling

if key is None:
    raise ValueError('priority missing')
heap.insert(key)

Type guard

def is_insertable(key) -> bool:
    return key is not None

Try / catch

try:
    heap.insert(key)
except TypeError:
    pass  # skip None keys

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of donnemartin/interactive-coding-challenges@358f2cc604 (2026-08-28). Data as JSON: /api/errors/f4c1967f6573eca9. Report an issue: GitHub.