Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions codeflash/languages/code_replacer.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@ def get_optimized_code_for_module(relative_path: Path, optimized_code: CodeStrin
module_optimized_code = file_to_code_context["None"]
logger.debug(f"Using code block with None file_path for {relative_path}")
else:
logger.warning(
f"Optimized code not found for {relative_path} In the context\n-------\n{optimized_code}\n-------\n"
"re-check your 'markdown code structure'"
f"existing files are {file_to_code_context.keys()}"
)
# Avoid expensive string formatting when logging is not enabled
if logger.isEnabledFor(logger.level):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: logger.isEnabledFor(logger.level) is always False — warnings are silently suppressed.

logger is logging.getLogger("rich") which never has its level explicitly set, so logger.level is 0 (NOTSET). isEnabledFor(0) checks 0 >= effective_level (which is INFO=20 from the root), returning False. This means the warning is never logged under any circumstances.

The 41x speedup comes from skipping the warning entirely, not from avoiding unnecessary formatting.

The correct guard (if desired) should be:

Suggested change
if logger.isEnabledFor(logger.level):
if logger.isEnabledFor(logging.WARNING):

This would still skip f-string formatting when warnings are disabled (e.g., if the log level is set to ERROR), while correctly logging when warnings are enabled.

logger.warning(
f"Optimized code not found for {relative_path} In the context\n-------\n{optimized_code}\n-------\n"
"re-check your 'markdown code structure'"
f"existing files are {file_to_code_context.keys()}"
)
module_optimized_code = ""
return module_optimized_code

Expand Down
12 changes: 6 additions & 6 deletions codeflash/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,12 +331,12 @@ def file_to_path(self) -> dict[str, str]:
dict[str, str]: Mapping from file path (as string) to code.

"""
if self._cache.get("file_to_path") is not None:
return self._cache["file_to_path"]
self._cache["file_to_path"] = {
str(code_string.file_path): code_string.code for code_string in self.code_strings
}
return self._cache["file_to_path"]
cached = self._cache.get("file_to_path")
if cached is not None:
return cached
result = {str(code_string.file_path): code_string.code for code_string in self.code_strings}
self._cache["file_to_path"] = result
return result

@staticmethod
def parse_markdown_code(markdown_code: str, expected_language: str = "python") -> CodeStringsMarkdown:
Expand Down
Loading