donnemartin/interactive-coding-challenges · error · TypeError

source or dest cannot be None

Error message

source or dest cannot be None

What it means

Raised by Array.merge_into when source or dest is None. The method walks both arrays backwards from the given end indices, so both must be real lists; None is rejected first with a TypeError, before the index validation on the following line.

Source

Thrown at sorting_searching/merge_into/merge_into_solution.ipynb:159

  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Array(object):\n",
    "\n",
    "    def merge_into(self, source, dest, source_end_index, dest_end_index):\n",
    "        if source is None or dest is None:\n",
    "            raise TypeError('source or dest cannot be None')\n",
    "        if source_end_index < 0 or dest_end_index < 0:\n",
    "            raise ValueError('end indices must be >= 0')\n",
    "        if not source:\n",
    "            return dest\n",
    "        if not dest:\n",
    "            return source\n",
    "        source_index = source_end_index - 1\n",
    "        dest_index = dest_end_index - 1\n",
    "        insert_index = source_end_index + dest_end_index - 1\n",
    "        while dest_index >= 0:\n",
    "            if source[source_index] > dest[dest_index]:\n",
    "                source[insert_index] = source[source_index]\n",
    "                source_index -= 1\n",
    "            else:\n",
    "                source[insert_index] = dest[dest_index]\n",
    "                dest_index -= 1\n",
    "            insert_index -= 1\n",
    "        return source"

View on GitHub (pinned to 358f2cc604)

Solutions

  1. Initialize both buffers as lists before calling (e.g. dest = [None] * capacity)
  2. Guard the call: proceed only when both source and dest are non-None
  3. Ensure upstream allocation code cannot return None

Example fix

// before
arr.merge_into(src, None, len(src), total)
// after
dest = [None] * (len(src) + extra)
arr.merge_into(src, dest, len(src), len(dest))
Defensive patterns

Strategy: type-guard

Validate before calling

if source is None or dest is None:
    raise ValueError('source and dest buffers required')
arr.merge_into(source, dest, len(source), len(dest))

Type guard

def valid_buffers(a, b):
    return isinstance(a, list) and isinstance(b, list)

Try / catch

try:
    arr.merge_into(src, dest, i, j)
except TypeError as e:
    if 'None' in str(e):
        dest = [None] * capacity
        arr.merge_into(src, dest, i, capacity)

Prevention

When it happens

Trigger: Calling merge_into(None, dest, i, j) or merge_into(source, None, i, j). Empty source or dest are handled by early returns; only None raises.

Common situations: Merging buffers where one array failed to allocate or was never populated; merging results from optional data sources; refactoring that leaves a buffer uninitialized.

Related errors


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