From 3648c7de91df37da2eb0a58ba37d28bb60bbbcef Mon Sep 17 00:00:00 2001 From: correctmost <134317971+correctmost@users.noreply.github.com> Date: Mon, 13 May 2024 19:58:55 -0400 Subject: [PATCH 1/2] Avoid platform checks when a file doesn't exist --- isort/utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/isort/utils.py b/isort/utils.py index 339c86f6a..8bf303551 100644 --- a/isort/utils.py +++ b/isort/utils.py @@ -66,7 +66,7 @@ def exists_case_sensitive(path: str) -> bool: Python can only import using the case of the real file. """ result = os.path.exists(path) - if (sys.platform.startswith("win") or sys.platform == "darwin") and result: # pragma: no cover + if result and (sys.platform.startswith("win") or sys.platform == "darwin"): # pragma: no cover directory, basename = os.path.split(path) result = basename in os.listdir(directory) return result From 9e2e37113c9ffb7b853a6579016c00d60d10a3c0 Mon Sep 17 00:00:00 2001 From: correctmost <134317971+correctmost@users.noreply.github.com> Date: Mon, 13 May 2024 20:00:22 -0400 Subject: [PATCH 2/2] Cache exists_case_sensitive calls to avoid repeated stats --- isort/utils.py | 2 ++ tests/unit/test_isort.py | 2 ++ 2 files changed, 4 insertions(+) diff --git a/isort/utils.py b/isort/utils.py index 8bf303551..cb206d374 100644 --- a/isort/utils.py +++ b/isort/utils.py @@ -1,5 +1,6 @@ import os import sys +from functools import lru_cache from pathlib import Path from typing import Any, Dict, Optional, Tuple @@ -58,6 +59,7 @@ def search(self, filename: str) -> Tuple[str, Dict[str, Any]]: return last_stored_config +@lru_cache(maxsize=1000) def exists_case_sensitive(path: str) -> bool: """Returns if the given path exists and also matches the case on Windows. diff --git a/tests/unit/test_isort.py b/tests/unit/test_isort.py index 7b6743c7b..b9d5df704 100644 --- a/tests/unit/test_isort.py +++ b/tests/unit/test_isort.py @@ -3064,6 +3064,7 @@ def test_third_party_case_sensitive() -> None: def test_exists_case_sensitive_file(tmpdir) -> None: """Test exists_case_sensitive function for a file.""" + exists_case_sensitive.cache_clear() tmpdir.join("module.py").ensure(file=1) assert exists_case_sensitive(str(tmpdir.join("module.py"))) assert not exists_case_sensitive(str(tmpdir.join("MODULE.py"))) @@ -3071,6 +3072,7 @@ def test_exists_case_sensitive_file(tmpdir) -> None: def test_exists_case_sensitive_directory(tmpdir) -> None: """Test exists_case_sensitive function for a directory.""" + exists_case_sensitive.cache_clear() tmpdir.join("pkg").ensure(dir=1) assert exists_case_sensitive(str(tmpdir.join("pkg"))) assert not exists_case_sensitive(str(tmpdir.join("PKG")))