Skip to content

fix(init): validate Python version constraint in interactive prompt#10957

Open
JSap0914 wants to merge 1 commit into
python-poetry:mainfrom
JSap0914:fix/validate-python-version-constraint-in-init
Open

fix(init): validate Python version constraint in interactive prompt#10957
JSap0914 wants to merge 1 commit into
python-poetry:mainfrom
JSap0914:fix/validate-python-version-constraint-in-init

Conversation

@JSap0914

Copy link
Copy Markdown

Resolves: #7043 (partially — applies same validation to Python version prompt that #10909 applied to dependency version prompt)

Bug

The interactive Python version prompt in `poetry init` accepted arbitrary strings without validation, writing them verbatim into `pyproject.toml`. For example:

```
Compatible Python versions [>=3.12]: invalid-python
```

would produce `requires-python = "invalid-python"`, only failing at the next `poetry lock`.

PR #10909 added `_validate_version_constraint` to the dependency-version prompt but the Python-version prompt was not updated.

Fix

Apply the same `question.set_validator(self._validate_version_constraint)` and `question.set_max_attempts(3)` to the Compatible Python versions question in `_init_pyproject`, consistent with the dependency-version and author validators in the same command.

Verification

```
pytest tests/console/commands/test_init.py
-k test_python_version_constraint_validated_in_interactive_prompt
```

Before fix: invalid constraint accepted, test fails.
After fix: invalid constraint rejected, user re-prompted, test passes.

AI-assisted development (GitHub Copilot / Claude); logic and test authored by contributor.

  • Added tests for changed code.
  • Updated documentation for changed code.

The interactive Python version prompt in `poetry init` accepted
arbitrary strings without validation, silently writing invalid
constraints like `invalid-python` verbatim into `pyproject.toml`.
This inconsistency existed because PR python-poetry#10909 added
`_validate_version_constraint` to the dependency-version prompt but
left the Python-version prompt unguarded.

Apply the same validator (`_validate_version_constraint`) and the same
`set_max_attempts(3)` to the Compatible Python versions question.  Invalid
constraints now raise a clear error and re-prompt the user, consistent
with the dependency version and author validators in the same command.

Verification:
  pytest tests/console/commands/test_init.py \
      -k test_python_version_constraint_validated_in_interactive_prompt
Copilot AI review requested due to automatic review settings June 17, 2026 07:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've reviewed your changes and they look great!


Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Improve poetry init validate

2 participants