donnemartin/interactive-coding-challenges · error · TypeError

array cannot be None

Error message

array cannot be None

What it means

RadixSort.sort raises TypeError('array cannot be None') when the array argument is None. Note the distinction: None raises TypeError, while an empty list is valid and returns []. This mirrors Python's sorted() contract of rejecting None.

Source

Thrown at sorting_searching/radix_sort/radix_sort_solution.ipynb:125

  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class RadixSort(object):\n",
    "\n",
    "    def sort(self, array, base=10):\n",
    "        if array is None:\n",
    "            raise TypeError('array cannot be None')\n",
    "        if not array:\n",
    "            return []\n",
    "        max_element = max(array)\n",
    "        max_digits = len(str(abs(max_element)))\n",
    "        curr_array = array\n",
    "        for digit in range(max_digits):\n",
    "            buckets = [[] for _ in range(base)]\n",
    "            for item in curr_array:\n",
    "                buckets[(item//(base**digit))%base].append(item)\n",
    "            curr_array = []\n",
    "            for bucket in buckets:\n",
    "                curr_array.extend(bucket)\n",
    "        return curr_array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},

View on GitHub (pinned to 358f2cc604)

Solutions

  1. Ensure the input is a list; substitute [] for None if the empty case is intended
  2. Check where the array comes from and handle the None-producing failure there
  3. Add an assertion or early return in the caller for None inputs

Example fix

// before
result = RadixSort().sort(array)  # array may be None

// after
result = RadixSort().sort(array if array is not None else [])
Defensive patterns

Strategy: validation

Validate before calling

if array is None:
    array = []
result = RadixSort().sort(array)

Type guard

def is_sortable(x):
    return isinstance(x, list)

Try / catch

try:
    result = rs.sort(array)
except TypeError as e:
    if 'cannot be None' in str(e):
        result = []
    else:
        raise

Prevention

When it happens

Trigger: Calling RadixSort().sort(None); passing a variable that was never assigned or whose source (file parse, query result) returned None.

Common situations: Data ingestion steps that can return None on failure; refactors making the array parameter optional; interactive/notebook sessions with stale or unset variables.

Related errors


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