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
2 changes: 2 additions & 0 deletions src/poetry/console/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ def _init_pyproject(
f"Compatible Python versions [<comment>{python}</comment>]: ",
default=python,
)
question.set_validator(self._validate_version_constraint)
question.set_max_attempts(3)
python = self.ask(question)

if is_interactive:
Expand Down
23 changes: 23 additions & 0 deletions tests/console/commands/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,29 @@ def test_validate_version_constraint_rejects_invalid_inputs(invalid: str) -> Non
InitCommand._validate_version_constraint(invalid)


def test_python_version_constraint_validated_in_interactive_prompt(
tester: CommandTester,
) -> None:
"""Invalid Python version constraints should be rejected and re-prompted."""
inputs = [
"my-package", # Package name
"1.2.3", # Version
"", # Description
"n", # Author
"", # License
"invalid-python", # Python version -- INVALID, gets rejected
">=3.8", # Python version -- VALID on retry
"n", # Interactive packages
"n", # Interactive dev packages
"\n", # Generate
]
tester.execute(inputs="\n".join(inputs))
output = tester.io.fetch_output()

assert 'requires-python = ">=3.8"' in output
assert 'requires-python = "invalid-python"' not in output


@pytest.mark.parametrize(
"author",
[
Expand Down
Loading