Skip to main content

A tool for resolving PEP 735 Dependency Group data

Project description

dependency-groups

An implementation of Dependency Groups (PEP 735).

This is a library which is able to parse dependency groups, following includes, and provide that data as output.

Usage

dependency_groups expects data in the form of a dict, the loaded [dependency-groups] table. Start by loading:

import tomllib

with open("pyproject.toml", "rb") as fp:
    pyproject = tomllib.load(fp)

groups = pyproject["dependency-groups"]

Using dependency_groups.resolve, you can produce a list of strings, which must be valid Dependency Specifiers.

from dependency_groups import resolve

groups = {
  "test": ["pytest", {"include-group": "runtime"}],
  "runtime": ["flask"],
}

resolve(groups, "test")  # ['pytest', 'flask']

The library provides its resolution machinery via an object oriented interface, which allows users to explore the structure of data before or during resolution using DependencyGroupInclude and DependencyGroupResolver.

For example,

from dependency_groups import DependencyGroupResolver

groups = {
  "test": ["pytest", {"include-group": "runtime"}],
  "runtime": ["flask"],
}

resolver = DependencyGroupResolver(groups)

# you can lookup a group without resolving it
resolver.lookup("test")  # [Requirement('pytest'), DependencyGroupInclude('runtime')]

# and resolve() produces packaging Requirements
resolver.resolve("test")  # [Requirement('pytest'), Requirement('flask')]

Functional Interface

def resolve(
    dependency_groups: Mapping[str, str | Mapping[str, str]], group: str, /
) -> tuple[str, ...]:
    """
    Resolve a dependency group to a tuple of requirements, as strings.

    :param dependency_groups: the parsed contents of the ``[dependency-groups]`` table
        from ``pyproject.toml``
    :param group: the name of the group to resolve

    :raises TypeError: if the inputs appear to be the wrong types
    :raises ValueError: if the data does not appear to be valid dependency group data
    :raises LookupError: if group name is absent
    :raises packaging.requirements.InvalidRequirement: if a specifier is not valid
    """

Models

Parsed Dependency Group Includes are represented by a dataclass:

@dataclasses.dataclass
class DependencyGroupInclude:
    include_group: str

Resolver

class DependencyGroupResolver:
    """
    A resolver for Dependency Group data.

    This class handles caching, name normalization, cycle detection, and other
    parsing requirements. There are only two public methods for exploring the data:
    ``lookup()`` and ``resolve()``.

    :param dependency_groups: A mapping, as provided via pyproject
        ``[dependency-groups]``.
    """

    def lookup(self, group: str) -> tuple[Requirement | DependencyGroupInclude, ...]:
        """
        Lookup a group name, returning the parsed dependency data for that group.
        This will not resolve includes.

        :param group: the name of the group to lookup

        :raises ValueError: if the data does not appear to be valid dependency group
            data
        :raises LookupError: if group name is absent
        :raises packaging.requirements.InvalidRequirement: if a specifier is not valid
        """

    def resolve(self, group: str) -> tuple[Requirement, ...]:
        """
        Resolve a dependency group to a list of requirements.

        :param group: the name of the group to resolve

        :raises TypeError: if the inputs appear to be the wrong types
        :raises ValueError: if the data does not appear to be valid dependency group
            data
        :raises LookupError: if group name is absent
        :raises packaging.requirements.InvalidRequirement: if a specifier is not valid
        """

Errors

The following error classes are defined:

class CyclicDependencyError(ValueError):
    """
    An error representing the detection of a cycle.
    """

CLI Usage

dependency-groups provides a simple, module-level entrypoint. It can parse a pyproject.toml file and print a dependency group's contents back out. Just use python -m dependency_groups --help for details!

License

dependency-groups is distributed under the terms of the MIT license.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dependency_groups-0.1.0.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

dependency_groups-0.1.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file dependency_groups-0.1.0.tar.gz.

File metadata

  • Download URL: dependency_groups-0.1.0.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for dependency_groups-0.1.0.tar.gz
Algorithm Hash digest
SHA256 65c9159bd94a073878c0b4a86058fb3c5c741b645dc79be94fe16b7ec86964f8
MD5 a51015c3e7cdfba5adb205687bcd9f7c
BLAKE2b-256 ced636dbd69d7d5eb95d06c16446fcd51d7594df7cf07cb5ff5a74c29546e4c6

See more details on using hashes here.

File details

Details for the file dependency_groups-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for dependency_groups-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f569e1246498cd6121d25de0f04efc0341474dd1718024debecf02e1b28823b5
MD5 f3990e3665556419d94df1156ac96b57
BLAKE2b-256 4b573f514e7a4731c19b88591d12c66141adee0f7329d4ce4e327d1542443f76

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page