Skip to content

Commit 0135fa4

Browse files
committed
lazy string formatting
1 parent ec28a6a commit 0135fa4

7 files changed

Lines changed: 46 additions & 46 deletions

File tree

codeflash/languages/javascript/import_resolver.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def resolve_import(self, import_info: ImportInfo, source_file: Path) -> Resolved
6161

6262
# Skip external packages (node_modules)
6363
if self._is_external_package(module_path):
64-
logger.debug(f"Skipping external package: {module_path}")
64+
logger.debug("Skipping external package: %s", module_path)
6565
return None
6666

6767
# Check cache
@@ -79,7 +79,7 @@ def resolve_import(self, import_info: ImportInfo, source_file: Path) -> Resolved
7979
self._resolution_cache[cache_key] = resolved_path
8080

8181
if resolved_path is None:
82-
logger.debug(f"Could not resolve import: {module_path} from {source_file}")
82+
logger.debug("Could not resolve import: %s from %s", module_path, source_file)
8383
return None
8484

8585
return self._build_resolved_import(import_info, resolved_path)
@@ -145,7 +145,7 @@ def _resolve_relative_import(self, module_path: str, source_dir: Path) -> Path |
145145
try:
146146
base_path.relative_to(self.project_root)
147147
except ValueError:
148-
logger.debug(f"Import path outside project root: {base_path}")
148+
logger.debug("Import path outside project root: %s", base_path)
149149
return None
150150

151151
# If the path already has an extension, try it directly first
@@ -439,7 +439,7 @@ def _extract_helper_from_file(
439439
try:
440440
source = file_path.read_text(encoding="utf-8")
441441
except Exception as e:
442-
logger.warning(f"Failed to read {file_path}: {e}")
442+
logger.warning("Failed to read %s: %s", file_path, e)
443443
return None
444444

445445
# Get analyzer for this file type
@@ -488,7 +488,7 @@ def _extract_helper_from_file(
488488
end_line=func.end_line,
489489
)
490490

491-
logger.debug(f"Function {function_name} not found in {file_path}")
491+
logger.debug("Function %s not found in %s", function_name, file_path)
492492
return None
493493

494494
def _find_helpers_recursive(
@@ -518,7 +518,7 @@ def _find_helpers_recursive(
518518
try:
519519
source = file_path.read_text(encoding="utf-8")
520520
except Exception as e:
521-
logger.warning(f"Failed to read {file_path}: {e}")
521+
logger.warning("Failed to read %s: %s", file_path, e)
522522
return {}
523523

524524
# Get analyzer and imports for this file

codeflash/languages/javascript/line_profiler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ def _instrument_function(self, func: FunctionInfo, lines: list[str], file_path:
194194
tree = analyzer.parse(source.encode("utf8"))
195195
executable_lines = self._find_executable_lines(tree.root_node, source.encode("utf8"))
196196
except Exception as e:
197-
logger.warning(f"Failed to parse function {func.name}: {e}")
197+
logger.warning("Failed to parse function %s: %s", func.name, e)
198198
return func_lines
199199

200200
# Add profiling to each executable line
@@ -329,5 +329,5 @@ def parse_results(profile_file: Path) -> dict:
329329
}
330330

331331
except Exception as e:
332-
logger.exception(f"Failed to parse line profile results: {e}")
332+
logger.exception("Failed to parse line profile results: %s", e)
333333
return {"timings": {}, "unit": 1e-9, "functions": {}}

codeflash/languages/javascript/module_system.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def detect_module_system(project_root: Path, file_path: Path | None = None) -> s
7373
return ModuleSystem.COMMONJS
7474

7575
except Exception as e:
76-
logger.warning(f"Failed to parse package.json: {e}")
76+
logger.warning("Failed to parse package.json: %s", e)
7777

7878
# Strategy 2: Check file extension
7979
if file_path:
@@ -108,7 +108,7 @@ def detect_module_system(project_root: Path, file_path: Path | None = None) -> s
108108
return ModuleSystem.COMMONJS
109109

110110
except Exception as e:
111-
logger.warning(f"Failed to analyze file {file_path}: {e}")
111+
logger.warning("Failed to analyze file %s: %s", file_path, e)
112112

113113
# Default to CommonJS (more common and backward compatible)
114114
logger.debug("Defaulting to CommonJS")

codeflash/languages/javascript/support.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def discover_functions(
8484
try:
8585
source = file_path.read_text(encoding="utf-8")
8686
except Exception as e:
87-
logger.warning(f"Failed to read {file_path}: {e}")
87+
logger.warning("Failed to read %s: %s", file_path, e)
8888
return []
8989

9090
try:
@@ -129,7 +129,7 @@ def discover_functions(
129129
return functions
130130

131131
except Exception as e:
132-
logger.warning(f"Failed to parse {file_path}: {e}")
132+
logger.warning("Failed to parse %s: %s", file_path, e)
133133
return []
134134

135135
def discover_functions_from_source(self, source: str, file_path: Path | None = None) -> list[FunctionInfo]:
@@ -184,7 +184,7 @@ def discover_functions_from_source(self, source: str, file_path: Path | None = N
184184
return functions
185185

186186
except Exception as e:
187-
logger.warning(f"Failed to parse source: {e}")
187+
logger.warning("Failed to parse source: %s", e)
188188
return []
189189

190190
def _get_test_patterns(self) -> list[str]:
@@ -248,7 +248,7 @@ def discover_tests(self, test_root: Path, source_functions: Sequence[FunctionInf
248248
TestInfo(test_name=test_name, test_file=test_file, test_class=None)
249249
)
250250
except Exception as e:
251-
logger.debug(f"Failed to analyze test file {test_file}: {e}")
251+
logger.debug("Failed to analyze test file %s: %s", test_file, e)
252252

253253
return result
254254

@@ -299,7 +299,7 @@ def extract_code_context(self, function: FunctionInfo, project_root: Path, modul
299299
try:
300300
source = function.file_path.read_text()
301301
except Exception as e:
302-
logger.exception(f"Failed to read {function.file_path}: {e}")
302+
logger.exception("Failed to read %s: %s", function.file_path, e)
303303
return CodeContext(target_code="", target_file=function.file_path, language=Language.JAVASCRIPT)
304304

305305
# Find imports and helper functions
@@ -623,7 +623,7 @@ def _find_helper_functions(
623623
helpers.extend(file_helpers)
624624

625625
except Exception as e:
626-
logger.debug(f"Failed to find cross-file helpers: {e}")
626+
logger.debug("Failed to find cross-file helpers: %s", e)
627627

628628
return helpers
629629

@@ -866,7 +866,7 @@ def _find_imported_type_definitions(
866866
break
867867

868868
except Exception as e:
869-
logger.debug(f"Failed to resolve type definition for {local_name}: {e}")
869+
logger.debug("Failed to resolve type definition for %s: %s", local_name, e)
870870
continue
871871

872872
return found_definitions
@@ -888,7 +888,7 @@ def find_helper_functions(self, function: FunctionInfo, project_root: Path) -> l
888888
imports = analyzer.find_imports(source)
889889
return self._find_helper_functions(function, source, analyzer, imports, project_root)
890890
except Exception as e:
891-
logger.warning(f"Failed to find helpers for {function.name}: {e}")
891+
logger.warning("Failed to find helpers for %s: %s", function.name, e)
892892
return []
893893

894894
# === Code Transformation ===
@@ -913,7 +913,7 @@ def replace_function(self, source: str, function: FunctionInfo, new_source: str)
913913
914914
"""
915915
if function.start_line is None or function.end_line is None:
916-
logger.error(f"Function {function.name} has no line information")
916+
logger.error("Function %s has no line information", function.name)
917917
return source
918918

919919
# Get analyzer for parsing
@@ -925,7 +925,7 @@ def replace_function(self, source: str, function: FunctionInfo, new_source: str)
925925
# Extract just the method body from the new source
926926
new_body = self._extract_function_body(new_source, function.name, analyzer)
927927
if new_body is None:
928-
logger.warning(f"Could not extract body for {function.name} from optimized code, using full replacement")
928+
logger.warning("Could not extract body for %s from optimized code, using full replacement", function.name)
929929
return self._replace_function_text_based(source, function, new_source, analyzer)
930930

931931
# Find the original function and replace its body
@@ -1062,7 +1062,7 @@ def find_function_at_line(node, target_name: str, target_line: int):
10621062

10631063
func_node = find_function_at_line(tree.root_node, function.name, function.start_line)
10641064
if not func_node:
1065-
logger.warning(f"Could not find function {function.name} at line {function.start_line}")
1065+
logger.warning("Could not find function %s at line %s", function.name, function.start_line)
10661066
return source
10671067

10681068
# Find the body node in the original
@@ -1074,7 +1074,7 @@ def find_function_at_line(node, target_name: str, target_line: int):
10741074
break
10751075

10761076
if not body_node:
1077-
logger.warning(f"Could not find body for function {function.name}")
1077+
logger.warning("Could not find body for function %s", function.name)
10781078
return source
10791079

10801080
# Get the indentation of the original body's opening brace
@@ -1250,7 +1250,7 @@ def format_code(self, source: str, file_path: Path | None = None) -> str:
12501250
except (subprocess.TimeoutExpired, FileNotFoundError):
12511251
pass
12521252
except Exception as e:
1253-
logger.debug(f"Prettier formatting failed: {e}")
1253+
logger.debug("Prettier formatting failed: %s", e)
12541254

12551255
return source
12561256

@@ -1300,10 +1300,10 @@ def run_tests(
13001300
return results, junit_xml
13011301

13021302
except subprocess.TimeoutExpired:
1303-
logger.warning(f"Test execution timed out after {timeout}s")
1303+
logger.warning("Test execution timed out after %ss", timeout)
13041304
return [], junit_xml
13051305
except Exception as e:
1306-
logger.exception(f"Test execution failed: {e}")
1306+
logger.exception("Test execution failed: %s", e)
13071307
return [], junit_xml
13081308

13091309
def parse_test_results(self, junit_xml_path: Path, stdout: str) -> list[TestResult]:
@@ -1363,7 +1363,7 @@ def parse_test_results(self, junit_xml_path: Path, stdout: str) -> list[TestResu
13631363
)
13641364
)
13651365
except Exception as e:
1366-
logger.warning(f"Failed to parse JUnit XML: {e}")
1366+
logger.warning("Failed to parse JUnit XML: %s", e)
13671367

13681368
return results
13691369

@@ -1731,10 +1731,10 @@ def instrument_source_for_line_profiler(
17311731

17321732
# Write instrumented code to source file
17331733
source_file_path.write_text(instrumented_source, encoding="utf-8")
1734-
logger.debug(f"Wrote instrumented source to {source_file_path}") # noqa: G004
1734+
logger.debug("Wrote instrumented source to %s", source_file_path)
17351735
return True # noqa: TRY300
17361736
except Exception as e:
1737-
logger.warning(f"Failed to instrument source for line profiling: {e}") # noqa: G004
1737+
logger.warning("Failed to instrument source for line profiling: %s", e)
17381738
return False
17391739

17401740
def parse_line_profile_results(self, line_profiler_output_file: Path) -> dict:
@@ -1746,7 +1746,7 @@ def parse_line_profile_results(self, line_profiler_output_file: Path) -> dict:
17461746
# Format output string for display
17471747
str_out = self._format_js_line_profile_output(parsed_results)
17481748
return {"timings": parsed_results.get("timings", {}), "unit": 1e-9, "str_out": str_out}
1749-
logger.warning(f"No line profiler output file found at {line_profiler_output_file}") # noqa: G004
1749+
logger.warning("No line profiler output file found at %s", line_profiler_output_file)
17501750
return {"timings": {}, "unit": 0, "str_out": ""}
17511751

17521752
def _format_js_line_profile_output(self, parsed_results: dict) -> str:
@@ -1984,6 +1984,6 @@ def format_code(self, source: str, file_path: Path | None = None) -> str:
19841984
except (subprocess.TimeoutExpired, FileNotFoundError):
19851985
pass
19861986
except Exception as e:
1987-
logger.debug(f"Prettier formatting failed: {e}")
1987+
logger.debug("Prettier formatting failed: %s", e)
19881988

19891989
return source

codeflash/languages/javascript/tracer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ def _wrap_method(self, func_lines: list[str], func_name: str, file_path: Path, i
326326
# For methods, we wrap by reassigning them after definition
327327
# This is complex, so for now we'll return unwrapped
328328
# TODO: Implement method wrapping
329-
logger.warning(f"Method wrapping not fully implemented for {func_name}")
329+
logger.warning("Method wrapping not fully implemented for %s", func_name)
330330
return func_lines
331331

332332
def _wrap_regular_function(
@@ -366,7 +366,7 @@ def parse_results(trace_file: Path) -> list[dict[str, Any]]:
366366
with json_file.open("r") as f:
367367
return json.load(f)
368368
except Exception as e:
369-
logger.exception(f"Failed to parse trace JSON: {e}")
369+
logger.exception("Failed to parse trace JSON: %s", e)
370370
return []
371371

372372
# Try SQLite database
@@ -398,5 +398,5 @@ def parse_results(trace_file: Path) -> list[dict[str, Any]]:
398398
return traces
399399

400400
except Exception as e:
401-
logger.exception(f"Failed to parse trace database: {e}")
401+
logger.exception("Failed to parse trace database: %s", e)
402402
return []

codeflash/languages/python/support.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ def discover_functions(
128128
return functions
129129

130130
except Exception as e:
131-
logger.warning(f"Failed to discover functions in {file_path}: {e}")
131+
logger.warning("Failed to discover functions in %s: %s", file_path, e)
132132
return []
133133

134134
def discover_tests(self, test_root: Path, source_functions: Sequence[FunctionInfo]) -> dict[str, list[TestInfo]]:
@@ -183,7 +183,7 @@ def extract_code_context(self, function: FunctionInfo, project_root: Path, modul
183183
try:
184184
source = function.file_path.read_text()
185185
except Exception as e:
186-
logger.exception(f"Failed to read {function.file_path}: {e}")
186+
logger.exception("Failed to read %s: %s", function.file_path, e)
187187
return CodeContext(target_code="", target_file=function.file_path, language=Language.PYTHON)
188188

189189
# Extract the function source
@@ -285,7 +285,7 @@ def find_helper_functions(self, function: FunctionInfo, project_root: Path) -> l
285285
)
286286

287287
except Exception as e:
288-
logger.warning(f"Failed to find helpers for {function.name}: {e}")
288+
logger.warning("Failed to find helpers for %s: %s", function.name, e)
289289

290290
return helpers
291291

@@ -319,7 +319,7 @@ def replace_function(self, source: str, function: FunctionInfo, new_source: str)
319319
preexisting_objects=set(),
320320
)
321321
except Exception as e:
322-
logger.warning(f"Failed to replace function {function.name}: {e}")
322+
logger.warning("Failed to replace function %s: %s", function.name, e)
323323
return source
324324

325325
def format_code(self, source: str, file_path: Path | None = None) -> str:
@@ -345,7 +345,7 @@ def format_code(self, source: str, file_path: Path | None = None) -> str:
345345
except (subprocess.TimeoutExpired, FileNotFoundError):
346346
pass
347347
except Exception as e:
348-
logger.debug(f"Ruff formatting failed: {e}")
348+
logger.debug("Ruff formatting failed: %s", e)
349349

350350
# Try black as fallback
351351
try:
@@ -357,7 +357,7 @@ def format_code(self, source: str, file_path: Path | None = None) -> str:
357357
except (subprocess.TimeoutExpired, FileNotFoundError):
358358
pass
359359
except Exception as e:
360-
logger.debug(f"Black formatting failed: {e}")
360+
logger.debug("Black formatting failed: %s", e)
361361

362362
return source
363363

@@ -395,10 +395,10 @@ def run_tests(
395395
return results, junit_xml
396396

397397
except subprocess.TimeoutExpired:
398-
logger.warning(f"Test execution timed out after {timeout}s")
398+
logger.warning("Test execution timed out after %ss", timeout)
399399
return [], junit_xml
400400
except Exception as e:
401-
logger.exception(f"Test execution failed: {e}")
401+
logger.exception("Test execution failed: %s", e)
402402
return [], junit_xml
403403

404404
def parse_test_results(self, junit_xml_path: Path, stdout: str) -> list[TestResult]:
@@ -459,7 +459,7 @@ def parse_test_results(self, junit_xml_path: Path, stdout: str) -> list[TestResu
459459
)
460460
)
461461
except Exception as e:
462-
logger.warning(f"Failed to parse JUnit XML: {e}")
462+
logger.warning("Failed to parse JUnit XML: %s", e)
463463

464464
return results
465465

codeflash/languages/registry.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,19 +89,19 @@ def file_extensions(self) -> tuple[str, ...]:
8989
if ext_lower in _EXTENSION_REGISTRY:
9090
existing = _EXTENSION_REGISTRY[ext_lower]
9191
logger.warning(
92-
f"Extension '{ext}' already registered to {existing.__name__}, overwriting with {cls.__name__}"
92+
"Extension '%s' already registered to %s, overwriting with %s", ext, existing.__name__, cls.__name__
9393
)
9494
_EXTENSION_REGISTRY[ext_lower] = cls
9595

9696
# Register by language
9797
if language in _LANGUAGE_REGISTRY:
9898
existing = _LANGUAGE_REGISTRY[language]
9999
logger.warning(
100-
f"Language '{language}' already registered to {existing.__name__}, overwriting with {cls.__name__}"
100+
"Language '%s' already registered to %s, overwriting with %s", language, existing.__name__, cls.__name__
101101
)
102102
_LANGUAGE_REGISTRY[language] = cls
103103

104-
logger.debug(f"Registered {cls.__name__} for language '{language}' with extensions {extensions}")
104+
logger.debug("Registered %s for language '%s' with extensions %s", cls.__name__, language, extensions)
105105

106106
return cls
107107

@@ -241,7 +241,7 @@ def detect_project_language(project_root: Path, module_root: Path) -> Language:
241241
for ext, count in sorted(extension_counts.items(), key=lambda x: -x[1]):
242242
if ext in _EXTENSION_REGISTRY:
243243
cls = _EXTENSION_REGISTRY[ext]
244-
logger.info(f"Detected language: {cls().language} (found {count} '{ext}' files)")
244+
logger.info("Detected language: %s (found %d '%s' files)", cls().language, count, ext)
245245
return cls().language
246246

247247
msg = f"No supported language detected in {module_root}"

0 commit comments

Comments
 (0)