Skip to content

Commit dcbd7ea

Browse files
Cache looking up isort config and skip sorting of imports on template expressions (#26)
This saves on a whole lot of subprocess calls.
1 parent 3329551 commit dcbd7ea

2 files changed

Lines changed: 35 additions & 12 deletions

File tree

ruff_cgx/template_formatter.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ def format_python_expression(expr: str) -> str:
4848
wrapped = f"__dummy__ = {expr}"
4949

5050
# Format with ruff using single quotes
51-
formatted = run_ruff_format(wrapped, use_single_quotes=True)
51+
# Skip import sorting since template expressions don't have imports
52+
formatted = run_ruff_format(
53+
wrapped, use_single_quotes=True, skip_import_sort=True
54+
)
5255

5356
# Extract the expression back out
5457
# (remove "__dummy__ = " and trailing newline)

ruff_cgx/utils.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
# Module-level configuration for ruff command
1515
_ruff_command: str | None = None
1616

17+
# Module-level cache for isort configuration
18+
_isort_configured_cache: bool | None = None
19+
1720

1821
def set_ruff_command(command: str) -> None:
1922
"""
@@ -229,7 +232,17 @@ def create_virtual_render_content(original_content: str, modified_content: str)
229232

230233

231234
def is_isort_configured() -> bool:
232-
"""Check if 'unsorted-imports' is both enabled and marked as should_fix."""
235+
"""
236+
Check if 'unsorted-imports' is both enabled and marked as should_fix.
237+
238+
Result is cached for the lifetime of the process to avoid repeated subprocess calls.
239+
"""
240+
global _isort_configured_cache
241+
242+
# Return cached result if available
243+
if _isort_configured_cache is not None:
244+
return _isort_configured_cache
245+
233246
result = False
234247
try:
235248
# Print ruff settings
@@ -247,21 +260,27 @@ def is_isort_configured() -> bool:
247260
)
248261

249262
if not enabled_match or not should_fix_match:
250-
return False
251-
252-
# Check that 'unsorted-imports' rule appears in both sections
253-
in_enabled = "unsorted-imports" in enabled_match.group(1)
254-
in_should_fix = "unsorted-imports" in should_fix_match.group(1)
255-
256-
return in_enabled and in_should_fix
263+
result = False
264+
else:
265+
# Check that 'unsorted-imports' rule appears in both sections
266+
in_enabled = "unsorted-imports" in enabled_match.group(1)
267+
in_should_fix = "unsorted-imports" in should_fix_match.group(1)
268+
result = in_enabled and in_should_fix
257269

258270
except Exception:
259-
pass
271+
result = False
272+
273+
# Cache the result
274+
_isort_configured_cache = result
260275
return result
261276

262277

263278
def run_ruff_format(
264-
source: str, *, use_single_quotes: bool = False, check: bool = False
279+
source: str,
280+
*,
281+
use_single_quotes: bool = False,
282+
check: bool = False,
283+
skip_import_sort: bool = False,
265284
) -> str:
266285
"""
267286
Format Python source code using ruff via stdin.
@@ -270,11 +289,12 @@ def run_ruff_format(
270289
source: The Python source code to format
271290
use_single_quotes: If True, configure ruff to use single quotes
272291
check: If True, only check without modifying
292+
skip_import_sort: If True, skip import sorting (useful for template expressions)
273293
274294
Returns:
275295
Formatted Python source code
276296
"""
277-
should_sort_imports = is_isort_configured()
297+
should_sort_imports = not skip_import_sort and is_isort_configured()
278298

279299
# Sort imports first if configured
280300
if should_sort_imports:

0 commit comments

Comments
 (0)