Skip to content

Commit

Permalink
Cache Path.is_dir calls (#779)
Browse files Browse the repository at this point in the history
  • Loading branch information
ethantkoenig authored Oct 20, 2024
1 parent eb68b29 commit 737a203
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
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

0 comments on commit 737a203

Please sign in to comment.