Skip to content
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

fix transitive_marker handling #10141

Merged
merged 2 commits into from
Feb 3, 2025
Merged

Conversation

radoering
Copy link
Member

@radoering radoering commented Feb 3, 2025

Pull Request Check List

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

Without this fix, test_solver_should_not_raise_errors_for_irrelevant_transitive_python_constraints wrongly chooses the later importlib-resources version even though it is not compatible with the project's python constraint depending on which is resolved first, virtualenv or pre-commit, because the transitive marker of the other's dependency on importlib-resources is ignored.

The first commit is just refactoring to facilitate the test, added in the second commit.

Summary by Sourcery

Fix handling of transitive markers during dependency resolution. This addresses an issue where incompatible transitive dependencies could be selected due to incorrect marker handling.

Bug Fixes:

  • Fix an issue where test_solver_should_not_raise_errors_for_irrelevant_transitive_python_constraints would select an incompatible importlib-resources version.

Tests:

  • Add a test to verify the fix for transitive marker handling.

Copy link

sourcery-ai bot commented Feb 3, 2025

Reviewer's Guide by Sourcery

This pull request fixes an issue where the solver was not correctly handling transitive markers, leading to incorrect version resolution in certain scenarios. The changes include refactoring the version selection logic and adding a new test case to verify the fix.

Sequence diagram for package version resolution with transitive markers

sequenceDiagram
    participant Solver as VersionSolver
    participant Solution
    participant Term
    participant Dependency

    Solver->>Solution: Get unsatisfied dependencies
    Solution-->>Solver: Return list of dependencies
    Solver->>Solver: _choose_next(unsatisfied)
    Note over Solver: Select dependency based on preference
    Solver->>Term: Check relation with other terms
    Term->>Term: Compare transitive markers
    Note over Term: New: Consider transitive markers<br/>when checking relations
    Term->>Dependency: Union transitive markers
    Note over Dependency: Merge markers when<br/>combining positive terms
Loading

Class diagram showing the modified version solver components

classDiagram
    class VersionSolver {
        -_solution: Solution
        -_provider: Provider
        +_choose_package_version(): str|None
        +_choose_next(unsatisfied: list[Dependency]): Dependency
        +_resolve_conflict(incompatibility: Incompatibility): Incompatibility
    }

    class Term {
        +dependency: Dependency
        +is_positive: bool
        +_relation(other: Term): str
        +_non_empty_term(other: Term, constraint: Any, is_positive: bool): Term
    }

    class Dependency {
        +transitive_marker: Any
        +complete_name: str
        +with_constraint(constraint: Any): Dependency
    }

    VersionSolver ..> Term: uses
    Term ..> Dependency: contains

    note for Term "Modified to handle transitive markers"
    note for Dependency "Enhanced with transitive marker handling"
Loading

File-Level Changes

Change Details Files
Refactor version selection logic to facilitate testing and improve determinism.
  • Extracted the logic for choosing the next dependency to resolve into a separate _choose_next method.
  • Modified the _choose_package_version method to use the new _choose_next method.
src/poetry/mixology/version_solver.py
Ensure transitive markers are correctly handled during version solving.
  • Added logic to propagate the transitive marker to the selected package in _choose_package_version.
  • Modified the _relation method in Term to consider transitive markers when determining if two terms are disjoint.
  • Modified the _non_empty_term method in Term to merge transitive markers when combining terms.
src/poetry/mixology/version_solver.py
src/poetry/mixology/term.py
Add a new test case to verify the fix for transitive marker handling.
  • Added a new test case test_solver_should_not_raise_errors_for_irrelevant_transitive_python_constraints to reproduce the reported issue.
  • Added a parameterization to the test to verify the fix in different scenarios.
  • Patched the _choose_next method to control the order of dependency resolution for the test.
tests/puzzle/test_solver.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:

  • Please add documentation explaining how transitive markers work and are handled in the dependency resolver. This will help users and contributors understand this important behavior.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟡 Testing: 1 issue found
  • 🟢 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.

tests/puzzle/test_solver.py Show resolved Hide resolved
Without this fix, `test_solver_should_not_raise_errors_for_irrelevant_transitive_python_constraints` wrongly chooses the later `importlib-resources` version even though it is not compatible with the project's python constraint depending on which is resolved first, `virtualenv` or `pre-commit`, because the transitive marker of the other's dependency on `importlib-resources` is ignored.
@radoering radoering force-pushed the fix-transitive-marker branch from b59d634 to 67ded88 Compare February 3, 2025 18:26
@abn abn merged commit 1edf8fb into python-poetry:main Feb 3, 2025
53 checks passed
@finswimmer finswimmer mentioned this pull request Feb 6, 2025
4 tasks
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.

2 participants