fix(init): validate Python version constraint in interactive prompt#10957
Open
JSap0914 wants to merge 1 commit into
Open
fix(init): validate Python version constraint in interactive prompt#10957JSap0914 wants to merge 1 commit into
JSap0914 wants to merge 1 commit into
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.