donnemartin/interactive-coding-challenges · error · TypeError

file_system cannot be None

Error message

file_system cannot be None

What it means

Raised by Solution.length_longest_path when file_system is None. The method calls file_system.splitlines() immediately, which would raise AttributeError on None; the guard requires a newline-separated path string up front.

Source

Thrown at online_judges/longest_abs_file_path/longest_path_solution.ipynb:138

  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Code"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution(object):\n",
    "\n",
    "    def length_longest_path(self, file_system):\n",
    "        if file_system is None:\n",
    "            raise TypeError('file_system cannot be None')\n",
    "        max_len = 0\n",
    "        path_len = {0: 0}\n",
    "        for line in file_system.splitlines():\n",
    "            name = line.lstrip('\\t')\n",
    "            depth = len(line) - len(name)\n",
    "            if '.' in name:\n",
    "                max_len = max(max_len, path_len[depth] + len(name))\n",
    "            else:\n",
    "                path_len[depth + 1] = path_len[depth] + len(name) + 1\n",
    "        return max_len"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Unit Test"
   ]

View on GitHub (pinned to 358f2cc604)

Solutions

  1. Pass the multiline path string, e.g. 'dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext'
  2. Guard: if file_system is None: handle missing input before the call
  3. Fix the reader to raise or default when the source is missing

Example fix

# before
fs = read_input(path)  # None if file missing
solution.length_longest_path(fs)

# after
fs = read_input(path)
if fs is None:
    raise FileNotFoundError(path)
solution.length_longest_path(fs)
Defensive patterns

Strategy: validation

Validate before calling

if file_system is None: raise ValueError('file_system is required')
solution.length_longest_path(file_system)

Type guard

def is_str(s): return isinstance(s, str)

Try / catch

try:
    n = solution.length_longest_path(fs)
except TypeError as e:
    n = 0
    logger.warning(e)

Prevention

When it happens

Trigger: Calling length_longest_path(None) — e.g. the input string was never read (file missing, cell not run) or an optional parameter was forwarded as None.

Common situations: Reading the input from a file or stdin that may be absent; notebook workflows where an earlier cell defines file_system; tests of the guard.

Related errors


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