donnemartin/interactive-coding-challenges · error · TypeError

data cannot be None

Error message

data cannot be None

What it means

Raised by Bst.insert when data is None. The BST uses None internally to mark an empty tree (self.root is None), so a None data value would corrupt tree invariants. It indicates a caller passed an unset or missing value.

Source

Thrown at graphs_trees/bst/bst.py:20

    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
        self.parent = None

    def __repr__(self):
        return str(self.data)


class Bst(object):

    def __init__(self, root=None):
        self.root = root

    def insert(self, data):
        if data is None:
            raise TypeError('data cannot be None')
        if self.root is None:
            self.root = Node(data)
            return self.root
        else:
            return self._insert(self.root, data)

    def _insert(self, node, data):
        if node is None:
            return Node(data)
        if data <= node.data:
            if node.left is None:
                node.left = self._insert(node.left, data)
                node.left.parent = node
                return node.left
            else:
                return self._insert(node.left, data)
        else:
            if node.right is None:

View on GitHub (pinned to 358f2cc604)

Solutions

  1. Filter out None values before calling insert
  2. Find where the None originates (unset variable, dict.get default) and supply a real value
  3. Represent 'missing' with a wrapper or sentinel object instead of None if needed

Example fix

# before
bst.insert(data.get('value'))  # may be None

# after
value = data.get('value')
if value is not None:
    bst.insert(value)
Defensive patterns

Strategy: validation

Validate before calling

if data is None:
    raise ValueError('missing data, skipping insert')
bst.insert(data)

Type guard

def is_insertable(value) -> bool:
    return value is not None

Try / catch

try:
    bst.insert(data)
except TypeError as e:
    if 'cannot be None' in str(e):
        # skip or log the bad record
        pass
    else:
        raise

Prevention

When it happens

Trigger: Calling bst.insert(None), or feeding insert from data that was not filtered for missing values (e.g. dict.get() returning None).

Common situations: Loading datasets with nulls from CSV/DB and inserting without cleaning; passing optional variables that were never set.

Related errors


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