Skip to content

Commit 257c576

Browse files
committed
fix(env): fail on incompatible Python without venv creation
1 parent 2f99695 commit 257c576

3 files changed

Lines changed: 61 additions & 1 deletion

File tree

src/poetry/utils/env/env_manager.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,18 @@ def create_venv(
408408
name = self._poetry.package.name
409409

410410
supported_python = self._poetry.package.python_constraint
411+
if create_venv is False:
412+
current_python = Version.parse(
413+
".".join(str(c) for c in env.version_info[:3])
414+
)
415+
if not supported_python.allows(current_python):
416+
raise InvalidCurrentPythonVersionError(
417+
self._poetry.package.python_versions,
418+
str(current_python),
419+
"Poetry cannot switch to a compatible Python version because "
420+
"virtualenv creation is disabled.",
421+
)
422+
411423
if not supported_python.allows(python.patch_version):
412424
# The currently activated or chosen Python version
413425
# is not compatible with the Python constraint specified

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ def __init__(self, expected: str, given: str | None = None) -> None:
3131

3232

3333
class InvalidCurrentPythonVersionError(PythonVersionError):
34-
def __init__(self, expected: str, given: str) -> None:
34+
def __init__(self, expected: str, given: str, note: str | None = None) -> None:
3535
message = (
3636
f"Current Python version ({given}) "
3737
f"is not allowed by the project ({expected}).\n"
3838
'Please change python executable via the "env use" command.'
3939
)
40+
if note is not None:
41+
message = f"{message}\n{note}"
4042

4143
super().__init__(message)

tests/utils/env/test_env_manager.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from poetry.utils.env import EnvManager
2222
from poetry.utils.env import IncorrectEnvError
2323
from poetry.utils.env.env_manager import EnvsFile
24+
from poetry.utils.env.mock_env import MockEnv
2425
from poetry.utils.env.python.exceptions import InvalidCurrentPythonVersionError
2526
from poetry.utils.env.python.exceptions import NoCompatiblePythonVersionFoundError
2627
from poetry.utils.env.python.exceptions import PythonVersionNotFoundError
@@ -32,6 +33,7 @@
3233
from collections.abc import Iterator
3334
from unittest.mock import MagicMock
3435

36+
from _pytest.monkeypatch import MonkeyPatch
3537
from cleo.io.buffered_io import BufferedIO
3638
from pytest import LogCaptureFixture
3739
from pytest_mock import MockerFixture
@@ -1069,6 +1071,50 @@ def test_create_venv_fails_if_no_compatible_python_version_could_be_found(
10691071
assert m.call_count == 0
10701072

10711073

1074+
@pytest.mark.parametrize("use_poetry_python", [True, False])
1075+
def test_create_venv_fails_if_current_python_is_not_supported_without_creating_venv(
1076+
manager: EnvManager,
1077+
poetry: Poetry,
1078+
config: Config,
1079+
mocker: MockerFixture,
1080+
mocked_python_register: MockedPythonRegister,
1081+
monkeypatch: MonkeyPatch,
1082+
use_poetry_python: bool,
1083+
) -> None:
1084+
config.config["virtualenvs"]["create"] = False
1085+
config.config["virtualenvs"]["use-poetry-python"] = use_poetry_python
1086+
monkeypatch.delenv("VIRTUAL_ENV", raising=False)
1087+
1088+
poetry.package.python_versions = "^3.10"
1089+
1090+
current_python = mocked_python_register("3.9.0")
1091+
compatible_python = mocked_python_register("3.10.0")
1092+
mocker.patch.object(manager, "get", return_value=MockEnv(version_info=(3, 9, 0)))
1093+
mocker.patch(
1094+
"poetry.utils.env.env_manager.Python.get_preferred_python",
1095+
return_value=current_python if use_poetry_python else compatible_python,
1096+
)
1097+
get_compatible_python = mocker.patch(
1098+
"poetry.utils.env.env_manager.Python.get_compatible_python",
1099+
return_value=compatible_python,
1100+
)
1101+
build_venv = mocker.patch("poetry.utils.env.EnvManager.build_venv")
1102+
1103+
with pytest.raises(InvalidCurrentPythonVersionError) as e:
1104+
manager.create_venv()
1105+
1106+
expected_message = (
1107+
"Current Python version (3.9.0) is not allowed by the project (^3.10).\n"
1108+
'Please change python executable via the "env use" command.\n'
1109+
"Poetry cannot switch to a compatible Python version because virtualenv "
1110+
"creation is disabled."
1111+
)
1112+
1113+
assert str(e.value) == expected_message
1114+
get_compatible_python.assert_not_called()
1115+
build_venv.assert_not_called()
1116+
1117+
10721118
@pytest.mark.parametrize("use_poetry_python", [True, False])
10731119
def test_create_venv_does_not_try_to_find_compatible_versions_with_executable(
10741120
manager: EnvManager,

0 commit comments

Comments
 (0)