From 90da38a6d0900d59b5aff345c8ec0c8d153ab98f Mon Sep 17 00:00:00 2001 From: Koen Deschacht Date: Wed, 4 Jun 2025 09:45:09 +0200 Subject: [PATCH] Fix crash in dmypy when packages are removed during dmypy run --- mypy/dmypy_server.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/mypy/dmypy_server.py b/mypy/dmypy_server.py index 33e9e07477ca..6ba547b990c0 100644 --- a/mypy/dmypy_server.py +++ b/mypy/dmypy_server.py @@ -643,9 +643,9 @@ def fine_grained_increment_follow_imports( worklist = changed.copy() while worklist: module = worklist.pop() - if module[0] not in graph: - continue sources2 = self.direct_imports(module, graph) + if sources2 is None: + continue # Filter anything already seen before. This prevents # infinite looping if there are any self edges. (Self # edges are maybe a bug, but...) @@ -770,10 +770,14 @@ def find_reachable_changed_modules( def direct_imports( self, module: tuple[str, str], graph: mypy.build.Graph - ) -> list[BuildSource]: + ) -> list[BuildSource] | None: """Return the direct imports of module not included in seen.""" - state = graph[module[0]] - return [BuildSource(graph[dep].path, dep, followed=True) for dep in state.dependencies] + try: + state = graph[module[0]] + return [BuildSource(graph[dep].path, dep, followed=True) for dep in state.dependencies] + except KeyError: + # Dependency not found in graph, it was probably removed while dmypy was running. + return None def find_added_suppressed( self, graph: mypy.build.Graph, seen: set[str], search_paths: SearchPaths