donnemartin/interactive-coding-challenges · error · TypeError
data cannot be None
Error message
data cannot be None
What it means
Bst.insert raises TypeError('data cannot be None') when inserting a None value. A BST needs comparable keys to route left/right during insertion, and None cannot be compared with <, so it is rejected before tree traversal.
Source
Thrown at graphs_trees/bst/bst_solution.ipynb:133
"\n",
" def __init__(self, data):\n",
" self.data = data\n",
" self.left = None\n",
" self.right = None\n",
" self.parent = None\n",
"\n",
" def __repr__(self):\n",
" return str(self.data)\n",
"\n",
"\n",
"class Bst(object):\n",
"\n",
" def __init__(self, root=None):\n",
" self.root = root\n",
"\n",
" def insert(self, data):\n",
" if data is None:\n",
" raise TypeError('data cannot be None')\n",
" if self.root is None:\n",
" self.root = Node(data)\n",
" return self.root\n",
" else:\n",
" return self._insert(self.root, data)\n",
"\n",
" def _insert(self, node, data):\n",
" if node is None:\n",
" return Node(data)\n",
" if data <= node.data:\n",
" if node.left is None:\n",
" node.left = self._insert(node.left, data)\n",
" node.left.parent = node\n",
" return node.left\n",
" else:\n",
" return self._insert(node.left, data)\n",
" else:\n",
" if node.right is None:\n",View on GitHub (pinned to 358f2cc604)
Solutions
- Filter out None values before inserting: for x in items: if x is not None: bst.insert(x)
- Fix the data source to supply a default (e.g. coalesce to a sentinel value)
- Validate records upstream and log/reject rows missing the key field
Example fix
# before
for value in values:
bst.insert(value) # values may contain None
# after
for value in values:
if value is None:
continue
bst.insert(value) Defensive patterns
Strategy: validation
Validate before calling
for value in values:
if value is None:
continue # or raise, per policy
bst.insert(value) Type guard
def is_insertable(v) -> bool:
return v is not None Try / catch
try:
bst.insert(data)
except TypeError as e:
if 'cannot be None' in str(e):
logger.warning('skipping null record')
else:
raise Prevention
- Filter None out of lists before bulk insert
- Make key columns non-nullable at ingestion
- Wrap inserts in a helper that enforces a not-None policy
When it happens
Trigger: bst.insert(None), inserting values read from a stream/file where a record is missing a field, or a loop over a list that contains None entries.
Common situations: ETL/ingest pipelines inserting rows with nullable columns; lists containing None from map/filter chains; API payloads with absent optional fields.
Related errors
- root cannot be None
- number cannot be None
- a or b cannot be None
- Invalid argument: None
- num cannot be None
AI-assisted analysis of donnemartin/interactive-coding-challenges@358f2cc604 (2026-08-28).
Data as JSON: /api/errors/db8fa0dd51edfc3c.
Report an issue: GitHub.