Skip to content

Conversation

@JaviMaligno
Copy link
Contributor

@JaviMaligno JaviMaligno commented Dec 10, 2025

Summary

Fixes #10569

  • Updated RequirementsParser._parse_simple to prioritize regex matching for standard constraints (e.g., >=1.0) before checking for spaces to split the string
  • This ensures that constraints containing spaces (like ||) are captured as a single version string associated with the package name, rather than incorrectly splitting the package name

Test Plan

  • Added comprehensive test cases in tests/utils/test_issue_10569.py covering:
    • poetry add "cachy>=0.1.0 || <0.3.0" - the original issue case
    • Various other constraint formats (>=, <=, >, <, !=, ~, ^)
    • Constraints with extras
    • Comma-separated constraints
  • All existing tests in tests/utils/test_dependency_specification.py pass
  • All existing tests in tests/console/commands/test_add.py pass

🤖 Generated with Claude Code

Summary by Sourcery

Handle version constraints with the || operator in requirement strings without breaking existing formats in the requirements parser.

Bug Fixes:

  • Correct parsing of requirements where the version constraint contains the || operator so the entire constraint is associated with the package name instead of being split incorrectly.

Tests:

  • Add regression tests for requirements containing || operators, extras, and various comparator formats to ensure correct parsing and prevent future regressions.

@sourcery-ai
Copy link

sourcery-ai bot commented Dec 10, 2025

Reviewer's Guide

Adjusts dependency requirement parsing to correctly treat spaced version constraints with operators like || as a single constraint string, and adds regression tests for issue #10569 to ensure existing formats remain supported.

File-Level Changes

Change Details Files
Prioritize regex-based parsing of standard version constraints before space-based splitting in RequirementsParser._parse_simple to correctly handle spaced constraints like those using ` `.
Add regression tests to cover ` ` constraints and verify that existing requirement formats are not broken by the new parsing logic.

Assessment against linked issues

Issue Objective Addressed Explanation
#10569 Support parsing of poetry add arguments that contain the ` operator in version constraints (e.g.,cachy>=0.1.0
#10569 Ensure that projects containing dependencies with ` in their version specifiers (e.g.,pytest (>=4,<5

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@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 there - I've reviewed your changes - here's some feedback:

  • The extras regex character class r"\[([\w\d,-_]+)\]$" is a bit confusing and potentially error-prone (e.g. \d is redundant and the - is in the middle of the class); consider simplifying to something like r"\[([-\w,]+)\]$" or a clearly ordered character class to avoid unintended ranges.
  • The logic for extracting extras from the name is now duplicated in both the initial constraint-regex branch and the space-based fallback; consider factoring this into a small helper to keep the parsing behavior consistent and easier to maintain.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The extras regex character class `r"\[([\w\d,-_]+)\]$"` is a bit confusing and potentially error-prone (e.g. `\d` is redundant and the `-` is in the middle of the class); consider simplifying to something like `r"\[([-\w,]+)\]$"` or a clearly ordered character class to avoid unintended ranges.
- The logic for extracting extras from the name is now duplicated in both the initial constraint-regex branch and the space-based fallback; consider factoring this into a small helper to keep the parsing behavior consistent and easier to maintain.

## Individual Comments

### Comment 1
<location> `tests/utils/test_issue_10569.py:25` </location>
<code_context>
+    ("requirement", "expected"),
+    [
+        # Issue #10569: || operator with spaces
+        (
+            "cachy>=0.1.0 || <0.3.0",
+            {"name": "cachy", "version": ">=0.1.0 || <0.3.0"},
</code_context>

<issue_to_address>
**suggestion (testing):** Add tests for `||` constraints without surrounding spaces and with irregular spacing

Since the regression is about handling `||` with varying spacing, the current parametrization is a bit narrow. Please extend the test matrix to include examples like:

- `"cachy>=0.1.0||<0.3.0"` (no spaces)
- `"cachy>=0.1.0  ||  <0.3.0"` (extra spaces)
- `"cachy >=0.1.0 ||<0.3.0"` (asymmetric spacing)

This will better protect the regex-based behavior and ensure the fallback space-splitting logic does not mis-handle these variants.
</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.

("requirement", "expected"),
[
# Issue #10569: || operator with spaces
(
Copy link

Choose a reason for hiding this comment

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

suggestion (testing): Add tests for || constraints without surrounding spaces and with irregular spacing

Since the regression is about handling || with varying spacing, the current parametrization is a bit narrow. Please extend the test matrix to include examples like:

  • "cachy>=0.1.0||<0.3.0" (no spaces)
  • "cachy>=0.1.0 || <0.3.0" (extra spaces)
  • "cachy >=0.1.0 ||<0.3.0" (asymmetric spacing)

This will better protect the regex-based behavior and ensure the fallback space-splitting logic does not mis-handle these variants.

@radoering
Copy link
Member

I am afraid you may be on the wrong track. I have not checked in detail but if I grasp your fix correctly, you are trying to make reading constraints more tolerant. However, the issue in #10569 is that we write invalid constraints. Even if Poetry would be able to read them they are still invalid and other tools would fail (|| is not allowed in project.dependencies). See also #10569 (comment).

…oetry#10569)

Union constraints (e.g., `^4|^6`) cannot be represented in PEP 508 format
because the `||` operator is not part of the standard. When writing such
constraints to `project.dependencies` or `dependency-groups`, Poetry would
produce invalid entries like `pytest (>=4,<5 || >=6,<7)` which would then
fail to parse on subsequent commands.

This fix:
- Detects when a dependency constraint is a VersionUnion
- Skips writing to project.dependencies/dependency-groups for such constraints
- Shows a warning explaining the constraint uses union syntax
- Writes only to tool.poetry.dependencies where the Poetry-specific syntax is valid

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <[email protected]>
@JaviMaligno
Copy link
Contributor Author

PR Rewritten with Correct Approach

Thanks @radoering for pointing out that the issue is about writing invalid constraints, not reading them.

What Changed

The previous approach tried to make Poetry more tolerant when reading || constraints. This was wrong because:

  1. || is not valid in PEP 508 (used by project.dependencies and dependency-groups)
  2. Even if Poetry could read them, other tools would fail

New Approach

This PR now:

  1. Detects when a constraint is a VersionUnion (e.g., ^4|^6>=4,<5 || >=6,<7)
  2. Skips writing to project.dependencies or dependency-groups for such constraints
  3. Shows a warning explaining that union syntax cannot be represented in PEP 508
  4. Writes only to tool.poetry.dependencies where Poetry's union syntax is valid

Example

$ poetry add 'pytest@^4|^6'

The constraint ^4|^6 for pytest uses union syntax (|) which cannot be represented in PEP 508 format.
Adding to [tool.poetry.dependencies] only.

Result in pyproject.toml:

[tool.poetry.dependencies]
pytest = "^4|^6"

# NOT added to project.dependencies (which would produce invalid PEP 508)

Tests

Added two tests covering:

  • project.dependencies (main dependencies)
  • dependency-groups (PEP 735 groups)

All 86 tests in test_add.py pass.

bug_fix:
  - <
code_change:
  - Hey there - I've reviewed your changes - here's some feedback:
logic:
  - **suggestion (testing):** Add tests for `||` constraints without surrounding spaces and with irre...
general:
  - I am afraid you may be on the wrong track
style:
  - ## PR Rewritten with Correct Approach
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.

poetry add with || operator breaks further poetry commands

3 participants