{"record":{"id":"b158f5a8ded1bd96","repo":"donnemartin/interactive-coding-challenges","slug":"invalid-key-b158f5","errorCode":null,"errorMessage":"Invalid key","messagePattern":"Invalid key","errorType":"exception","errorClass":"KeyError","httpStatus":null,"severity":"error","filePath":"graphs_trees/graph/graph_solution.ipynb","lineNumber":225,"sourceCode":"    \"        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)\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 2,\n   \"metadata\": {},\n   \"outputs\": [],","sourceCodeStart":207,"sourceCodeEnd":243,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/graphs_trees/graph/graph_solution.ipynb#L207-L243","documentation":"Raised by Graph.add_edge(source_key, dest_key, weight) when source_key or dest_key is None. The method auto-creates missing nodes, but None is not an acceptable key, so it raises KeyError('Invalid key') rather than silently creating a None-keyed node. Note this fires before the None keys can flow into add_node.","triggerScenarios":"Calling add_edge(None, 'b'), add_edge('a', None), or add_edge(x, y) where x/y came from a failed lookup like graph.nodes.get(missing) or a nullable data field.","commonSituations":"Building graphs from edge lists with null endpoint columns; passing variables that were never initialized because an earlier step failed silently; confusing None with the default weight parameter positionally (add_edge('a', None)).","solutions":["Validate both endpoint keys are non-None before calling add_edge","Fix upstream data: fill or drop edges with null endpoints at load time","If None means 'not found', use graph.nodes.get() and skip edge creation when it returns None"],"exampleFix":"# before\ngraph.add_edge(row['src'], row['dst'])  # KeyError when a column is null\n\n# after\nif row['src'] is not None and row['dst'] is not None:\n    graph.add_edge(row['src'], row['dst'])","handlingStrategy":"validation","validationCode":"if source_key is not None and dest_key is not None:\n    graph.add_edge(source_key, dest_key, weight)","typeGuard":"def is_valid_edge(src, dst):\n    return src is not None and dst is not None","tryCatchPattern":"try:\n    graph.add_edge(src, dst, weight)\nexcept KeyError as e:\n    if 'Invalid key' in str(e):\n        log_bad_edge(src, dst)\n    else:\n        raise","preventionTips":["Filter edges with null endpoints at load time","Pass weight as a keyword argument to avoid positional mix-ups"],"tags":["python","graph","keyerror","edge-validation"],"backgroundTag":"none-argument-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}