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: do not ignore dynamic project dependencies via tool.poetry.dependencies if project.optional-dependencies are defined #811

Merged
merged 2 commits into from
Jan 11, 2025

Conversation

radoering
Copy link
Member

@radoering radoering commented Jan 10, 2025

Resolves: python-poetry/poetry#9959
Closes: #810

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

Summary:

  • improve logic to get dependencies (for building) and dependencies for locking from project.dependencies, project.optional-dependencies and tool.poetry.dependencies
  • add more tests for this logic
  • add additional fixture sample_project_dynamic (everything dynamic that we did not deprecate) in addition to existing fixtures sample_project (legacy, no project section) and sample_project_new (everything that is possible in project section)
  • add additional fixture complete_dynamic in addition to existing fixtures complete and complete_new

Summary by Sourcery

Include dynamic optional dependencies defined in the tool.poetry.dependencies section when project.optional-dependencies are also defined.

Bug Fixes:

  • Fixed a bug where dynamic project dependencies specified via tool.poetry.dependencies were ignored if project.optional-dependencies were defined.

Tests:

  • Added tests for dependency resolution logic.

Copy link

sourcery-ai bot commented Jan 10, 2025

Reviewer's Guide by Sourcery

This pull request addresses a bug where dynamic project dependencies specified via tool.poetry.dependencies were ignored if project.optional-dependencies were also defined. The fix improves the logic for retrieving dependencies during build and lock operations, taking into account optional and dynamic dependencies. A new fixture, sample_project_dynamic, has been added to test this logic, alongside existing fixtures. Additionally, a new complete_dynamic fixture complements the existing complete and complete_new fixtures.

Class diagram showing updated DependencyGroup class

classDiagram
    class DependencyGroup {
        -_dependencies: list[Dependency]
        -_poetry_dependencies: list[Dependency]
        +name: str
        +dependencies(): list[Dependency]
        +dependencies_for_locking(): list[Dependency]
    }
    note for DependencyGroup "Modified dependencies property to handle
optional and non-optional dependencies
from both sources correctly"
Loading

File-Level Changes

Change Details Files
Improved dependency resolution logic to include dynamic project dependencies.
  • Modified dependency resolution to consider optional dependencies in dynamic projects.
  • Added tests to verify the correct handling of dynamic project dependencies alongside optional dependencies.
src/poetry/core/packages/dependency_group.py
tests/packages/test_dependency_group.py
Added new test fixtures for dynamic projects.
  • Created sample_project_dynamic fixture to represent a project with dynamic dependencies.
  • Created complete_dynamic fixture to represent a complete project with dynamic dependencies.
  • Updated tests to utilize the new fixtures.
tests/fixtures/sample_project_dynamic/pyproject.toml
tests/fixtures/sample_project_dynamic/README.rst
tests/test_factory.py
tests/masonry/builders/fixtures/complete_dynamic/pyproject.toml
tests/masonry/builders/fixtures/complete_dynamic/bin/script.sh
tests/masonry/builders/fixtures/complete_dynamic/my_package/__init__.py
tests/masonry/builders/fixtures/complete_dynamic/LICENSE
tests/masonry/builders/fixtures/complete_dynamic/README.rst
tests/masonry/builders/fixtures/complete_dynamic/my_package/data1/test.json
tests/masonry/builders/fixtures/complete_dynamic/my_package/sub_pkg2/data2/data.json
Updated tests and test parametrization to include dynamic project fixture.
  • Added complete_dynamic to test parametrization for broader test coverage.
  • Updated tests to include assertions for dynamic project behavior.
tests/masonry/builders/test_sdist.py
tests/masonry/test_api.py
tests/masonry/builders/test_wheel.py
tests/masonry/builders/test_complete.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:

  • Consider adding documentation to explain the dependency merging behavior, though the comprehensive test suite does a good job of illustrating the expected behavior.
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 radoering requested a review from a team January 10, 2025 17:40
@@ -26,7 +26,20 @@ def name(self) -> str:

@property
def dependencies(self) -> list[Dependency]:
return self._dependencies or self._poetry_dependencies
if not self._dependencies:
Copy link
Member

Choose a reason for hiding this comment

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

I guess I understood what's going on here 💪 And it looks like it should work.

As far as I see, we don't take into account the dynamic key of the project section anywhere in our codebase. Instead we are doing educated guesses. Wouldn't it be easier to rely on this field?

Copy link
Member Author

Choose a reason for hiding this comment

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

At first, I thought it is redundant. However, there is at least one use case where it is necessary:

Projects that do not have any mandatory dependencies but optional dependencies and use tool.poetry.dependencies to define sources for these optional dependencies. Without taking project.dynamic into account the dependencies in tool.poetry.dependencies would have been considered mandatory dependencies in this case.

I added a second commit that takes project.dynamic into account to fix this use case.

…ndencies` if `project.optional-dependencies` are defined
…cies are used to define or only to enrich project dependencies

This is especially relevant for projects that do not have any mandatory dependencies but optional dependencies and use `tool.poetry.dependencies` to define sources for these optional dependencies. Without this change the dependencies in `tool.poetry.dependencies` would have been considered mandatory dependencies in this case.
@radoering radoering force-pushed the optional-and-dynamic-deps branch from 3d67a46 to ad3da61 Compare January 11, 2025 16:33
@radoering radoering enabled auto-merge (rebase) January 11, 2025 16:33
@radoering radoering merged commit ed4bb56 into python-poetry:main Jan 11, 2025
18 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.

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