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 poetry add --optional locking #10076

Merged
merged 1 commit into from
Jan 21, 2025

Conversation

radoering
Copy link
Member

@radoering radoering commented Jan 19, 2025

Pull Request Check List

Resolves: #10041

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

Summary by Sourcery

Fix optional dependency locking when adding a new optional dependency.

Bug Fixes:

  • Fix a bug where adding an optional dependency with poetry add --optional would not correctly update the lock file.

Tests:

  • Add tests to verify the fix for optional dependency locking.

Copy link

sourcery-ai bot commented Jan 19, 2025

Reviewer's Guide by Sourcery

This pull request fixes an issue where optional dependencies were not being correctly added to the lock file when using the poetry add --optional command. The changes ensure that when a dependency is added with the --optional flag, it is correctly included in the extras section of the lock file and that the lock file is updated with the new dependency.

Sequence diagram for adding optional dependency

sequenceDiagram
    participant User as User
    participant Add as AddCommand
    participant Poetry as PoetryPackage
    participant Lock as Locker

    User->>Add: poetry add --optional dependency
    Add->>Add: handle()
    Note over Add: Process dependency constraints
    Add->>Add: _add_dependency_to_extras()
    Add->>Poetry: Get package extras
    Poetry-->>Add: Current extras
    Add->>Poetry: Update package extras
    Add->>Lock: Refresh lock file
    Lock-->>Add: Lock file updated
    Add-->>User: Dependency added successfully
Loading

Class diagram for AddCommand changes

classDiagram
    class AddCommand {
        +handle()
        -_add_dependency_to_extras(dependency: Dependency, extra_name: NormalizedName)
        -notify_about_existing_packages(existing_packages: list[str])
    }
    class Dependency {
        +name: str
        +_in_extras: list[str]
    }
    class Poetry {
        +package: Package
    }
    class Package {
        +extras: dict
    }
    AddCommand --> Dependency: uses
    AddCommand --> Poetry: uses
    Poetry --> Package: contains
Loading

Flow diagram for optional dependency handling

flowchart TD
    A[Start] --> B[Process dependency]
    B --> C{Is optional?}
    C -->|Yes| D[Canonicalize extra name]
    D --> E[Set _in_extras]
    E --> F[Add to extras]
    F --> G[Update lock file]
    C -->|No| G
    G --> H[End]
Loading

File-Level Changes

Change Details Files
Correctly add optional dependencies to the lock file.
  • Added logic to set the _in_extras attribute on the dependency object when the --optional flag is used.
  • Added a new method _add_dependency_to_extras to handle adding the dependency to the extras section of the package.
  • Modified the handle method to call the new _add_dependency_to_extras method when adding an optional dependency.
src/poetry/console/commands/add.py
Add tests to verify the fix.
  • Added new test cases to verify that optional dependencies are correctly added to the lock file.
  • Added assertions to check the content of the lock file, specifically the extras section and the markers of the added package.
  • Modified existing test cases to use a different dependency to avoid conflicts with other tests.
tests/console/commands/test_add.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

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 updates explaining the fixed behavior of poetry add --optional. This will help users understand how optional dependencies are now handled correctly.
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.

@abn abn force-pushed the fix-add-optional-locking branch from 2eefadd to 97b62e1 Compare January 21, 2025 15:01
@abn abn enabled auto-merge (rebase) January 21, 2025 15:02
@abn abn merged commit 1518109 into python-poetry:main Jan 21, 2025
58 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.

Adding an extra via the cli does not generate the required markers in the lockfile
2 participants