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
17 changes: 16 additions & 1 deletion src/poetry/console/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
31 changes: 31 additions & 0 deletions tests/console/commands/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Comment on lines +1222 to +1233
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (testing): Add a test case for valid constraints with surrounding whitespace

To cover _validate_version_constraint's stripping behavior, please add a case like (" ^1.0 ", "^1.0") to this parametrization so we also test whitespace around a valid constraint, not just pure-whitespace mapping to None.

Suggested change
@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(
@pytest.mark.parametrize(
("constraint", "expected"),
[
("^1.0", "^1.0"),
("==1.2.3", "==1.2.3"),
(">=1,<2", ">=1,<2"),
(" ^1.0 ", "^1.0"),
("", 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)
Loading