跳转到主要内容

一个用于解析PEP 735依赖分组数据的工具

项目描述

dependency-groups

依赖分组(PEP 735)的实现。

这是一个库,能够解析依赖分组,包含项,并将这些数据作为输出提供。

使用方法

dependency_groups 预期数据以字典的形式存在,加载 [dependency-groups] 表格。首先加载

import tomllib

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

groups = pyproject["dependency-groups"]

使用 dependency_groups.resolve,您可以生成一个字符串列表,这些字符串必须是有效的依赖规范。

from dependency_groups import resolve

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

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

该库通过面向对象接口提供其解析机制,允许用户在解析前后使用 DependencyGroupIncludeDependencyGroupResolver 探索数据结构。

例如,

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')]

功能接口

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
    """

模型

解析后的依赖分组包含由数据类表示

@dataclasses.dataclass
class DependencyGroupInclude:
    include_group: str

解析器

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
        """

错误

定义了以下错误类

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

CLI使用

dependency-groups 提供了一个简单的模块级入口点。它可以解析 pyproject.toml 文件,并打印依赖分组的详细内容。只需使用 python -m dependency_groups --help 获取详细信息!

Lint CLI

dependency-groups 包含一个作为独立 CLI 入口点的代码检查器,lint-dependency-groups

使用 lint-dependency-groups --help 查看详细信息。

pre-commit

lint-dependency-groups CLI 也可用作 pre-commit 钩子

repos:
  - repo: https://github.com/sirosen/dependency-groups
    rev: 0.3.0
    hooks:
      - id: lint-dependency-groups

安装 CLI

dependency-groups 包含一个 pip 包装器,pip-install-dependency-groups

用法简单,只需 pip-install-dependency-groups groupname 即可安装!

使用 pip-install-dependency-groups --help 查看更多详细信息。

许可证

dependency-groupsMIT 许可证的条款下分发。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码分发

dependency_groups-0.3.0.tar.gz (10.4 kB 查看哈希值)

上传时间 源代码

构建分发

dependency_groups-0.3.0-py3-none-any.whl (9.5 kB 查看哈希值)

上传时间 Python 3

支持者

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页面