{"record":{"id":"7a234ed3850799dc","repo":"donnemartin/interactive-coding-challenges","slug":"neighbor-or-weight-cannot-be-none-7a234e","errorCode":null,"errorMessage":"neighbor or weight cannot be None","messagePattern":"neighbor or weight cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"graphs_trees/graph/graph_solution.ipynb","lineNumber":196,"sourceCode":"    \"\\n\",\n    \"class Node:\\n\",\n    \"\\n\",\n    \"    def __init__(self, key):\\n\",\n    \"        self.key = key\\n\",\n    \"        self.visit_state = State.unvisited\\n\",\n    \"        self.incoming_edges = 0\\n\",\n    \"        self.adj_nodes = {}  # Key = key, val = Node\\n\",\n    \"        self.adj_weights = {}  # Key = key, val = weight\\n\",\n    \"\\n\",\n    \"    def __repr__(self):\\n\",\n    \"        return str(self.key)\\n\",\n    \"\\n\",\n    \"    def __lt__(self, other):\\n\",\n    \"        return self.key < other.key\\n\",\n    \"\\n\",\n    \"    def add_neighbor(self, neighbor, weight=0):\\n\",\n    \"        if neighbor is None or weight is None:\\n\",\n    \"            raise TypeError('neighbor or weight cannot be None')\\n\",\n    \"        neighbor.incoming_edges += 1\\n\",\n    \"        self.adj_weights[neighbor.key] = weight\\n\",\n    \"        self.adj_nodes[neighbor.key] = neighbor\\n\",\n    \"\\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\",","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/graphs_trees/graph/graph_solution.ipynb#L178-L214","documentation":"Raised by Node.add_neighbor(neighbor, weight) in graph_solution.ipynb when either neighbor or weight is None. The graph implementation stores weights keyed by neighbor in adj_weights/adj_nodes dicts, so a None neighbor has no .key and None weight would poison shortest-path arithmetic. The guard rejects the call with TypeError before any mutation happens.","triggerScenarios":"Calling add_neighbor(None, 5), add_neighbor(node, None), or relying on the default weight=0 while explicitly passing weight=None. Also triggered indirectly by Graph.add_edge if it ever forwards a None weight or a None-valued node lookup.","commonSituations":"Building graphs from sparse data (CSV/JSON) where some edge weights are missing and default to None; passing an uninitialized node variable; calling add_edge(src, dst, weight=None) expecting the default weight to kick in.","solutions":["Coerce None weight to the default: use weight = weight if weight is not None else 0 (or call add_edge without the weight argument)","Ensure the neighbor object is created and registered via Graph.add_node before add_neighbor","Validate edge data at load time and reject/skip rows with missing endpoints or weights"],"exampleFix":"# before\nnode_a.add_neighbor(node_b, None)  # TypeError\n\n# after\nweight = 0 if weight is None else weight\nnode_a.add_neighbor(node_b, weight)","handlingStrategy":"validation","validationCode":"if neighbor is not None and weight is not None:\n    node.add_neighbor(neighbor, weight)\n# or normalize: node.add_neighbor(neighbor, 0 if weight is None else weight)","typeGuard":"def is_valid_neighbor(neighbor, weight):\n    return neighbor is not None and weight is not None","tryCatchPattern":"try:\n    node.add_neighbor(neighbor, weight)\nexcept TypeError as e:\n    if 'cannot be None' in str(e):\n        skip_or_log_edge(neighbor, weight)\n    else:\n        raise","preventionTips":["Never pass weight=None when you mean the default; omit the argument instead","Validate edge tuples (src, dst, weight) at data-load time before graph construction"],"tags":["python","graph","none-check","edge-weight"],"backgroundTag":"none-argument-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}