Skip to content

Commit

Permalink
Merge pull request #2264 from correctmost/cm/speed-up-exists-case-sen…
Browse files Browse the repository at this point in the history
…sitive

Speed up exists_case_sensitive calls
  • Loading branch information
staticdev authored Jan 20, 2025
2 parents 86485e2 + b817bc8 commit 90e2c73
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
4 changes: 3 additions & 1 deletion isort/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import sys
from functools import lru_cache
from pathlib import Path
from typing import Any, Dict, Optional, Tuple

Expand Down Expand Up @@ -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.
Expand All @@ -66,7 +68,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
2 changes: 2 additions & 0 deletions tests/unit/test_isort.py
Original file line number Diff line number Diff line change
Expand Up @@ -3066,13 +3066,15 @@ 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")))


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")))
Expand Down

0 comments on commit 90e2c73

Please sign in to comment.