{"record":{"id":"59cd0dac5d0469c9","repo":"donnemartin/interactive-coding-challenges","slug":"neighbor-or-weight-cannot-be-none","errorCode":null,"errorMessage":"neighbor or weight cannot be None","messagePattern":"neighbor or weight cannot be None","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"graphs_trees/graph/graph.py","lineNumber":28,"sourceCode":"\nclass Node:\n\n    def __init__(self, key):\n        self.key = key\n        self.visit_state = State.unvisited\n        self.incoming_edges = 0\n        self.adj_nodes = {}  # Key = key, val = Node\n        self.adj_weights = {}  # Key = key, val = weight\n\n    def __repr__(self):\n        return str(self.key)\n\n    def __lt__(self, other):\n        return self.key < other.key\n\n    def add_neighbor(self, neighbor, weight=0):\n        if neighbor is None or weight is None:\n            raise TypeError('neighbor or weight cannot be None')\n        neighbor.incoming_edges += 1\n        self.adj_weights[neighbor.key] = weight\n        self.adj_nodes[neighbor.key] = neighbor\n\n    def remove_neighbor(self, neighbor):\n        if neighbor is None:\n            raise TypeError('neighbor cannot be None')\n        if neighbor.key not in self.adj_nodes:\n            raise KeyError('neighbor not found')\n        neighbor.incoming_edges -= 1\n        del self.adj_weights[neighbor.key]\n        del self.adj_nodes[neighbor.key]\n\n\nclass Graph:\n\n    def __init__(self):\n        self.nodes = {}  # Key = key, val = Node","sourceCodeStart":10,"sourceCodeEnd":46,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/graphs_trees/graph/graph.py#L10-L46","documentation":"Raised by Node.add_neighbor when either neighbor or weight is None. The graph stores weights and node references in dicts keyed by neighbor.key, so None inputs would break those lookups. It guards the internal edge-bookkeeping used by add_edge.","triggerScenarios":"Calling node.add_neighbor(None) or node.add_neighbor(node, None); indirectly via Graph.add_edge(src, dst, weight=None).","commonSituations":"Passing a default weight parameter that was computed as None from config; calling the low-level Node API directly instead of Graph.add_edge.","solutions":["Call Graph.add_edge instead of Node.add_neighbor; it handles node creation","Ensure weight is an int (default 0) and neighbor is a real Node instance","Check for None before invoking when weight comes from external config"],"exampleFix":"# before\ngraph.nodes['a'].add_neighbor(graph.nodes['b'], None)\n\n# after\ngraph.add_edge('a', 'b', weight=0)","handlingStrategy":"validation","validationCode":"if neighbor is not None and weight is not None:\n    node.add_neighbor(neighbor, weight)","typeGuard":"def valid_edge_parts(neighbor, weight) -> bool:\n    return neighbor is not None and weight is not None and isinstance(weight, int)","tryCatchPattern":"try:\n    node.add_neighbor(neighbor, weight)\nexcept TypeError:\n    log.warning('invalid edge parts, skipping')","preventionTips":["Prefer Graph.add_edge over the low-level Node.add_neighbor API","Always pass an explicit integer weight"],"tags":["graph","adjacency-list","none-check","typeerror"],"backgroundTag":"none-argument-rejected","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}