Skip to content

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

Open
SarthakB11 wants to merge 1 commit into
python-poetry:mainfrom
SarthakB11:fix/issue-8797
Open

fix(init): validate version constraint in interactive prompt#10909
SarthakB11 wants to merge 1 commit into
python-poetry:mainfrom
SarthakB11:fix/issue-8797

Conversation

@SarthakB11
Copy link
Copy Markdown
Contributor

Pull Request Check List

Resolves: #8797

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

The version-constraint prompt in poetry init (and poetry add -i) only stripped whitespace, so invalid PEP 440 strings like latest, 1.0,!2.0, or ==1., were accepted and written verbatim into pyproject.toml. The error only surfaced at the next poetry lock / install.

The validator now calls poetry-core's parse_constraint and raises ValueError("Invalid version constraint: <value>") on failure, chained from the underlying parser error via from e. The existing set_max_attempts(3) re-prompts the user. Same pattern as _validate_author / _validate_package in the same file. Blank input still returns None (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 in tests/console/commands/test_init.py cover valid, invalid, and blank inputs.

Copy link
Copy Markdown

@sourcery-ai sourcery-ai Bot left a comment

Choose a reason for hiding this comment

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

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>

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.

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(
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(

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.

Interactive dependency prompt accepts invalid version strings

1 participant