donnemartin/interactive-coding-challenges · error · TypeError

str input cannot be None

Error message

str input cannot be None

What it means

Raised by StringCompare.longest_common_subseq when str0 or str1 is None. The method sizes its DP table as len(str0)+1 by len(str1)+1 in the very next lines, so None strings are rejected explicitly with a clear TypeError instead of 'object of type NoneType has no len()'.

Source

Thrown at recursion_dynamic/longest_common_subsequence/longest_common_subseq_solution.ipynb:126

  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class StringCompare(object):\n",
    "\n",
    "    def longest_common_subseq(self, str0, str1):\n",
    "        if str0 is None or str1 is None:\n",
    "            raise TypeError('str input cannot be None')\n",
    "        # Add one to number of rows and cols for the dp table's\n",
    "        # first row of 0's and first col of 0's\n",
    "        num_rows = len(str0) + 1\n",
    "        num_cols = len(str1) + 1\n",
    "        T = [[None] * num_cols for _ in range(num_rows)]\n",
    "        for i in range(num_rows):\n",
    "            for j in range(num_cols):\n",
    "                if i == 0 or j == 0:\n",
    "                    T[i][j] = 0\n",
    "                elif str0[j - 1] != str1[i - 1]:\n",
    "                    T[i][j] = max(T[i][j - 1],\n",
    "                                  T[i - 1][j])\n",
    "                else:\n",
    "                    T[i][j] = T[i - 1][j - 1] + 1\n",
    "        results = ''\n",
    "        i = num_rows - 1\n",
    "        j = num_cols - 1\n",
    "        # Walk backwards to determine the subsequence\n",

View on GitHub (pinned to 358f2cc604)

Solutions

  1. Coalesce None to '' at the call site if an empty comparison is meaningful: (str0 or '')
  2. Validate that both operands are str before calling (isinstance checks)
  3. Fix upstream data loading so missing text fields become empty strings

Example fix

// before
lcs = sc.longest_common_subseq(record_a.bio, record_b.bio)
// after
lcs = sc.longest_common_subseq(record_a.bio or '', record_b.bio or '')
Defensive patterns

Strategy: type-guard

Validate before calling

str0 = str0 or ''
str1 = str1 or ''
sc.longest_common_subseq(str0, str1)

Type guard

def is_str_pair(a, b):
    return isinstance(a, str) and isinstance(b, str)

Try / catch

try:
    sc.longest_common_subseq(a, b)
except TypeError:
    lcs = ''

Prevention

When it happens

Trigger: Calling longest_common_subseq(None, 'abc') or longest_common_subseq('abc', None). Empty strings '' are valid (they produce a table of zeros) and do not raise.

Common situations: Comparing user-supplied or database fields where one record's column is NULL; diffing files where one side failed to read; optional function parameters defaulting to None.

Related errors


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