Skip to content

Conversation

aseembits93
Copy link
Contributor

@aseembits93 aseembits93 commented Oct 15, 2025

User description

isort fails to format if there is a # isort:skip_file, we are now catching it everywhere with a try-catch block


PR Type

Enhancement, Bug fix


Description

  • Centralize import sorting via sort_imports

  • Add safe isort fallback on exceptions

  • Replace direct isort usage across modules

  • Support float_to_top passthrough


Diagram Walkthrough

flowchart LR A["Modules using isort directly"] -- "replace with" --> B["code_utils.formatter.sort_imports()"] B -- "handles exceptions, float_to_top" --> C["Consistent, safe import sorting"] 
Loading

File Walkthrough

Relevant files
Enhancement
8 files
instrument_codeflash_trace.py
Use shared import sorter for decorator instrumentation     
+3/-2     
replay_test.py
Replace isort with safe sorter for test code                         
+2/-3     
code_replacer.py
Apply centralized sorter in pytest marker addition             
+2/-2     
formatter.py
Add robust sort_imports wrapper with options                         
+3/-3     
instrument_existing_tests.py
Route async decorator import sorting through wrapper         
+2/-1     
line_profile_utils.py
Use safe sorter after adding profiler imports                       
+2/-2     
function_optimizer.py
Check import sorting changes via wrapper                                 
+1/-2     
instrument_codeflash_capture.py
Sort imports via formatter after AST changes                         
+2/-3     
Bug fix
1 files
tracing_new_process.py
Guard isort call with suppress for replay tests                   
+4/-1     

Copy link

PR Reviewer Guide 🔍

Here are some key observations to aid the review process:

⏱️ Estimated effort to review: 2 🔵🔵⚪⚪⚪
🧪 No relevant tests
🔒 No security concerns identified
⚡ Recommended focus areas for review

Logging Level

The exception path for import sorting logs with logger.exception, which may be noisy for expected isort skips; consider downgrading to warning/info or adding conditional handling for known benign cases to reduce log noise.

try: # Deduplicate and sort imports, modify the code in memory, not on disk sorted_code = isort.code(code=code, float_to_top=float_to_top) except Exception: # this will also catch the FileSkipComment exception, use this fn everywhere logger.exception("Failed to sort imports with isort.") return code # Fall back to original code if isort fails
Inconsistent Wrapper

Most modules now use sort_imports but tracing_new_process still imports isort directly with a suppress block; consider switching to sort_imports for consistency and to centralize error handling.

from contextlib import suppress import isort from codeflash.tracing.replay_test import create_trace_replay_test from codeflash.verification.verification_utils import get_test_file_path replay_test = create_trace_replay_test( trace_file=self.output_file, functions=self.function_modules, test_framework=self.config["test_framework"], max_run_count=self.max_function_count, ) function_path = "_".join(self.functions) if self.functions else self.sanitized_filename test_file_path = get_test_file_path( test_dir=Path(self.config["tests_root"]), function_name=function_path, test_type="replay" ) with suppress(Exception): replay_test = isort.code(replay_test)
Behavior Change Check

The heuristic to disable sorting now uses sort_imports; verify this maintains the intended behavior and performance, since sort_imports may return unchanged code on exceptions and alter the condition.

def reformat_code_and_helpers( self, helper_functions: list[FunctionSource], path: Path, original_code: str, optimized_context: CodeStringsMarkdown, ) -> tuple[str, dict[Path, str]]: should_sort_imports = not self.args.disable_imports_sorting if should_sort_imports and sort_imports(code=original_code) != original_code: should_sort_imports = False
Copy link

PR Code Suggestions ✨

Explore these optional code suggestions:

CategorySuggestion                                                                                                                                    Impact
General
Use unified import sorter

Avoid importing and using isort directly here; use the centralized sort_imports
helper to keep behavior consistent (including safe failure handling). This also
removes the need for local suppression logic duplication.

codeflash/tracing/tracing_new_process.py [268-287]

-from contextlib import suppress +from codeflash.code_utils.formatter import sort_imports +... +replay_test = sort_imports(code=replay_test) -import isort -... -with suppress(Exception): - replay_test = isort.code(replay_test) -
Suggestion importance[1-10]: 8

__

Why: Replacing direct isort with centralized sort_imports aligns with the PR-wide refactor and leverages consistent error handling, removing ad-hoc suppression. This improves maintainability and robustness across the codebase.

Medium
Downgrade failure log severity

Reduce noise by logging at debug instead of exception level when import sorting
fails, since the function already safely falls back. This prevents log spam in
normal flows where files opt-out via skip comments.

codeflash/code_utils/formatter.py [169-177]

 def sort_imports(code: str, *, float_to_top: bool = False) -> str: try: - # Deduplicate and sort imports, modify the code in memory, not on disk sorted_code = isort.code(code=code, float_to_top=float_to_top) - except Exception: # this will also catch the FileSkipComment exception, use this fn everywhere - logger.exception("Failed to sort imports with isort.") - return code # Fall back to original code if isort fails + except Exception: + logger.debug("Skipping import sorting; isort failed.", exc_info=True) + return code return sorted_code
Suggestion importance[1-10]: 6

__

Why: Changing the log from exception to debug reduces noisy logs for expected skip cases while preserving fallback behavior; it's accurate and beneficial but not critical to functionality.

Low
Possible issue
Fix conditional logic inversion

This logic disables import sorting when it detects that sorting would change the
code, which is the opposite of the intended check. Invert the comparison so sorting
is performed only when it would actually change something. This prevents silently
skipping necessary import sorting.

codeflash/optimization/function_optimizer.py [902-903]

-if should_sort_imports and sort_imports(code=original_code) != original_code: +if should_sort_imports and sort_imports(code=original_code) == original_code: should_sort_imports = False
Suggestion importance[1-10]: 7

__

Why: The existing check disables sorting when a change would occur, which contradicts the likely intent; flipping the comparison matches the description and improves behavior. Impact is moderate as it affects whether imports get sorted but not a critical bug.

Medium
Copy link
Contributor

codeflash-ai bot commented Oct 15, 2025

⚡️ Codeflash found optimizations for this PR

📄 175% (1.75x) speedup for add_codeflash_capture_to_init in codeflash/verification/instrument_codeflash_capture.py

⏱️ Runtime : 399 milliseconds 145 milliseconds (best of 36 runs)

A dependent PR with the suggested changes has been created. Please review:

If you approve, it will be merged into this PR (branch isort-disregard-skip).

Copy link
Contributor

@KRRT7 KRRT7 left a comment

Choose a reason for hiding this comment

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

I'm a bit confused with this, wasn't the error happening in the backend? Additionally it seems like disregard_skip is not being passed as a config to isort, which would help

@aseembits93
Copy link
Contributor Author

@KRRT7 i have tried with disregard_skip, it doesn't work the way you're thinking

@aseembits93
Copy link
Contributor Author

aseembits93 commented Oct 15, 2025

@KRRT7 try this

import isort code = """# isort:skip_file import sys, os, json # isort will ignore this file completely""" isort.code(code=code, disregard_skip=True) 
@aseembits93
Copy link
Contributor Author

the error happens both on frontend and backend

Saga4
Saga4 previously approved these changes Oct 16, 2025
Copy link
Contributor

@Saga4 Saga4 left a comment

Choose a reason for hiding this comment

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

LGTM, can add a short test for isort for future validation

_run_formatting_test(source_code, True, optimized_function=optimization_function, expected=expected)

def test_sort_imports_skip_file():
"""Test that isort skips files with # isort:skip_file."""
Copy link
Contributor

Choose a reason for hiding this comment

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

This is skip file test? Whats the use of this. We should have a test where we are sorting the packages and asserting on them I think,

@aseembits93 aseembits93 merged commit fe82617 into main Oct 17, 2025
21 of 22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

3 participants