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
1821def set_ruff_command (command : str ) -> None :
1922 """
@@ -229,7 +232,17 @@ def create_virtual_render_content(original_content: str, modified_content: str)
229232
230233
231234def 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
263278def 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