Skip to content

Conversation

@radoering
Copy link
Member

@radoering radoering commented Dec 20, 2025

Currently, this is only relevant for poetry-plugin-export because in poetry itself, we only evaluate the marker.

See python-poetry/poetry-plugin-export#334 (comment) ff.

Pull Request Check List

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

Summary by Sourcery

Ensure deterministic ordering of dependency groups and their markers when generating transitive package metadata.

Bug Fixes:

  • Fix non-deterministic marker generation by sorting dependency groups before combining group markers.

Enhancements:

  • Introduce and reuse a shared group sorting helper that prioritizes the main group for consistent output ordering.

Tests:

  • Update transitive package marker tests to use sets for groups and validate deterministic marker combinations regardless of input order.

@sourcery-ai
Copy link

sourcery-ai bot commented Dec 20, 2025

Reviewer's guide (collapsed on small PRs)

Reviewer's Guide

Makes environment marker derivation and serialization deterministic by consistently sorting groups (prioritizing 'main') and aligning tests with set-based group collections.

Sequence diagram for deterministic marker derivation and serialization

sequenceDiagram
    participant Locker
    participant TransitivePackageInfo
    participant group_sort_key
    participant Marker as BaseMarker

    Locker->>TransitivePackageInfo: get_marker(groups)
    activate TransitivePackageInfo
    TransitivePackageInfo->>group_sort_key: sort(groups, key=group_sort_key)
    group_sort_key-->>TransitivePackageInfo: (group != main, group) for each group
    TransitivePackageInfo->>Marker: union markers in sorted group order
    TransitivePackageInfo-->>Locker: combined BaseMarker
    deactivate TransitivePackageInfo

    Note over Locker,TransitivePackageInfo: In _dump_package
    Locker->>group_sort_key: sort(transitive_info.groups, key=group_sort_key)
    group_sort_key-->>Locker: (group != main, group) for each group
    Locker->>group_sort_key: sort(transitive_info.markers.items(), key=lambda x: group_sort_key(x[0]))
    group_sort_key-->>Locker: (group != main, group) for each marker group
    Locker->>Locker: serialize deterministic groups and markers
Loading

Class diagram for deterministic marker grouping and sorting

classDiagram
    class TransitivePackageInfo {
        int depth
        set~NormalizedName~ groups
        dict~NormalizedName, BaseMarker~ markers
        get_marker(groups Iterable_NormalizedName_) BaseMarker
    }

    class group_sort_key {
        <<function>>
        group_sort_key(group NormalizedName) tuple_bool_NormalizedName_
    }

    class BaseMarker {
    }

    class EmptyMarker {
    }

    TransitivePackageInfo ..> BaseMarker : uses
    TransitivePackageInfo ..> EmptyMarker : uses
    TransitivePackageInfo ..> group_sort_key : uses for sorting groups

    class Locker {
        _dump_package(package, transitive_info TransitivePackageInfo, data) void
    }

    Locker ..> TransitivePackageInfo : reads groups, markers
    Locker ..> group_sort_key : sorts groups and marker items
Loading

File-Level Changes

Change Details Files
Make transitive package marker combination order deterministic by sorting groups with a dedicated key function.
  • Introduce a reusable group_sort_key helper that orders groups with 'main' first, then lexicographically by name.
  • Update TransitivePackageInfo.get_marker to iterate over groups using sorted(groups, key=group_sort_key) so marker unions follow a stable order.
src/poetry/packages/transitive_package_info.py
Ensure locker uses the same group sorting strategy when dumping package metadata and markers.
  • Replace inline lambda for sorting transitive_info.groups with the shared group_sort_key helper for consistent ordering between computation and serialization.
  • Adjust sorting of transitive_info.markers.items() to use group_sort_key on the key part, keeping marker tables ordered deterministically.
src/poetry/packages/locker.py
Align tests with the new deterministic group handling by using sets to model groups.
  • Change parameterized test inputs for groups from lists to sets to better reflect unordered group collections and validate deterministic output under varying input order.
  • Update test_get_marker type annotation from list[str] to set[str] to match the new set-based test inputs.
tests/packages/test_transitive_package_info.py

Possibly linked issues

  • #unknown: The PR fixes non-deterministic marker generation in lock/export, which causes the inaccurate markers reported in the issue.

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 - I've reviewed your changes and they look great!


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.

branchv
branchv previously approved these changes Dec 20, 2025
Currently, this is only relevant for poetry-plugin-export because in poetry itself, we only evaluate the marker.
@radoering radoering merged commit e8a05b7 into python-poetry:main Dec 22, 2025
54 checks passed
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