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

Cache Path.is_dir calls #779

Merged
merged 4 commits into from
Oct 20, 2024
Merged
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
3 changes: 2 additions & 1 deletion src/poetry/core/packages/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ def create_from_pep_508(
"""
from poetry.core.packages.url_dependency import URLDependency
from poetry.core.packages.utils.link import Link
from poetry.core.packages.utils.utils import cached_is_dir
from poetry.core.packages.utils.utils import is_archive_file
from poetry.core.packages.utils.utils import is_python_project
from poetry.core.packages.utils.utils import is_url
Expand Down Expand Up @@ -361,7 +362,7 @@ def create_from_pep_508(
else:
path_str = os.path.normpath(os.path.abspath(name)) # noqa: PTH100
p, extras = strip_extras(path_str)
if p.is_dir() and (os.path.sep in name or name.startswith(".")):
if cached_is_dir(p) and (os.path.sep in name or name.startswith(".")):
if not is_python_project(Path(name)):
raise ValueError(
f"Directory {name!r} is not installable. Not a Python project."
Expand Down
8 changes: 7 additions & 1 deletion src/poetry/core/packages/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,16 @@ def strip_extras(path: str) -> tuple[Path, str | None]:
return Path(path_no_extras), extras


@functools.lru_cache(maxsize=None)
def cached_is_dir(path: Path) -> bool:
"""A cached version of `Path.is_dir`."""
return path.is_dir()


@functools.lru_cache(maxsize=None)
def is_python_project(path: Path) -> bool:
"""Return true if the directory is a Python project"""
if not path.is_dir():
if not cached_is_dir(path):
return False

setup_py = path / "setup.py"
Expand Down
Loading