{"record":{"id":"702802100bdcb2b6","repo":"donnemartin/interactive-coding-challenges","slug":"key-cannot-be-none-702802","errorCode":null,"errorMessage":"key cannot be None","messagePattern":"key cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"graphs_trees/graph/graph_solution.ipynb","lineNumber":218,"sourceCode":"    \"\\n\",\n    \"    def remove_neighbor(self, neighbor):\\n\",\n    \"        if neighbor is None:\\n\",\n    \"            raise TypeError('neighbor cannot be None')\\n\",\n    \"        if neighbor.key not in self.adj_nodes:\\n\",\n    \"            raise KeyError('neighbor not found')\\n\",\n    \"        neighbor.incoming_edges -= 1\\n\",\n    \"        del self.adj_weights[neighbor.key]\\n\",\n    \"        del self.adj_nodes[neighbor.key]\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"class Graph:\\n\",\n    \"\\n\",\n    \"    def __init__(self):\\n\",\n    \"        self.nodes = {}  # Key = key, val = Node\\n\",\n    \"\\n\",\n    \"    def add_node(self, key):\\n\",\n    \"        if key is None:\\n\",\n    \"            raise TypeError('key cannot be None')\\n\",\n    \"        if key not in self.nodes:\\n\",\n    \"            self.nodes[key] = Node(key)\\n\",\n    \"        return self.nodes[key]\\n\",\n    \"\\n\",\n    \"    def add_edge(self, source_key, dest_key, weight=0):\\n\",\n    \"        if source_key is None or dest_key is None:\\n\",\n    \"            raise KeyError('Invalid key')\\n\",\n    \"        if source_key not in self.nodes:\\n\",\n    \"            self.add_node(source_key)\\n\",\n    \"        if dest_key not in self.nodes:\\n\",\n    \"            self.add_node(dest_key)\\n\",\n    \"        self.nodes[source_key].add_neighbor(self.nodes[dest_key], weight)\\n\",\n    \"\\n\",\n    \"    def add_undirected_edge(self, src_key, dst_key, weight=0):\\n\",\n    \"        if src_key is None or dst_key is None:\\n\",\n    \"            raise TypeError('key cannot be None')\\n\",\n    \"        self.add_edge(src_key, dst_key, weight)\\n\",\n    \"        self.add_edge(dst_key, src_key, weight)\"","sourceCodeStart":200,"sourceCodeEnd":236,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/graphs_trees/graph/graph_solution.ipynb#L200-L236","documentation":"Raised by Graph.add_node(key) when key is None. Nodes are stored in the self.nodes dict keyed by key, and None is treated as 'no key provided' rather than a hashable identifier. The guard uses TypeError because the argument type/value is fundamentally unusable as a node key.","triggerScenarios":"Calling add_node(None); passing an optional variable that was never assigned; Graph.add_edge auto-invoking add_node(source_key) or add_node(dest_key) when one of those keys is None.","commonSituations":"Loading vertices from sparse JSON/DB rows where a key field is null; using None as a sentinel for 'missing node' and then feeding it into the graph; dict.get(key) returning None and being forwarded as a node key.","solutions":["Default missing keys to a real value or skip the record before calling add_node","Use graph.nodes.get(key) lookups instead of None sentinels when threading values into add_node","Add an assertion/validation layer over raw input data that rejects null keys early"],"exampleFix":"# before\ngraph.add_node(some_dict.get('id'))  # None if missing -> TypeError\n\n# after\nkey = some_dict.get('id')\nif key is not None:\n    graph.add_node(key)","handlingStrategy":"validation","validationCode":"if key is not None:\n    graph.add_node(key)","typeGuard":"def is_valid_key(key):\n    return key is not None","tryCatchPattern":"try:\n    graph.add_node(key)\nexcept TypeError:\n    skip_record(key)  # or log and continue batch import","preventionTips":["Reject or default null key fields during data ingestion","Avoid dict.get(key) results as direct node keys without a None check"],"tags":["python","graph","none-check","node-key"],"backgroundTag":"none-argument-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}