Skip to content

Commit f52b710

Browse files
committed
refactor: centralize python installation path logic via PoetryPythonPathProvider
1 parent 6ace575 commit f52b710

File tree

4 files changed

+15
-8
lines changed

4 files changed

+15
-8
lines changed

src/poetry/console/commands/python/list.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
from poetry.core.constraints.version import parse_constraint
99
from poetry.core.version.exceptions import InvalidVersionError
1010

11-
from poetry.config.config import Config
1211
from poetry.console.commands.command import Command
1312
from poetry.utils.env.python import Python
13+
from poetry.utils.env.python.providers import PoetryPythonPathProvider
1414

1515

1616
if TYPE_CHECKING:
@@ -84,7 +84,7 @@ def handle(self) -> int:
8484
)
8585

8686
implementations = {"cpython": "CPython", "pypy": "PyPy"}
87-
python_installation_path = Config.create().python_installation_dir
87+
python_installation_path = PoetryPythonPathProvider.base_installation_dir()
8888

8989
row_count = 0
9090

src/poetry/console/commands/python/remove.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
from poetry.core.constraints.version.version import Version
1111
from poetry.core.version.exceptions import InvalidVersionError
1212

13-
from poetry.config.config import Config
1413
from poetry.console.commands.command import Command
14+
from poetry.utils.env.python.providers import PoetryPythonPathProvider
1515

1616

1717
if TYPE_CHECKING:
@@ -63,7 +63,9 @@ def remove_python_installation(request: str, implementation: str, io: IO) -> int
6363
return 1
6464

6565
request_title = f"<c1>{request}</> (<b>{implementation}</>)"
66-
path = Config.create().python_installation_dir / f"{implementation}@{version}"
66+
path = PoetryPythonPathProvider.installation_dir(
67+
version=version, implementation=implementation
68+
)
6769

6870
if path.exists():
6971
if io.is_verbose():

src/poetry/utils/env/python/installer.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
from poetry.core.constraints.version import Version
1212

13-
from poetry.config.config import Config
1413
from poetry.console.exceptions import ConsoleMessage
1514
from poetry.console.exceptions import PoetryRuntimeError
1615
from poetry.utils.env.python import Python
16+
from poetry.utils.env.python.providers import PoetryPythonPathProvider
1717

1818

1919
if TYPE_CHECKING:
@@ -46,7 +46,8 @@ class PythonInstaller:
4646
implementation: Literal["cpython", "pypy"] = dataclasses.field(default="cpython")
4747
free_threaded: bool = dataclasses.field(default=False)
4848
installation_directory: Path = dataclasses.field(
49-
init=False, default_factory=lambda: Config.create().python_installation_dir
49+
init=False,
50+
default_factory=lambda: PoetryPythonPathProvider.base_installation_dir(),
5051
)
5152

5253
@property

src/poetry/utils/env/python/providers.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,13 @@ def find_python_by_name(cls, name: str) -> findpython.PythonVersion | None:
4141

4242
@dataclasses.dataclass
4343
class PoetryPythonPathProvider(PathProvider): # type: ignore[misc]
44+
@classmethod
45+
def base_installation_dir(cls) -> Path:
46+
return Config.create().python_installation_dir
47+
4448
@classmethod
4549
def installation_dir(cls, version: Version, implementation: str) -> Path:
46-
return Config.create().python_installation_dir / f"{implementation}@{version}"
50+
return cls.base_installation_dir() / f"{implementation}@{version}"
4751

4852
@classmethod
4953
def _make_bin_paths(cls, base: Path | None = None) -> list[Path]:
@@ -55,7 +59,7 @@ def _make_bin_paths(cls, base: Path | None = None) -> list[Path]:
5559
# If both versions exist, the first one is preferred.
5660
# However, sometimes (especially for free-threaded Python),
5761
# only the second version exists!
58-
install_dir = base or Config.create().python_installation_dir
62+
install_dir = base or cls.base_installation_dir()
5963
if WINDOWS and not sysconfig.get_platform().startswith("mingw"):
6064
# On Windows Python executables are top level.
6165
# (Only in virtualenvs, they are in the Scripts directory.)

0 commit comments

Comments
 (0)