From 1ebb2d5c97efebd7b9ed8e7c10389b9182c1457d Mon Sep 17 00:00:00 2001 From: SarthakB11 Date: Sun, 17 May 2026 18:37:54 +0000 Subject: [PATCH 1/3] fix(init): validate version constraint in interactive prompt Resolves #8797 --- src/poetry/console/commands/init.py | 17 +++++++++++++++- tests/console/commands/test_init.py | 31 +++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 1 deletion(-) diff --git a/src/poetry/console/commands/init.py b/src/poetry/console/commands/init.py index 48ea6e206ed..ca8c87710ac 100644 --- a/src/poetry/console/commands/init.py +++ b/src/poetry/console/commands/init.py @@ -382,7 +382,7 @@ def _determine_requirements( "(or leave blank to use the latest version):" ) question.set_max_attempts(3) - question.set_validator(lambda x: (x or "").strip() or None) + question.set_validator(self._validate_version_constraint) package_constraint = self.ask(question) @@ -521,6 +521,21 @@ def _validate_package(package: str | None) -> str | None: return package + @staticmethod + def _validate_version_constraint(constraint: str | None) -> str | None: + from poetry.core.constraints.version import parse_constraint + + constraint = (constraint or "").strip() or None + if constraint is None: + return None + + try: + parse_constraint(constraint) + except ValueError as e: + raise ValueError(f"Invalid version constraint: {constraint}") from e + + return constraint + def _get_pool(self) -> RepositoryPool: from poetry.config.config import Config from poetry.repositories import RepositoryPool diff --git a/tests/console/commands/test_init.py b/tests/console/commands/test_init.py index 7585740edec..020ce0e1342 100644 --- a/tests/console/commands/test_init.py +++ b/tests/console/commands/test_init.py @@ -1217,3 +1217,34 @@ def test_init_does_not_add_readme_key_when_readme_missing( # Assert pyproject = (tmp_path / "pyproject.toml").read_text(encoding="utf-8") assert "readme =" not in pyproject + + +@pytest.mark.parametrize( + ("constraint", "expected"), + [ + ("^1.0", "^1.0"), + ("==1.2.3", "==1.2.3"), + (">=1,<2", ">=1,<2"), + ("", None), + (None, None), + (" ", None), + ], +) +def test_validate_version_constraint_accepts_valid_inputs( + constraint: str | None, expected: str | None +) -> None: + assert InitCommand._validate_version_constraint(constraint) == expected + + +@pytest.mark.parametrize( + "invalid", + [ + "latest", + "isort", + "==1.,", + "not-a-version", + ], +) +def test_validate_version_constraint_rejects_invalid_inputs(invalid: str) -> None: + with pytest.raises(ValueError, match="Invalid version constraint"): + InitCommand._validate_version_constraint(invalid) From 6bdd4838d116cfa2a71f9fc883b261552b67dff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Sun, 24 May 2026 17:32:30 +0200 Subject: [PATCH 2/3] simplify and add test --- src/poetry/console/commands/init.py | 4 ++-- tests/console/commands/test_init.py | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/poetry/console/commands/init.py b/src/poetry/console/commands/init.py index ca8c87710ac..a0921fdd1eb 100644 --- a/src/poetry/console/commands/init.py +++ b/src/poetry/console/commands/init.py @@ -525,8 +525,8 @@ def _validate_package(package: str | None) -> str | None: def _validate_version_constraint(constraint: str | None) -> str | None: from poetry.core.constraints.version import parse_constraint - constraint = (constraint or "").strip() or None - if constraint is None: + constraint = (constraint or "").strip() + if not constraint: return None try: diff --git a/tests/console/commands/test_init.py b/tests/console/commands/test_init.py index 020ce0e1342..5ce9763ad2d 100644 --- a/tests/console/commands/test_init.py +++ b/tests/console/commands/test_init.py @@ -1223,6 +1223,7 @@ def test_init_does_not_add_readme_key_when_readme_missing( ("constraint", "expected"), [ ("^1.0", "^1.0"), + (" ^1.0 ", "^1.0"), ("==1.2.3", "==1.2.3"), (">=1,<2", ">=1,<2"), ("", None), From 1f359d1828519d7a4b809ac9d1b8d97abc4ded52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Randy=20D=C3=B6ring?= <30527984+radoering@users.noreply.github.com> Date: Sun, 24 May 2026 17:36:11 +0200 Subject: [PATCH 3/3] move tests to other validate tests --- tests/console/commands/test_init.py | 64 ++++++++++++++--------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/tests/console/commands/test_init.py b/tests/console/commands/test_init.py index 5ce9763ad2d..935f6d1d9cb 100644 --- a/tests/console/commands/test_init.py +++ b/tests/console/commands/test_init.py @@ -1016,6 +1016,38 @@ def test_validate_package_invalid(name: str) -> None: assert InitCommand._validate_package(name) +@pytest.mark.parametrize( + ("constraint", "expected"), + [ + ("^1.0", "^1.0"), + (" ^1.0 ", "^1.0"), + ("==1.2.3", "==1.2.3"), + (">=1,<2", ">=1,<2"), + ("", None), + (None, None), + (" ", None), + ], +) +def test_validate_version_constraint_accepts_valid_inputs( + constraint: str | None, expected: str | None +) -> None: + assert InitCommand._validate_version_constraint(constraint) == expected + + +@pytest.mark.parametrize( + "invalid", + [ + "latest", + "isort", + "==1.,", + "not-a-version", + ], +) +def test_validate_version_constraint_rejects_invalid_inputs(invalid: str) -> None: + with pytest.raises(ValueError, match="Invalid version constraint"): + InitCommand._validate_version_constraint(invalid) + + @pytest.mark.parametrize( "author", [ @@ -1217,35 +1249,3 @@ def test_init_does_not_add_readme_key_when_readme_missing( # Assert pyproject = (tmp_path / "pyproject.toml").read_text(encoding="utf-8") assert "readme =" not in pyproject - - -@pytest.mark.parametrize( - ("constraint", "expected"), - [ - ("^1.0", "^1.0"), - (" ^1.0 ", "^1.0"), - ("==1.2.3", "==1.2.3"), - (">=1,<2", ">=1,<2"), - ("", None), - (None, None), - (" ", None), - ], -) -def test_validate_version_constraint_accepts_valid_inputs( - constraint: str | None, expected: str | None -) -> None: - assert InitCommand._validate_version_constraint(constraint) == expected - - -@pytest.mark.parametrize( - "invalid", - [ - "latest", - "isort", - "==1.,", - "not-a-version", - ], -) -def test_validate_version_constraint_rejects_invalid_inputs(invalid: str) -> None: - with pytest.raises(ValueError, match="Invalid version constraint"): - InitCommand._validate_version_constraint(invalid)