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

feat: Support PEP 735 dependency groups #823

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

finswimmer
Copy link
Member

@finswimmer finswimmer commented Jan 21, 2025

Required-by: python-poetry/poetry#10130
Relates-to: python-poetry/poetry#9751

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

Summary by Sourcery

New Features:

  • Add support for PEP 735 dependency groups.

Summary by Sourcery

New Features:

  • Add support for PEP 735 dependency groups.

Copy link

sourcery-ai bot commented Jan 21, 2025

Reviewer's Guide by Sourcery

This pull request implements support for PEP 735 dependency groups by extending the package configuration and dependency creation logic. The changes update how pyproject.toml data is parsed to include a new dependency-groups section, propagate this configuration through the package setup, and adjust dependency instantiation to carry group information. Additionally, new tests and fixtures have been added to validate both proper and invalid configurations.

Sequence Diagram for PEP 735 Dependency Groups Processing

sequenceDiagram
    participant P as pyproject.toml
    participant F as Factory.configure_package
    participant D as _configure_package_dependencies
    participant G as DependencyGroup
    participant Dep as Dependency.create_from_pep_508

    P->>F: Read 'dependency-groups' and tool_poetry config
    F->>D: Pass project, tool_poetry, and dependency_groups
    D->>G: For each dependency group, create DependencyGroup
    loop For each dependency constraint in group
        D->>Dep: Create Dependency (with groups parameter)
        Dep-->>D: Return Dependency instance
        D->>G: Add Dependency to DependencyGroup
    end
    D->>F: Package dependencies configured
Loading

Updated Dependency Class Diagram with Group Support

classDiagram
    class Dependency {
      - string name
      - string constraint
      - list extras
      - list groups
      + Dependency(name, constraint, extras, groups)
      + with_constraint(constraint)
      + create_from_pep_508(name, relative_to, groups)
    }

    class VCSDependency {
      - string url
      + VCSDependency(name, "git", url, extras, groups)
    }

    class URLDependency {
      - string url
      + URLDependency(name, "http/https", url, extras, groups)
    }

    class FileDependency {
      - string path
      + FileDependency(name, path, base, directory, extras, groups)
    }

    class DirectoryDependency {
      - string path
      + DirectoryDependency(name, path, base, extras, groups)
    }

    Dependency <|-- VCSDependency
    Dependency <|-- URLDependency
    Dependency <|-- FileDependency
    Dependency <|-- DirectoryDependency
Loading

File-Level Changes

Change Details Files
Integrate dependency group configuration into package setup.
  • Read the 'dependency-groups' key from the pyproject.toml data in the factory class.
  • Pass the dependency groups information to the dependency configuration functions.
  • Iterate over the dependency groups to create and add dependency groups to the package.
  • Include fallback logic to handle legacy configuration via the tool.poetry.group section.
  • Add validation for dependency groups by updating the validation logic.
src/poetry/core/factory.py
Extend dependency creation functions to support groups.
  • Add a new 'groups' parameter to the create_from_pep_508 method in the dependency module.
  • Pass the groups parameter to VCSDependency, URLDependency, FileDependency, and DirectoryDependency constructors.
  • Ensure that group information is stored within dependency instances.
src/poetry/core/packages/dependency.py
Enhance testing and fixture data for dependency groups.
  • Introduce tests to verify package configuration with dependency groups in tests/test_factory.py.
  • Assert expected properties such as optional flags and group assignments for dependencies.
  • Add tests for handling invalid dependency groups configurations, expecting appropriate errors.
  • Provide fixture files including an updated pyproject.toml with dependency-groups, a README fixture, and an invalid dependency groups configuration.
  • Include a new dependency-groups JSON schema file to validate the dependency groups section.
tests/test_factory.py
tests/fixtures/sample_project_with_groups_new/pyproject.toml
tests/fixtures/project_with_invalid_dependency_groups/pyproject.toml
src/poetry/core/json/schemas/dependency-groups-schema.json
tests/fixtures/sample_project_with_groups_new/README.rst

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

@finswimmer
Copy link
Member Author

@sourcery-ai review

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 - here's some feedback:

Overall Comments:

  • Please add documentation for the new dependency groups feature - this is a significant change that needs to be well documented for users
  • Link to the relevant issue number/tracking issue for PEP 735 implementation in the PR description
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.

@finswimmer finswimmer changed the title wip: poc pep 735 Support PEP 735 dependency groups Feb 5, 2025
@finswimmer
Copy link
Member Author

@sourcery-ai review

@finswimmer finswimmer changed the title Support PEP 735 dependency groups feat: Support PEP 735 dependency groups Feb 5, 2025
@finswimmer finswimmer requested a review from radoering February 5, 2025 08:09
@finswimmer
Copy link
Member Author

@sourcery-ai review

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 - here's some feedback:

Overall Comments:

  • Consider consolidating the two loops in _configure_package_dependencies that handle dependency groups (one for dependency-groups and one for tool.poetry.group) to reduce repeated logic.
  • If a dependency group exists in the pyproject data but lacks a corresponding tool.poetry.group entry, consider whether you want to apply default optional settings explicitly.
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.

@finswimmer finswimmer marked this pull request as ready for review February 8, 2025 08:25
@finswimmer finswimmer requested a review from a team February 8, 2025 08:25
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 - here's some feedback:

Overall Comments:

  • Consider consolidating the two loops in _configure_package_dependencies to reduce code duplication in processing dependency groups.
  • Validate and normalize the dependency groups input early to ensure consistent types before iterating over them.
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.

@@ -844,6 +869,19 @@ def test_create_poetry_with_invalid_dev_dependencies(caplog: LogCaptureFixture)
assert any("dev" in r.groups for r in poetry.package.all_requires)


def test_create_poetry_with_invalid_dependency_groups() -> None:
Copy link

Choose a reason for hiding this comment

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

suggestion (testing): Test more invalid group configurations.

The current test only covers one invalid case. Consider adding tests for other invalid scenarios, such as incorrect data types for dependency names, invalid characters in group names, or missing required fields.

Suggested implementation:

def test_create_poetry_with_invalid_dependency_groups() -> None:
    with pytest.raises(RuntimeError) as e:
        _ = Factory().create_poetry(
            fixtures_dir / "project_with_invalid_dependency_groups", with_groups=False
        )

    expected = """\
The Poetry configuration is invalid:
  - dependency-groups.testing[1] must be string
"""
    assert str(e.value) == expected


def test_create_poetry_with_invalid_dependency_names() -> None:
    with pytest.raises(RuntimeError) as e:
        _ = Factory().create_poetry(
            fixtures_dir / "project_with_invalid_dependency_names"
        )
    expected = """\
The Poetry configuration is invalid:
  - dependencies.some_dependency must be string, got int
"""
    assert str(e.value) == expected


def test_create_poetry_with_invalid_group_names() -> None:
    with pytest.raises(RuntimeError) as e:
        _ = Factory().create_poetry(
            fixtures_dir / "project_with_invalid_group_names"
        )
    expected = """\
The Poetry configuration is invalid:
  - dependency-groups.invalid-group-name! contains invalid characters
"""
    assert str(e.value) == expected


def test_create_poetry_with_missing_required_fields() -> None:
    with pytest.raises(RuntimeError) as e:
        _ = Factory().create_poetry(
            fixtures_dir / "project_with_missing_required_fields"
        )
    expected = """\
The Poetry configuration is invalid:
  - dependency 'essential' is missing required field 'version'
"""
    assert str(e.value) == expected

Make sure to create the corresponding fixture projects under the fixtures directory for each invalid configuration:

  1. project_with_invalid_dependency_names
  2. project_with_invalid_group_names
  3. project_with_missing_required_fields

These changes allow tests to cover multiple invalid scenarios as suggested.

@finswimmer finswimmer requested a review from radoering February 9, 2025 07:53
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.

Support for https://peps.python.org/pep-0735/#dependency-object-specifiers is missing, which might be confusing for users.

@finswimmer finswimmer marked this pull request as draft February 9, 2025 14:53
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