{"record":{"id":"55c3b8ca2f8f0bf1","repo":"donnemartin/interactive-coding-challenges","slug":"graph-cannot-be-none","errorCode":null,"errorMessage":"graph cannot be None","messagePattern":"graph cannot be None","errorType":"exception","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"graphs_trees/graph_shortest_path/graph_shortest_path_solution.ipynb","lineNumber":185,"sourceCode":"   \"outputs\": [],\n   \"source\": [\n    \"%run ../graph/graph.py\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 3,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"import sys\\n\",\n    \"\\n\",\n    \"\\n\",\n    \"class ShortestPath(object):\\n\",\n    \"\\n\",\n    \"    def __init__(self, graph):\\n\",\n    \"        if graph is None:\\n\",\n    \"            raise TypeError('graph cannot be None')\\n\",\n    \"        self.graph = graph\\n\",\n    \"        self.previous = {}  # Key: node key, val: prev node key, shortest path\\n\",\n    \"        self.path_weight = {}  # Key: node key, val: weight, shortest path\\n\",\n    \"        self.remaining = PriorityQueue()  # Queue of node key, path weight\\n\",\n    \"        for key in self.graph.nodes.keys():\\n\",\n    \"            # Set each node's previous node key to None\\n\",\n    \"            # Set each node's shortest path weight to infinity\\n\",\n    \"            # Add each node's shortest path weight to the priority queue\\n\",\n    \"            self.previous[key] = None\\n\",\n    \"            self.path_weight[key] = sys.maxsize\\n\",\n    \"            self.remaining.insert(\\n\",\n    \"                PriorityQueueNode(key, self.path_weight[key]))\\n\",\n    \"\\n\",\n    \"    def find_shortest_path(self, start_node_key, end_node_key):\\n\",\n    \"        if start_node_key is None or end_node_key is None:\\n\",\n    \"            raise TypeError('Input node keys cannot be None')\\n\",\n    \"        if (start_node_key not in self.graph.nodes or\\n\",\n    \"                end_node_key not in self.graph.nodes):\\n\",","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/graphs_trees/graph_shortest_path/graph_shortest_path_solution.ipynb#L167-L203","documentation":"Raised by ShortestPath.__init__(graph) in graph_shortest_path_solution.ipynb when graph is None. The constructor immediately iterates graph.nodes to seed previous/path_weight and fill the priority queue, so a None graph would crash with AttributeError; the guard raises TypeError('graph cannot be None') instead.","triggerScenarios":"Constructing ShortestPath(None); passing a variable holding None because the Graph was never instantiated or a builder function returned None on failure; conditionally creating the graph in one branch and using it in another.","commonSituations":"DI/test setups where the graph fixture failed to build; factory functions that return None on error whose result is passed straight through; refactoring that moved Graph construction after ShortestPath creation.","solutions":["Ensure the Graph is constructed and populated before creating ShortestPath","If using a builder/factory, make it raise or return a valid empty Graph rather than None","Guard: if graph is None: skip or construct a new Graph() before instantiating ShortestPath"],"exampleFix":"# before\nsp = ShortestPath(build_graph(data))  # TypeError if build_graph returns None\n\n# after\ng = build_graph(data)\nif g is None:\n    g = Graph()\nsp = ShortestPath(g)","handlingStrategy":"validation","validationCode":"if graph is not None:\n    sp = ShortestPath(graph)\nelse:\n    graph = Graph()  # or raise your own clearer error","typeGuard":"def is_usable_graph(graph):\n    return graph is not None and hasattr(graph, 'nodes') and isinstance(graph.nodes, dict)","tryCatchPattern":"try:\n    sp = ShortestPath(graph)\nexcept TypeError as e:\n    if 'graph cannot be None' in str(e):\n        raise RuntimeError('graph construction failed upstream') from e\n    raise","preventionTips":["Make graph builders raise on failure instead of returning None","Construct ShortestPath only after the Graph is fully populated"],"tags":["python","graph","dijkstra","none-check","constructor"],"backgroundTag":"none-argument-validation","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}