Skip to content

Commit 737a203

Browse files
authored
Cache Path.is_dir calls (#779)
1 parent eb68b29 commit 737a203

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/poetry/core/packages/dependency.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ def create_from_pep_508(
330330
"""
331331
from poetry.core.packages.url_dependency import URLDependency
332332
from poetry.core.packages.utils.link import Link
333+
from poetry.core.packages.utils.utils import cached_is_dir
333334
from poetry.core.packages.utils.utils import is_archive_file
334335
from poetry.core.packages.utils.utils import is_python_project
335336
from poetry.core.packages.utils.utils import is_url
@@ -361,7 +362,7 @@ def create_from_pep_508(
361362
else:
362363
path_str = os.path.normpath(os.path.abspath(name)) # noqa: PTH100
363364
p, extras = strip_extras(path_str)
364-
if p.is_dir() and (os.path.sep in name or name.startswith(".")):
365+
if cached_is_dir(p) and (os.path.sep in name or name.startswith(".")):
365366
if not is_python_project(Path(name)):
366367
raise ValueError(
367368
f"Directory {name!r} is not installable. Not a Python project."

src/poetry/core/packages/utils/utils.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,16 @@ def strip_extras(path: str) -> tuple[Path, str | None]:
117117
return Path(path_no_extras), extras
118118

119119

120+
@functools.lru_cache(maxsize=None)
121+
def cached_is_dir(path: Path) -> bool:
122+
"""A cached version of `Path.is_dir`."""
123+
return path.is_dir()
124+
125+
120126
@functools.lru_cache(maxsize=None)
121127
def is_python_project(path: Path) -> bool:
122128
"""Return true if the directory is a Python project"""
123-
if not path.is_dir():
129+
if not cached_is_dir(path):
124130
return False
125131

126132
setup_py = path / "setup.py"

0 commit comments

Comments
 (0)