Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up exists_case_sensitive calls #2264

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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 @@ -3064,13 +3064,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