poetry icon indicating copy to clipboard operation
poetry copied to clipboard

fix: handle || operator in version constraints for poetry add

Open JaviMaligno opened this issue 2 months ago • 1 comments

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.

JaviMaligno avatar Dec 10 '25 19:12 JaviMaligno

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
https://github.com/python-poetry/poetry/issues/10569 Support parsing of poetry add arguments that contain the ` operator in version constraints (e.g.,cachy>=0.1.0
https://github.com/python-poetry/poetry/issues/10569 Ensure that projects containing dependencies with ` in their version specifiers (e.g.,pytest (>=4,<5

Possibly linked issues

  • #10569: PR changes RequirementsParser to keep || inside version constraints, resolving the invalid requirement errors described.

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

  • Contact our support team for questions or feedback.
  • Visit our documentation for detailed guides and information.
  • Keep in touch with the Sourcery team by following us on X/Twitter, LinkedIn or GitHub.

sourcery-ai[bot] avatar Dec 10 '25 19:12 sourcery-ai[bot]

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 https://github.com/python-poetry/poetry/issues/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 https://github.com/python-poetry/poetry/issues/10569#issuecomment-3339237397.

radoering avatar Dec 13 '25 12:12 radoering