Skip to content

simplify python_version markers #851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

radoering
Copy link
Member

@radoering radoering commented Mar 14, 2025

Resolves: python-poetry/poetry#10249
Requires: python-poetry/poetry#10274

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

Summary by Sourcery

Tests:

  • Adds test cases to verify the correct simplification of python_version markers.

Summary by Sourcery

Tests:

  • Adds test cases to verify the correct simplification of python_version markers.

Copy link

sourcery-ai bot commented Mar 14, 2025

Reviewer's Guide by Sourcery

This pull request simplifies python_version markers by implementing logic to convert unions and intersections of single version markers into range markers where appropriate. It also includes new test cases to verify the correct simplification and addresses a potential combinatorial explosion issue.

Sequence diagram for union simplification of python_version markers

sequenceDiagram
  participant MultiMarker
  participant SingleMarker
  participant VersionRange

  MultiMarker->>SingleMarker: union(other: SingleMarker)
  SingleMarker->>SingleMarker: get_python_constraint_from_marker()
  SingleMarker->>VersionRange: allows_all(constraint)
  alt other_constraint.allows_all(constraint)
    SingleMarker-->>MultiMarker: return other
  else
    SingleMarker-->>MultiMarker: return None
  end
Loading

Sequence diagram for intersect simplification of python_version markers

sequenceDiagram
  participant MarkerUnion
  participant SingleMarker
  participant VersionRange

  MarkerUnion->>SingleMarker: intersect(other: SingleMarker)
  SingleMarker->>SingleMarker: get_python_constraint_from_marker()
  SingleMarker->>VersionRange: allows_all(other_constraint)
  alt constraint.allows_all(other_constraint)
    SingleMarker-->>MarkerUnion: return other
  else
    SingleMarker-->>MarkerUnion: return None
  end
Loading

Updated class diagram for BaseMarker and related classes

classDiagram
  class BaseMarker {
    +complexity: Tuple[int, int]
    +union(other: BaseMarker): BaseMarker | None
    +intersect(other: BaseMarker): BaseMarker | None
  }
  class MultiMarker {
    -markers: Set[BaseMarker]
    +of(*markers: BaseMarker): BaseMarker
  }
  class MarkerUnion {
    -markers: Set[BaseMarker]
    +of(*markers: BaseMarker): BaseMarker
  }
  class SingleMarkerLike {
    +name: str
    +constraint: Constraint
  }
  class SingleMarker {
    +name: str
    +constraint: Constraint
  }
  class AnyMarker
  class EmptyMarker

  BaseMarker <|-- MultiMarker
  BaseMarker <|-- MarkerUnion
  BaseMarker <|-- SingleMarkerLike
  SingleMarkerLike <|-- SingleMarker
  BaseMarker <|-- AnyMarker
  BaseMarker <|-- EmptyMarker

  MultiMarker *-- BaseMarker : contains
  MarkerUnion *-- BaseMarker : contains

  note for BaseMarker "Added union and intersect methods for simplification"
  note for MultiMarker "Added of method for simplification"
  note for MarkerUnion "Added of method for simplification"
Loading

File-Level Changes

Change Details Files
Added new test cases to verify the correct simplification of python_version markers.
  • Added test cases for equivalent ranges, ranges with the same start, adjacent ranges, overlapping ranges, and complex scenarios.
  • Included tests for cases that previously caused endless recursion.
  • Added a test case to address a real-world example from issue #10250, which involves complex intersections and potential combinatorial explosions.
tests/version/test_markers.py
Implemented simplification logic for python_version markers in union_simplify and intersect_simplify methods.
  • Added logic to union_simplify to convert unions of single python_version markers into a range marker when appropriate.
  • Added logic to intersect_simplify to simplify intersections involving python_version markers.
  • Modified union_simplify to handle cases where the union of unique markers results in a simple marker or a multi-marker with low complexity.
  • Modified intersect_simplify to handle cases where the intersection of unique markers results in a simple marker or a multi-marker with low complexity.
src/poetry/core/version/markers.py
Implemented a utility function _unique_product to remove duplicates from itertools.product while maintaining order.
  • Added _unique_product function to filter out duplicate combinations of markers during CNF/DNF conversion to prevent combinatorial explosions.
src/poetry/core/version/markers.py
Updated test cases to reflect changes in python_version marker simplification.
  • Modified the expected marker string in test_dependency_from_pep_508_with_python_version to reflect the new simplification logic.
tests/packages/test_main.py
tests/packages/utils/test_utils.py

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!
  • Generate a plan of action for an issue: Comment @sourcery-ai plan on
    an issue to generate a plan of action for it.

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

Overall Comments:

  • It looks like you're converting python_version == "3.x" or python_version == "3.y" to python_version >= "3.x" and python_version < "3.(y+1)" - can you confirm this is the expected behavior?
  • It might be helpful to add a comment explaining the logic behind the changes in _merge_single_markers.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

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.

@radoering radoering force-pushed the simplify-python-version-markers branch from b10fdee to faad967 Compare March 14, 2025 16:15
@radoering
Copy link
Member Author

  • It looks like you're converting python_version == "3.x" or python_version == "3.y" to python_version >= "3.x" and python_version < "3.(y+1)" - can you confirm this is the expected behavior?

More exactly, python_version == "3.x" or python_version == "3.(x+1)" is converted to python_version >= "3.x" and python_version < "3.(x+2)". This is expected behavior.

  • It might be helpful to add a comment explaining the logic behind the changes in _merge_single_markers.

Good point. I added a comment.

@radoering radoering marked this pull request as draft March 14, 2025 16:35
@radoering radoering force-pushed the simplify-python-version-markers branch from faad967 to a6d12a4 Compare March 15, 2025 13:36
@radoering radoering marked this pull request as ready for review March 15, 2025 14:00
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 @radoering - I've reviewed your changes - here's some feedback:

Overall Comments:

  • It looks like you're adding a lot of new tests, which is great, but some of them seem to have platform-specific conditions combined with python version, which might make them harder to read and maintain.
  • Consider adding a helper function to simplify the creation of MultiMarker instances in the tests, as there are several places where they are constructed manually.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟡 Complexity: 1 issue found
  • 🟢 Documentation: all looks good

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.

@radoering radoering marked this pull request as draft March 15, 2025 14:27
Improve recognization and merging of adjacent ranges.
radoering added a commit to radoering/poetry-core that referenced this pull request Mar 16, 2025
@radoering radoering force-pushed the simplify-python-version-markers branch from a6d12a4 to b365852 Compare March 16, 2025 15:41
@radoering radoering force-pushed the simplify-python-version-markers branch from b365852 to f984002 Compare March 16, 2025 15:47
@radoering radoering marked this pull request as ready for review March 16, 2025 16:22
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 @radoering - I've reviewed your changes - here's some feedback:

Overall Comments:

  • It looks like you're adding a lot of new tests, which is great, but some of them seem to be testing the same scenarios - can you reduce the redundancy?
  • The change to test_dependency_from_pep_508_with_python_version in tests/packages/test_main.py seems related, but it's not immediately clear why it's needed - can you add a comment explaining the change?
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

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.

@radoering
Copy link
Member Author

  • It looks like you're adding a lot of new tests, which is great, but some of them seem to be testing the same scenarios - can you reduce the redundancy?

Which tests are redundant?

  • The change to test_dependency_from_pep_508_with_python_version in tests/packages/test_main.py seems related, but it's not immediately clear why it's needed - can you add a comment explaining the change?

The change is necessary because otherwise the test will fail.

@radoering radoering requested a review from a team March 16, 2025 16:29
@radoering radoering merged commit 7ae0786 into python-poetry:main Mar 25, 2025
18 checks passed
radoering added a commit that referenced this pull request Mar 25, 2025
Improve recognization and merging of adjacent ranges.
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.

markers in poetry.lock are not simplified
2 participants