fix(init): validate version constraint in interactive prompt#10909
Open
SarthakB11 wants to merge 1 commit into
Open
fix(init): validate version constraint in interactive prompt#10909SarthakB11 wants to merge 1 commit into
SarthakB11 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
Hey - I've found 1 issue
Prompt for AI Agents
Please address the comments from this code review:
## Individual Comments
### Comment 1
<location path="tests/console/commands/test_init.py" line_range="1222-1233" />
<code_context>
+ [
+ ("^1.0", "^1.0"),
+ ("==1.2.3", "==1.2.3"),
+ (">=1,<2", ">=1,<2"),
+ ("", None),
+ (None, None),
</code_context>
<issue_to_address>
**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`.
```suggestion
@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(
```
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
Comment on lines
+1222
to
+1233
| @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( |
There was a problem hiding this comment.
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( |
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.
Pull Request Check List
Resolves: #8797
The version-constraint prompt in
poetry init(andpoetry add -i) only stripped whitespace, so invalid PEP 440 strings likelatest,1.0,!2.0, or==1.,were accepted and written verbatim intopyproject.toml. The error only surfaced at the nextpoetry lock/install.The validator now calls
poetry-core'sparse_constraintand raisesValueError("Invalid version constraint: <value>")on failure, chained from the underlying parser error viafrom e. The existingset_max_attempts(3)re-prompts the user. Same pattern as_validate_author/_validate_packagein the same file. Blank input still returnsNone(skip).Hard rejection (vs the reporter's suggestion of "ask for confirmation") was chosen for consistency with the two other validators in
init.py. Tests intests/console/commands/test_init.pycover valid, invalid, and blank inputs.