donnemartin/interactive-coding-challenges · error · TypeError

data cannot be None

Error message

data cannot be None

What it means

Raised by Solution.find_busiest_period when data is None. The method sorts and iterates the list of Interval records; None would fail at data.sort(), so it rejects None explicitly. (An empty list is legal and returns None.)

Source

Thrown at online_judges/busiest_period/busiest_period_solution.ipynb:168

    "\n",
    "\n",
    "class EventType(Enum):\n",
    "\n",
    "    ENTER = 0\n",
    "    EXIT = 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "class Solution(object):\n",
    "\n",
    "    def find_busiest_period(self, data):\n",
    "        if data is None:\n",
    "            raise TypeError('data cannot be None')\n",
    "        if not data:\n",
    "            return None\n",
    "        data.sort()\n",
    "        max_period = Period(0, 0)\n",
    "        max_people = 0\n",
    "        curr_people = 0\n",
    "        for index, interval in enumerate(data):\n",
    "            if interval.event_type == EventType.ENTER:\n",
    "                curr_people += interval.num_people\n",
    "            elif interval.event_type == EventType.EXIT:\n",
    "                curr_people -= interval.num_people\n",
    "            else:\n",
    "                raise ValueError('Invalid event type')\n",
    "            if (index < len(data) - 1 and \n",
    "                    data[index].timestamp == data[index + 1].timestamp):\n",
    "                continue\n",
    "            if curr_people > max_people:\n",
    "                max_people = curr_people\n",

View on GitHub (pinned to 358f2cc604)

Solutions

  1. Pass a list of Interval records
  2. Fix the loader to return [] instead of None when there is no data
  3. Guard: if data is not None: solution.find_busiest_period(data)

Example fix

# before
records = load_events(path)  # returns None on missing file
solution.find_busiest_period(records)

# after
records = load_events(path) or []
solution.find_busiest_period(records)
Defensive patterns

Strategy: validation

Validate before calling

if data is None: return None
solution.find_busiest_period(data)

Type guard

def is_interval_list(x): return isinstance(x, list) and all(hasattr(i, 'timestamp') for i in x)

Try / catch

try:
    period = solution.find_busiest_period(data)
except TypeError as e:
    period = None
    logger.warning(e)

Prevention

When it happens

Trigger: Calling find_busiest_period(None) — e.g. a loader returned None on missing data, or a variable was initialized to None and never assigned before the call.

Common situations: Data pipelines where the events list comes from an optional source (empty file, failed fetch); tests covering the guard.

Related errors


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