Skip to content

fix: ensure all poetry-dependencies are added to lock file (#9959) #810

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

Closed
wants to merge 1 commit into from

Conversation

finswimmer
Copy link
Member

@finswimmer finswimmer commented Jan 8, 2025

Resolves: python-poetry/poetry#9959

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

Summary by Sourcery

Bug Fixes:

  • Resolve an issue where not all poetry dependencies were added to the lock file.

Copy link

sourcery-ai bot commented Jan 8, 2025

Reviewer's Guide by Sourcery

This pull request fixes a bug where dependencies defined in the tool.poetry.dependencies section of pyproject.toml were not always included in the lock file. It does so by ensuring that all dependencies are added to the list of dependencies to be locked.

Sequence diagram for dependency locking process

sequenceDiagram
    participant DG as DependencyGroup
    participant Lock as LockProcess

DG->>DG: dependencies_for_locking()
    Note over DG: Create poetry_dependencies_by_name dict

    loop For each dependency
        alt dependency name in poetry_dependencies_by_name
            Note over DG: Check for enrichment with poetry deps
            alt enrichment possible
                DG->>DG: Enrich dependency
                DG->>DG: Track enriched poetry dependency
            else no enrichment
                DG->>DG: Keep original dependency
            end
        else dependency not in poetry_dependencies
            DG->>DG: Keep original dependency
        end
    end

    loop For remaining poetry dependencies
        Note over DG: Add non-enriched poetry dependencies
        DG->>DG: Add to dependencies list
    end

DG-->>Lock: Return complete dependencies list
Loading

Class diagram for DependencyGroup changes

classDiagram
    class DependencyGroup {
        -_dependencies: list[Dependency]
        -_poetry_dependencies: list[Dependency]
        +dependencies_for_locking() list[Dependency]
        +is_optional() bool
    }
    note for DependencyGroup "Modified to ensure all poetry
dependencies are included
in final lock list"
Loading

File-Level Changes

Change Details Files
Ensured that dependencies defined in the tool.poetry.dependencies section are included in the lock file.
  • Added logic to include dependencies from the _poetry_dependencies attribute if they are not already present in the list of dependencies to be locked.
  • Created a list of enriched poetry dependencies to keep track of which dependencies have already been processed.
  • Iterated through the _poetry_dependencies attribute and added any dependencies that were not already in the enriched list to the list of dependencies to be locked.
src/poetry/core/packages/dependency_group.py
Added tests to verify that dependencies defined in the tool.poetry.dependencies section are included in the lock file.
  • Added a new test case to test_dependencies_for_locking that covers the scenario where a dependency is defined in both the dependencies and tool.poetry.dependencies sections.
  • The new test case asserts that the dependency is included in the lock file even if it is also defined in the dependencies section.
tests/packages/test_dependency_group.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.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a 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. You can also use
    this command to specify where the summary should be inserted.

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

@finswimmer finswimmer marked this pull request as ready for review January 8, 2025 20:58
@finswimmer finswimmer requested a review from a team January 8, 2025 20:58
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 @finswimmer - I've reviewed your changes and they look great!

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.

Copy link
Member

@radoering radoering left a comment

Choose a reason for hiding this comment

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

Originally, I thought you should not be able to add additional dependencies via tool.poetry if you are using project.dependencies; you should only be able to enrich project.dependencies. However, silently ignoring them is not a good option either. I think my original thought should still hold true for building but maybe we can relax it for locking.

However, I think the underlying issue in python-poetry/poetry#9959 is deeper and not fixed by the PR: If you mix project.optional-dependencies and dynamic project.dependencies via tool.poetry.dependencies, the dependencies will also be missing when building a wheel. The issue is that optional dependencies go into the same dependency group as normal dependencies. The logic inside dependency groups works only well if there is one section in project and one section in tool.poetry, not if there are two sections each and you you are only using one in project and want to have the other dynamic.

I am not sure if fixing the underlying issue will make this PR redundant. Probably not. I will try to find a solution for the underlying issue tomorrow if nobody beats me to it and would like to keep this one open until it is clear if we need it or not.

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.

Missing packages from main group in poetry.lock when using dynamic dependencies
2 participants