Skip to content

Commit 3c3c65b

Browse files
committed
fix: guard against missing key in annotation dependency tracking
The `_extract_names_from_annotation` method accessed `self.definitions[self.current_top_level_name]` without verifying that `current_top_level_name` exists in the definitions dict. When visiting methods inside built-in types (e.g., `str.removeprefix`), the dotted name was set as `current_top_level_name` but never added to `definitions`, causing a KeyError on CI.
1 parent d9e3eaf commit 3c3c65b

2 files changed

Lines changed: 7 additions & 2 deletions

File tree

codeflash/languages/python/context/unused_definition_remover.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,12 @@ def visit_FunctionDef(self, node: cst.FunctionDef) -> None:
202202
def _extract_names_from_annotation(self, node: cst.CSTNode) -> None:
203203
if isinstance(node, cst.Name):
204204
name = node.value
205-
if name in self.definitions and name != self.current_top_level_name and self.current_top_level_name:
205+
if (
206+
name in self.definitions
207+
and name != self.current_top_level_name
208+
and self.current_top_level_name
209+
and self.current_top_level_name in self.definitions
210+
):
206211
self.definitions[self.current_top_level_name].dependencies.add(name)
207212
elif isinstance(node, cst.Subscript):
208213
self._extract_names_from_annotation(node.value)

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ module = ["jedi", "jedi.api.classes", "inquirer", "inquirer.themes", "numba", "d
225225
ignore_missing_imports = true
226226

227227
[[tool.mypy.overrides]]
228-
module = ["codeflash.code_utils.codeflash_wrap_decorator", "codeflash.code_utils.instrument_existing_tests", "codeflash.optimization.optimizer"]
228+
module = ["codeflash.code_utils.codeflash_wrap_decorator", "codeflash.code_utils.instrument_existing_tests", "codeflash.optimization.optimizer", "codeflash.languages.python.context.unused_definition_remover"]
229229
disable_error_code = ["attr-defined", "return-value", "no-untyped-call", "no-untyped-def", "arg-type", "assignment", "var-annotated", "no-any-return", "call-overload", "union-attr", "unreachable", "list-item"]
230230

231231
[tool.pydantic-mypy]

0 commit comments

Comments
 (0)