Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/poetry/utils/env/env_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,14 @@ def create_venv(
python.patch_version.to_string(),
)

if create_venv is False:
raise InvalidCurrentPythonVersionError(
self._poetry.package.python_versions,
python.patch_version.to_string(),
"Poetry cannot switch to a compatible Python version because "
"virtualenv creation is disabled.",
)

self._io.write_error_line(
f"<warning>The currently activated Python version {python.patch_version.to_string()} is not"
f" supported by the project ({self._poetry.package.python_versions}).\n"
Expand Down
4 changes: 3 additions & 1 deletion src/poetry/utils/env/python/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ def __init__(self, expected: str, given: str | None = None) -> None:


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

super().__init__(message)
41 changes: 41 additions & 0 deletions tests/utils/env/test_env_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from collections.abc import Iterator
from unittest.mock import MagicMock

from _pytest.monkeypatch import MonkeyPatch
from cleo.io.buffered_io import BufferedIO
from pytest import LogCaptureFixture
from pytest_mock import MockerFixture
Expand Down Expand Up @@ -1069,6 +1070,46 @@ def test_create_venv_fails_if_no_compatible_python_version_could_be_found(
assert m.call_count == 0


def test_create_venv_fails_if_current_python_is_not_supported_without_creating_venv(
manager: EnvManager,
poetry: Poetry,
config: Config,
mocker: MockerFixture,
mocked_python_register: MockedPythonRegister,
monkeypatch: MonkeyPatch,
) -> None:
config.config["virtualenvs"]["create"] = False
monkeypatch.delenv("VIRTUAL_ENV", raising=False)

poetry.package.python_versions = "^3.10"

current_python = mocked_python_register("3.9.0")
compatible_python = mocked_python_register("3.10.0")
mocker.patch(
"poetry.utils.env.env_manager.Python.get_preferred_python",
return_value=current_python,
)
get_compatible_python = mocker.patch(
"poetry.utils.env.env_manager.Python.get_compatible_python",
return_value=compatible_python,
)
build_venv = mocker.patch("poetry.utils.env.EnvManager.build_venv")

with pytest.raises(InvalidCurrentPythonVersionError) as e:
manager.create_venv()

expected_message = (
"Current Python version (3.9.0) is not allowed by the project (^3.10).\n"
'Please change python executable via the "env use" command.\n'
"Poetry cannot switch to a compatible Python version because virtualenv "
"creation is disabled."
)

assert str(e.value) == expected_message
get_compatible_python.assert_not_called()
build_venv.assert_not_called()


@pytest.mark.parametrize("use_poetry_python", [True, False])
def test_create_venv_does_not_try_to_find_compatible_versions_with_executable(
manager: EnvManager,
Expand Down
Loading