Skip to content

Commit 2bd160c

Browse files
committed
chore(env): spread out python manager
1 parent cca78e7 commit 2bd160c

File tree

12 files changed

+90
-74
lines changed

12 files changed

+90
-74
lines changed

src/poetry/console/commands/init.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from poetry.console.commands.command import Command
1818
from poetry.console.commands.env_command import EnvCommand
1919
from poetry.utils.dependency_specification import RequirementsParser
20-
from poetry.utils.env.python_manager import Python
20+
from poetry.utils.env.python import Python
2121

2222

2323
if TYPE_CHECKING:

src/poetry/utils/env/__init__.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
from poetry.utils.env.exceptions import EnvCommandError
1212
from poetry.utils.env.exceptions import EnvError
1313
from poetry.utils.env.exceptions import IncorrectEnvError
14-
from poetry.utils.env.exceptions import InvalidCurrentPythonVersionError
15-
from poetry.utils.env.exceptions import NoCompatiblePythonVersionFoundError
16-
from poetry.utils.env.exceptions import PythonVersionNotFoundError
1714
from poetry.utils.env.generic_env import GenericEnv
1815
from poetry.utils.env.mock_env import MockEnv
1916
from poetry.utils.env.null_env import NullEnv
@@ -102,11 +99,8 @@ def build_environment(
10299
"EnvManager",
103100
"GenericEnv",
104101
"IncorrectEnvError",
105-
"InvalidCurrentPythonVersionError",
106102
"MockEnv",
107-
"NoCompatiblePythonVersionFoundError",
108103
"NullEnv",
109-
"PythonVersionNotFoundError",
110104
"SitePackages",
111105
"SystemEnv",
112106
"VirtualEnv",

src/poetry/utils/env/env_manager.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
from poetry.utils._compat import encode
2525
from poetry.utils.env.exceptions import EnvCommandError
2626
from poetry.utils.env.exceptions import IncorrectEnvError
27-
from poetry.utils.env.exceptions import InvalidCurrentPythonVersionError
28-
from poetry.utils.env.exceptions import NoCompatiblePythonVersionFoundError
29-
from poetry.utils.env.exceptions import PythonVersionNotFoundError
3027
from poetry.utils.env.generic_env import GenericEnv
31-
from poetry.utils.env.python_manager import Python
28+
from poetry.utils.env.python import Python
29+
from poetry.utils.env.python.exceptions import InvalidCurrentPythonVersionError
30+
from poetry.utils.env.python.exceptions import NoCompatiblePythonVersionFoundError
31+
from poetry.utils.env.python.exceptions import PythonVersionNotFoundError
3232
from poetry.utils.env.script_strings import GET_ENV_PATH_ONELINER
3333
from poetry.utils.env.script_strings import GET_PYTHON_VERSION_ONELINER
3434
from poetry.utils.env.system_env import SystemEnv

src/poetry/utils/env/exceptions.py

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -31,39 +31,3 @@ def __init__(self, e: CalledProcessError) -> None:
3131
if e.stderr:
3232
message_parts.append(f"Error output:\n{decode(e.stderr)}")
3333
super().__init__("\n\n".join(message_parts))
34-
35-
36-
class PythonVersionNotFoundError(EnvError):
37-
def __init__(self, expected: str) -> None:
38-
super().__init__(f"Could not find the python executable {expected}")
39-
40-
41-
class NoCompatiblePythonVersionFoundError(EnvError):
42-
def __init__(self, expected: str, given: str | None = None) -> None:
43-
if given:
44-
message = (
45-
f"The specified Python version ({given}) "
46-
f"is not supported by the project ({expected}).\n"
47-
"Please choose a compatible version "
48-
"or loosen the python constraint specified "
49-
"in the pyproject.toml file."
50-
)
51-
else:
52-
message = (
53-
"Poetry was unable to find a compatible version. "
54-
"If you have one, you can explicitly use it "
55-
'via the "env use" command.'
56-
)
57-
58-
super().__init__(message)
59-
60-
61-
class InvalidCurrentPythonVersionError(EnvError):
62-
def __init__(self, expected: str, given: str) -> None:
63-
message = (
64-
f"Current Python version ({given}) "
65-
f"is not allowed by the project ({expected}).\n"
66-
'Please change python executable via the "env use" command.'
67-
)
68-
69-
super().__init__(message)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from __future__ import annotations
2+
3+
from poetry.utils.env.python.manager import Python
4+
5+
6+
__all__ = ["Python"]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from __future__ import annotations
2+
3+
4+
class PythonVersionError(Exception):
5+
pass
6+
7+
8+
class PythonVersionNotFoundError(PythonVersionError):
9+
def __init__(self, expected: str) -> None:
10+
super().__init__(f"Could not find the python executable {expected}")
11+
12+
13+
class NoCompatiblePythonVersionFoundError(PythonVersionError):
14+
def __init__(self, expected: str, given: str | None = None) -> None:
15+
if given:
16+
message = (
17+
f"The specified Python version ({given}) "
18+
f"is not supported by the project ({expected}).\n"
19+
"Please choose a compatible version "
20+
"or loosen the python constraint specified "
21+
"in the pyproject.toml file."
22+
)
23+
else:
24+
message = (
25+
"Poetry was unable to find a compatible version. "
26+
"If you have one, you can explicitly use it "
27+
'via the "env use" command.'
28+
)
29+
30+
super().__init__(message)
31+
32+
33+
class InvalidCurrentPythonVersionError(PythonVersionError):
34+
def __init__(self, expected: str, given: str) -> None:
35+
message = (
36+
f"Current Python version ({given}) "
37+
f"is not allowed by the project ({expected}).\n"
38+
'Please change python executable via the "env use" command.'
39+
)
40+
41+
super().__init__(message)

src/poetry/utils/env/python_manager.py renamed to src/poetry/utils/env/python/manager.py

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import contextlib
4-
import shutil
54
import sys
65

76
from functools import cached_property
@@ -18,30 +17,17 @@
1817
from cleo.io.outputs.output import Verbosity
1918
from poetry.core.constraints.version import Version
2019

21-
from poetry.utils.env.exceptions import NoCompatiblePythonVersionFoundError
20+
from poetry.utils.env.python.exceptions import NoCompatiblePythonVersionFoundError
21+
from poetry.utils.env.python.providers import ShutilWhichPythonProvider
2222

2323

2424
if TYPE_CHECKING:
25-
from collections.abc import Iterable
26-
2725
from cleo.io.io import IO
28-
from typing_extensions import Self
2926

3027
from poetry.config.config import Config
3128
from poetry.poetry import Poetry
3229

3330

34-
class ShutilWhichPythonProvider(findpython.BaseProvider): # type: ignore[misc]
35-
@classmethod
36-
def create(cls) -> Self | None:
37-
return cls()
38-
39-
def find_pythons(self) -> Iterable[findpython.PythonVersion]:
40-
if path := shutil.which("python"):
41-
return [findpython.PythonVersion(executable=Path(path))]
42-
return []
43-
44-
4531
class Python:
4632
@overload
4733
def __init__(self, *, python: findpython.PythonVersion) -> None: ...
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from __future__ import annotations
2+
3+
import shutil
4+
5+
from pathlib import Path
6+
from typing import TYPE_CHECKING
7+
8+
import findpython
9+
10+
11+
if TYPE_CHECKING:
12+
from collections.abc import Iterable
13+
14+
from typing_extensions import Self
15+
16+
17+
class ShutilWhichPythonProvider(findpython.BaseProvider): # type: ignore[misc]
18+
@classmethod
19+
def create(cls) -> Self | None:
20+
return cls()
21+
22+
def find_pythons(self) -> Iterable[findpython.PythonVersion]:
23+
if path := shutil.which("python"):
24+
return [findpython.PythonVersion(executable=Path(path))]
25+
return []

tests/conftest.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
from poetry.utils.env import MockEnv
4646
from poetry.utils.env import SystemEnv
4747
from poetry.utils.env import VirtualEnv
48-
from poetry.utils.env.python_manager import Python
48+
from poetry.utils.env.python import Python
4949
from poetry.utils.password_manager import PoetryKeyring
5050
from tests.helpers import MOCK_DEFAULT_GIT_REVISION
5151
from tests.helpers import TestLocker
@@ -906,7 +906,7 @@ def register(
906906

907907
if make_system:
908908
mocker.patch(
909-
"poetry.utils.env.python_manager.Python.get_system_python",
909+
"poetry.utils.env.python.Python.get_system_python",
910910
return_value=Python(python=python),
911911
)
912912
mocked_pythons_version_map[""] = python
@@ -959,6 +959,6 @@ def with_mocked_findpython(
959959
@pytest.fixture
960960
def with_no_active_python(mocker: MockerFixture) -> MagicMock:
961961
return mocker.patch(
962-
"poetry.utils.env.python_manager.Python.get_active_python",
962+
"poetry.utils.env.python.Python.get_active_python",
963963
return_value=None,
964964
)

tests/types.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from poetry.poetry import Poetry
2828
from poetry.repositories.legacy_repository import LegacyRepository
2929
from poetry.utils.env import Env
30-
from poetry.utils.env.python_manager import Python
30+
from poetry.utils.env.python import Python
3131
from tests.repositories.fixtures.distribution_hashes import DistributionHash
3232

3333
HTTPrettyResponse = tuple[int, dict[str, Any], bytes] # status code, headers, body

0 commit comments

Comments
 (0)