Skip to content
Closed
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
8 changes: 7 additions & 1 deletion codeflash/code_utils/formatter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from __future__ import annotations

import difflib
import functools
import os
import re
import shlex
Expand Down Expand Up @@ -169,9 +170,14 @@ def format_code(
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)
sorted_code = _cached_isort_code(code, 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

return sorted_code


@functools.lru_cache(maxsize=128)
def _cached_isort_code(code: str, float_to_top: bool) -> str:
return isort.code(code=code, float_to_top=float_to_top)
Loading