diff --git a/docs/html/user_guide.rst b/docs/html/user_guide.rst index 2837c2537b8..d6a0acf9cd8 100644 --- a/docs/html/user_guide.rst +++ b/docs/html/user_guide.rst @@ -256,6 +256,98 @@ Same as requirements files, constraints files can also be served via a URL, e.g. http://example.com/constraints.txt, so that your organization can store and serve them in a centralized place. + +.. _`Dependency Groups`: + + +Dependency Groups +================= + +"Dependency Groups" are lists of items to be installed stored in a +``pyproject.toml`` file. + +A dependency group is logically just a list of requirements, similar to the +contents of :ref:`Requirements Files`. Unlike requirements files, dependency +groups cannot contain non-package arguments for :ref:`pip install`. + +Groups can be declared like so: + +.. code-block:: toml + + # pyproject.toml + [dependency-groups] + groupA = [ + "pkg1", + "pkg2", + ] + +and installed with :ref:`pip install` like so: + +.. tab:: Unix/macOS + + .. code-block:: shell + + python -m pip install --group groupA + +.. tab:: Windows + + .. code-block:: shell + + py -m pip install --group groupA + +Full details on the contents of ``[dependency-groups]`` and more examples are +available in :ref:`the specification documentation `. + +.. note:: + + Dependency Groups are defined by a standard, and therefore do not support + ``pip``-specific syntax for requirements, only :ref:`standard dependency + specifiers `. + +``pip`` does not search projects or directories to discover ``pyproject.toml`` +files. The ``--group`` option is used to pass the path to the file, +and if the path is omitted, as in the example above, it defaults to +``pyproject.toml`` in the current directory. Using explicit paths, +:ref:`pip install` can use a file from another directory. For example: + +.. tab:: Unix/macOS + + .. code-block:: shell + + python -m pip install --group './project/subproject/pyproject.toml:groupA' + +.. tab:: Windows + + .. code-block:: shell + + py -m pip install --group './project/subproject/pyproject.toml:groupA' + + +This also makes it possible to install groups from multiple different projects +at once. For example, with a directory structure like so:: + + + project/ + + sub1/ + - pyproject.toml + + sub2/ + - pyproject.toml + +it is possible to install, from the ``project/`` directory, groups from the +subprojects thusly: + +.. tab:: Unix/macOS + + .. code-block:: shell + + python -m pip install --group './sub1/pyproject.toml:groupA' --group './sub2/pyproject.toml:groupB' + +.. tab:: Windows + + .. code-block:: shell + + py -m pip install --group './sub1/pyproject.toml:groupA' --group './sub2/pyproject.toml:groupB' + + .. _`Installing from Wheels`: diff --git a/news/12963.feature.rst b/news/12963.feature.rst new file mode 100644 index 00000000000..66320c7ba90 --- /dev/null +++ b/news/12963.feature.rst @@ -0,0 +1,4 @@ +- Add a ``--group`` option which allows installation from PEP 735 Dependency + Groups. ``--group`` accepts arguments of the form ``group`` or + ``path:group``, where the default path is ``pyproject.toml``, and installs + the named Dependency Group from the provided ``pyproject.toml`` file. diff --git a/src/pip/_internal/cli/cmdoptions.py b/src/pip/_internal/cli/cmdoptions.py index eeb7e651b79..81fed6e940d 100644 --- a/src/pip/_internal/cli/cmdoptions.py +++ b/src/pip/_internal/cli/cmdoptions.py @@ -13,6 +13,7 @@ import importlib.util import logging import os +import pathlib import textwrap from functools import partial from optparse import SUPPRESS_HELP, Option, OptionGroup, OptionParser, Values @@ -733,6 +734,46 @@ def _handle_no_cache_dir( help="Don't install package dependencies.", ) + +def _handle_dependency_group( + option: Option, opt: str, value: str, parser: OptionParser +) -> None: + """ + Process a value provided for the --group option. + + Splits on the rightmost ":", and validates that the path (if present) ends + in `pyproject.toml`. Defaults the path to `pyproject.toml` when one is not given. + + `:` cannot appear in dependency group names, so this is a safe and simple parse. + + This is an optparse.Option callback for the dependency_groups option. + """ + path, sep, groupname = value.rpartition(":") + if not sep: + path = "pyproject.toml" + else: + # check for 'pyproject.toml' filenames using pathlib + if pathlib.PurePath(path).name != "pyproject.toml": + msg = "group paths use 'pyproject.toml' filenames" + raise_option_error(parser, option=option, msg=msg) + + parser.values.dependency_groups.append((path, groupname)) + + +dependency_groups: Callable[..., Option] = partial( + Option, + "--group", + dest="dependency_groups", + default=[], + type=str, + action="callback", + callback=_handle_dependency_group, + metavar="[path:]group", + help='Install a named dependency-group from a "pyproject.toml" file. ' + 'If a path is given, the name of the file must be "pyproject.toml". ' + 'Defaults to using "pyproject.toml" in the current directory.', +) + ignore_requires_python: Callable[..., Option] = partial( Option, "--ignore-requires-python", diff --git a/src/pip/_internal/cli/req_command.py b/src/pip/_internal/cli/req_command.py index 92900f94ff4..82164e8a720 100644 --- a/src/pip/_internal/cli/req_command.py +++ b/src/pip/_internal/cli/req_command.py @@ -28,6 +28,7 @@ install_req_from_parsed_requirement, install_req_from_req_string, ) +from pip._internal.req.req_dependency_group import parse_dependency_groups from pip._internal.req.req_file import parse_requirements from pip._internal.req.req_install import InstallRequirement from pip._internal.resolution.base import BaseResolver @@ -79,6 +80,7 @@ class RequirementCommand(IndexGroupCommand): def __init__(self, *args: Any, **kw: Any) -> None: super().__init__(*args, **kw) + self.cmd_opts.add_option(cmdoptions.dependency_groups()) self.cmd_opts.add_option(cmdoptions.no_clean()) @staticmethod @@ -240,6 +242,16 @@ def get_requirements( ) requirements.append(req_to_add) + if options.dependency_groups: + for req in parse_dependency_groups(options.dependency_groups): + req_to_add = install_req_from_req_string( + req, + isolated=options.isolated_mode, + use_pep517=options.use_pep517, + user_supplied=True, + ) + requirements.append(req_to_add) + for req in options.editables: req_to_add = install_req_from_editable( req, @@ -272,7 +284,12 @@ def get_requirements( if any(req.has_hash_options for req in requirements): options.require_hashes = True - if not (args or options.editables or options.requirements): + if not ( + args + or options.editables + or options.requirements + or options.dependency_groups + ): opts = {"name": self.name} if options.find_links: raise CommandError( diff --git a/src/pip/_internal/req/req_dependency_group.py b/src/pip/_internal/req/req_dependency_group.py new file mode 100644 index 00000000000..8f124de5b81 --- /dev/null +++ b/src/pip/_internal/req/req_dependency_group.py @@ -0,0 +1,74 @@ +from typing import Any, Dict, Iterable, Iterator, List, Tuple + +from pip._vendor import tomli +from pip._vendor.dependency_groups import DependencyGroupResolver + +from pip._internal.exceptions import InstallationError + + +def parse_dependency_groups(groups: List[Tuple[str, str]]) -> List[str]: + """ + Parse dependency groups data as provided via the CLI, in a `[path:]group` syntax. + + Raises InstallationErrors if anything goes wrong. + """ + resolvers = _build_resolvers(path for (path, _) in groups) + return list(_resolve_all_groups(resolvers, groups)) + + +def _resolve_all_groups( + resolvers: Dict[str, DependencyGroupResolver], groups: List[Tuple[str, str]] +) -> Iterator[str]: + """ + Run all resolution, converting any error from `DependencyGroupResolver` into + an InstallationError. + """ + for path, groupname in groups: + resolver = resolvers[path] + try: + yield from (str(req) for req in resolver.resolve(groupname)) + except (ValueError, TypeError, LookupError) as e: + raise InstallationError( + f"[dependency-groups] resolution failed for '{groupname}' " + f"from '{path}': {e}" + ) from e + + +def _build_resolvers(paths: Iterable[str]) -> Dict[str, Any]: + resolvers = {} + for path in paths: + if path in resolvers: + continue + + pyproject = _load_pyproject(path) + if "dependency-groups" not in pyproject: + raise InstallationError( + f"[dependency-groups] table was missing from '{path}'. " + "Cannot resolve '--group' option." + ) + raw_dependency_groups = pyproject["dependency-groups"] + if not isinstance(raw_dependency_groups, dict): + raise InstallationError( + f"[dependency-groups] table was malformed in {path}. " + "Cannot resolve '--group' option." + ) + + resolvers[path] = DependencyGroupResolver(raw_dependency_groups) + return resolvers + + +def _load_pyproject(path: str) -> Dict[str, Any]: + """ + This helper loads a pyproject.toml as TOML. + + It raises an InstallationError if the operation fails. + """ + try: + with open(path, "rb") as fp: + return tomli.load(fp) + except FileNotFoundError: + raise InstallationError(f"{path} not found. Cannot resolve '--group' option.") + except tomli.TOMLDecodeError as e: + raise InstallationError(f"Error parsing {path}: {e}") from e + except OSError as e: + raise InstallationError(f"Error reading {path}: {e}") from e diff --git a/src/pip/_vendor/__init__.py b/src/pip/_vendor/__init__.py index 561089ccc0c..34ccb990791 100644 --- a/src/pip/_vendor/__init__.py +++ b/src/pip/_vendor/__init__.py @@ -60,6 +60,7 @@ def vendored(modulename): # Actually alias all of our vendored dependencies. vendored("cachecontrol") vendored("certifi") + vendored("dependency-groups") vendored("distlib") vendored("distro") vendored("packaging") diff --git a/src/pip/_vendor/dependency_groups/LICENSE.txt b/src/pip/_vendor/dependency_groups/LICENSE.txt new file mode 100644 index 00000000000..b9723b85ed7 --- /dev/null +++ b/src/pip/_vendor/dependency_groups/LICENSE.txt @@ -0,0 +1,9 @@ +MIT License + +Copyright (c) 2024-present Stephen Rosen + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/src/pip/_vendor/dependency_groups/__init__.py b/src/pip/_vendor/dependency_groups/__init__.py new file mode 100644 index 00000000000..9fec2029949 --- /dev/null +++ b/src/pip/_vendor/dependency_groups/__init__.py @@ -0,0 +1,13 @@ +from ._implementation import ( + CyclicDependencyError, + DependencyGroupInclude, + DependencyGroupResolver, + resolve, +) + +__all__ = ( + "CyclicDependencyError", + "DependencyGroupInclude", + "DependencyGroupResolver", + "resolve", +) diff --git a/src/pip/_vendor/dependency_groups/__main__.py b/src/pip/_vendor/dependency_groups/__main__.py new file mode 100644 index 00000000000..48ebb0d41cf --- /dev/null +++ b/src/pip/_vendor/dependency_groups/__main__.py @@ -0,0 +1,65 @@ +import argparse +import sys + +from ._implementation import resolve +from ._toml_compat import tomllib + + +def main() -> None: + if tomllib is None: + print( + "Usage error: dependency-groups CLI requires tomli or Python 3.11+", + file=sys.stderr, + ) + raise SystemExit(2) + + parser = argparse.ArgumentParser( + description=( + "A dependency-groups CLI. Prints out a resolved group, newline-delimited." + ) + ) + parser.add_argument( + "GROUP_NAME", nargs="*", help="The dependency group(s) to resolve." + ) + parser.add_argument( + "-f", + "--pyproject-file", + default="pyproject.toml", + help="The pyproject.toml file. Defaults to trying in the current directory.", + ) + parser.add_argument( + "-o", + "--output", + help="An output file. Defaults to stdout.", + ) + parser.add_argument( + "-l", + "--list", + action="store_true", + help="List the available dependency groups", + ) + args = parser.parse_args() + + with open(args.pyproject_file, "rb") as fp: + pyproject = tomllib.load(fp) + + dependency_groups_raw = pyproject.get("dependency-groups", {}) + + if args.list: + print(*dependency_groups_raw.keys()) + return + if not args.GROUP_NAME: + print("A GROUP_NAME is required", file=sys.stderr) + raise SystemExit(3) + + content = "\n".join(resolve(dependency_groups_raw, *args.GROUP_NAME)) + + if args.output is None or args.output == "-": + print(content) + else: + with open(args.output, "w", encoding="utf-8") as fp: + print(content, file=fp) + + +if __name__ == "__main__": + main() diff --git a/src/pip/_vendor/dependency_groups/_implementation.py b/src/pip/_vendor/dependency_groups/_implementation.py new file mode 100644 index 00000000000..80d91693820 --- /dev/null +++ b/src/pip/_vendor/dependency_groups/_implementation.py @@ -0,0 +1,213 @@ +from __future__ import annotations + +import dataclasses +import re +from collections.abc import Mapping + +from pip._vendor.packaging.requirements import Requirement + + +def _normalize_name(name: str) -> str: + return re.sub(r"[-_.]+", "-", name).lower() + + +def _normalize_group_names( + dependency_groups: Mapping[str, str | Mapping[str, str]] +) -> Mapping[str, str | Mapping[str, str]]: + original_names: dict[str, list[str]] = {} + normalized_groups = {} + + for group_name, value in dependency_groups.items(): + normed_group_name = _normalize_name(group_name) + original_names.setdefault(normed_group_name, []).append(group_name) + normalized_groups[normed_group_name] = value + + errors = [] + for normed_name, names in original_names.items(): + if len(names) > 1: + errors.append(f"{normed_name} ({', '.join(names)})") + if errors: + raise ValueError(f"Duplicate dependency group names: {', '.join(errors)}") + + return normalized_groups + + +@dataclasses.dataclass +class DependencyGroupInclude: + include_group: str + + +class CyclicDependencyError(ValueError): + """ + An error representing the detection of a cycle. + """ + + def __init__(self, requested_group: str, group: str, include_group: str) -> None: + self.requested_group = requested_group + self.group = group + self.include_group = include_group + + if include_group == group: + reason = f"{group} includes itself" + else: + reason = f"{include_group} -> {group}, {group} -> {include_group}" + super().__init__( + "Cyclic dependency group include while resolving " + f"{requested_group}: {reason}" + ) + + +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 __init__( + self, + dependency_groups: Mapping[str, str | Mapping[str, str]], + ) -> None: + if not isinstance(dependency_groups, Mapping): + raise TypeError("Dependency Groups table is not a mapping") + self.dependency_groups = _normalize_group_names(dependency_groups) + # a map of group names to parsed data + self._parsed_groups: dict[ + str, tuple[Requirement | DependencyGroupInclude, ...] + ] = {} + # a map of group names to their ancestors, used for cycle detection + self._include_graph_ancestors: dict[str, tuple[str, ...]] = {} + # a cache of completed resolutions to Requirement lists + self._resolve_cache: dict[str, tuple[Requirement, ...]] = {} + + 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 TypeError: if the data is not a string + :raises LookupError: if group name is absent + :raises packaging.requirements.InvalidRequirement: if a specifier is not valid + """ + if not isinstance(group, str): + raise TypeError("Dependency group name is not a str") + group = _normalize_name(group) + return self._parse_group(group) + + 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 + """ + if not isinstance(group, str): + raise TypeError("Dependency group name is not a str") + group = _normalize_name(group) + return self._resolve(group, group) + + def _parse_group( + self, group: str + ) -> tuple[Requirement | DependencyGroupInclude, ...]: + # short circuit -- never do the work twice + if group in self._parsed_groups: + return self._parsed_groups[group] + + if group not in self.dependency_groups: + raise LookupError(f"Dependency group '{group}' not found") + + raw_group = self.dependency_groups[group] + if not isinstance(raw_group, list): + raise TypeError(f"Dependency group '{group}' is not a list") + + elements: list[Requirement | DependencyGroupInclude] = [] + for item in raw_group: + if isinstance(item, str): + # packaging.requirements.Requirement parsing ensures that this is a + # valid PEP 508 Dependency Specifier + # raises InvalidRequirement on failure + elements.append(Requirement(item)) + elif isinstance(item, dict): + if tuple(item.keys()) != ("include-group",): + raise ValueError(f"Invalid dependency group item: {item}") + + include_group = next(iter(item.values())) + elements.append(DependencyGroupInclude(include_group=include_group)) + else: + raise ValueError(f"Invalid dependency group item: {item}") + + self._parsed_groups[group] = tuple(elements) + return self._parsed_groups[group] + + def _resolve(self, group: str, requested_group: str) -> tuple[Requirement, ...]: + """ + This is a helper for cached resolution to strings. + + :param group: The name of the group to resolve. + :param requested_group: The group which was used in the original, user-facing + request. + """ + if group in self._resolve_cache: + return self._resolve_cache[group] + + parsed = self._parse_group(group) + + resolved_group = [] + for item in parsed: + if isinstance(item, Requirement): + resolved_group.append(item) + elif isinstance(item, DependencyGroupInclude): + if item.include_group in self._include_graph_ancestors.get(group, ()): + raise CyclicDependencyError( + requested_group, group, item.include_group + ) + self._include_graph_ancestors[item.include_group] = ( + *self._include_graph_ancestors.get(group, ()), + group, + ) + resolved_group.extend( + self._resolve(item.include_group, requested_group) + ) + else: # unreachable + raise NotImplementedError( + f"Invalid dependency group item after parse: {item}" + ) + + self._resolve_cache[group] = tuple(resolved_group) + return self._resolve_cache[group] + + +def resolve( + dependency_groups: Mapping[str, str | Mapping[str, str]], /, *groups: 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 groups: the name of the group(s) 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 + """ + return tuple( + str(r) + for group in groups + for r in DependencyGroupResolver(dependency_groups).resolve(group) + ) diff --git a/src/pip/_vendor/dependency_groups/_lint_dependency_groups.py b/src/pip/_vendor/dependency_groups/_lint_dependency_groups.py new file mode 100644 index 00000000000..09454bdc280 --- /dev/null +++ b/src/pip/_vendor/dependency_groups/_lint_dependency_groups.py @@ -0,0 +1,59 @@ +from __future__ import annotations + +import argparse +import sys + +from ._implementation import DependencyGroupResolver +from ._toml_compat import tomllib + + +def main(*, argv: list[str] | None = None) -> None: + if tomllib is None: + print( + "Usage error: dependency-groups CLI requires tomli or Python 3.11+", + file=sys.stderr, + ) + raise SystemExit(2) + + parser = argparse.ArgumentParser( + description=( + "Lint Dependency Groups for validity. " + "This will eagerly load and check all of your Dependency Groups." + ) + ) + parser.add_argument( + "-f", + "--pyproject-file", + default="pyproject.toml", + help="The pyproject.toml file. Defaults to trying in the current directory.", + ) + args = parser.parse_args(argv if argv is not None else sys.argv[1:]) + + with open(args.pyproject_file, "rb") as fp: + pyproject = tomllib.load(fp) + dependency_groups_raw = pyproject.get("dependency-groups", {}) + + errors: list[str] = [] + try: + resolver = DependencyGroupResolver(dependency_groups_raw) + except (ValueError, TypeError) as e: + errors.append(f"{type(e).__name__}: {e}") + else: + for groupname in resolver.dependency_groups: + try: + resolver.resolve(groupname) + except (LookupError, ValueError, TypeError) as e: + errors.append(f"{type(e).__name__}: {e}") + + if errors: + print("errors encountered while examining dependency groups:") + for msg in errors: + print(f" {msg}") + sys.exit(1) + else: + print("ok") + sys.exit(0) + + +if __name__ == "__main__": + main() diff --git a/src/pip/_vendor/dependency_groups/_pip_wrapper.py b/src/pip/_vendor/dependency_groups/_pip_wrapper.py new file mode 100644 index 00000000000..f86d8961ba2 --- /dev/null +++ b/src/pip/_vendor/dependency_groups/_pip_wrapper.py @@ -0,0 +1,62 @@ +from __future__ import annotations + +import argparse +import subprocess +import sys + +from ._implementation import DependencyGroupResolver +from ._toml_compat import tomllib + + +def _invoke_pip(deps: list[str]) -> None: + subprocess.check_call([sys.executable, "-m", "pip", "install", *deps]) + + +def main(*, argv: list[str] | None = None) -> None: + if tomllib is None: + print( + "Usage error: dependency-groups CLI requires tomli or Python 3.11+", + file=sys.stderr, + ) + raise SystemExit(2) + + parser = argparse.ArgumentParser(description="Install Dependency Groups.") + parser.add_argument( + "DEPENDENCY_GROUP", nargs="+", help="The dependency groups to install." + ) + parser.add_argument( + "-f", + "--pyproject-file", + default="pyproject.toml", + help="The pyproject.toml file. Defaults to trying in the current directory.", + ) + args = parser.parse_args(argv if argv is not None else sys.argv[1:]) + + with open(args.pyproject_file, "rb") as fp: + pyproject = tomllib.load(fp) + dependency_groups_raw = pyproject.get("dependency-groups", {}) + + errors: list[str] = [] + resolved: list[str] = [] + try: + resolver = DependencyGroupResolver(dependency_groups_raw) + except (ValueError, TypeError) as e: + errors.append(f"{type(e).__name__}: {e}") + else: + for groupname in args.DEPENDENCY_GROUP: + try: + resolved.extend(str(r) for r in resolver.resolve(groupname)) + except (LookupError, ValueError, TypeError) as e: + errors.append(f"{type(e).__name__}: {e}") + + if errors: + print("errors encountered while examining dependency groups:") + for msg in errors: + print(f" {msg}") + sys.exit(1) + + _invoke_pip(resolved) + + +if __name__ == "__main__": + main() diff --git a/src/pip/_vendor/dependency_groups/_toml_compat.py b/src/pip/_vendor/dependency_groups/_toml_compat.py new file mode 100644 index 00000000000..8d6f921c2a5 --- /dev/null +++ b/src/pip/_vendor/dependency_groups/_toml_compat.py @@ -0,0 +1,9 @@ +try: + import tomllib +except ImportError: + try: + from pip._vendor import tomli as tomllib # type: ignore[no-redef, unused-ignore] + except ModuleNotFoundError: # pragma: no cover + tomllib = None # type: ignore[assignment, unused-ignore] + +__all__ = ("tomllib",) diff --git a/src/pip/_vendor/dependency_groups/py.typed b/src/pip/_vendor/dependency_groups/py.typed new file mode 100644 index 00000000000..e69de29bb2d diff --git a/src/pip/_vendor/vendor.txt b/src/pip/_vendor/vendor.txt index 03f8d428d04..c9db74290db 100644 --- a/src/pip/_vendor/vendor.txt +++ b/src/pip/_vendor/vendor.txt @@ -16,3 +16,4 @@ resolvelib==1.1.0 setuptools==70.3.0 tomli==2.2.1 truststore==0.10.1 +dependency-groups==1.3.0 diff --git a/tests/functional/test_install.py b/tests/functional/test_install.py index 35d4e58b65e..8ada80ed19a 100644 --- a/tests/functional/test_install.py +++ b/tests/functional/test_install.py @@ -318,6 +318,45 @@ def test_install_exit_status_code_when_blank_requirements_file( script.pip("install", "-r", "blank.txt") +def test_install_exit_status_code_when_empty_dependency_group( + script: PipTestEnvironment, +) -> None: + """ + Test install exit status code is 0 when empty dependency group specified + """ + script.scratch_path.joinpath("pyproject.toml").write_text( + """\ +[dependency-groups] +empty = [] +""" + ) + script.pip("install", "--group", "empty") + + +@pytest.mark.parametrize("file_exists", [True, False]) +def test_install_dependency_group_bad_filename_error( + script: PipTestEnvironment, file_exists: bool +) -> None: + """ + Test install exit status code is 2 (usage error) when a dependency group path is + specified which isn't a `pyproject.toml` + """ + if file_exists: + script.scratch_path.joinpath("not-pyproject.toml").write_text( + textwrap.dedent( + """ + [dependency-groups] + publish = ["twine"] + """ + ) + ) + result = script.pip( + "install", "--group", "not-pyproject.toml:publish", expect_error=True + ) + assert "group paths use 'pyproject.toml' filenames" in result.stderr + assert result.returncode == 2 + + @pytest.mark.network def test_basic_install_from_pypi(script: PipTestEnvironment) -> None: """ diff --git a/tests/functional/test_install_reqs.py b/tests/functional/test_install_reqs.py index b1aed6ad3f4..45103dc7c74 100644 --- a/tests/functional/test_install_reqs.py +++ b/tests/functional/test_install_reqs.py @@ -93,6 +93,93 @@ def test_requirements_file(script: PipTestEnvironment) -> None: assert result.files_created[script.site_packages / fn].dir +@pytest.mark.network +@pytest.mark.parametrize( + "path, groupname", + [ + (None, "initools"), + ("pyproject.toml", "initools"), + ("./pyproject.toml", "initools"), + (lambda path: path.absolute(), "initools"), + ], +) +def test_dependency_group( + script: PipTestEnvironment, + path: Any, + groupname: str, +) -> None: + """ + Test installing from a dependency group. + """ + pyproject = script.scratch_path / "pyproject.toml" + pyproject.write_text( + textwrap.dedent( + """\ + [dependency-groups] + initools = [ + "INITools==0.2", + "peppercorn<=0.6", + ] + """ + ) + ) + if path is None: + arg = groupname + else: + if callable(path): + path = path(pyproject) + arg = f"{path}:{groupname}" + result = script.pip("install", "--group", arg) + result.did_create(script.site_packages / "INITools-0.2.dist-info") + result.did_create(script.site_packages / "initools") + assert result.files_created[script.site_packages / "peppercorn"].dir + assert result.files_created[script.site_packages / "peppercorn-0.6.dist-info"].dir + + +@pytest.mark.network +def test_multiple_dependency_groups(script: PipTestEnvironment) -> None: + """ + Test installing from two dependency groups simultaneously. + + """ + pyproject = script.scratch_path / "pyproject.toml" + pyproject.write_text( + textwrap.dedent( + """\ + [dependency-groups] + initools = ["INITools==0.2"] + peppercorn = ["peppercorn<=0.6"] + """ + ) + ) + result = script.pip("install", "--group", "initools", "--group", "peppercorn") + result.did_create(script.site_packages / "INITools-0.2.dist-info") + result.did_create(script.site_packages / "initools") + assert result.files_created[script.site_packages / "peppercorn"].dir + assert result.files_created[script.site_packages / "peppercorn-0.6.dist-info"].dir + + +@pytest.mark.network +def test_dependency_group_with_non_normalized_name(script: PipTestEnvironment) -> None: + """ + Test installing from a dependency group with a non-normalized name, verifying that + the pyproject.toml content and CLI arg are normalized to match. + + """ + pyproject = script.scratch_path / "pyproject.toml" + pyproject.write_text( + textwrap.dedent( + """\ + [dependency-groups] + INITOOLS = ["INITools==0.2"] + """ + ) + ) + result = script.pip("install", "--group", "IniTools") + result.did_create(script.site_packages / "INITools-0.2.dist-info") + result.did_create(script.site_packages / "initools") + + def test_schema_check_in_requirements_file(script: PipTestEnvironment) -> None: """ Test installing from a requirements file with an invalid vcs schema.. @@ -212,6 +299,32 @@ def test_package_in_constraints_and_dependencies( assert "installed TopoRequires-0.0.1" in result.stdout +def test_constraints_apply_to_dependency_groups( + script: PipTestEnvironment, data: TestData +) -> None: + script.scratch_path.joinpath("constraints.txt").write_text("TopoRequires==0.0.1") + pyproject = script.scratch_path / "pyproject.toml" + pyproject.write_text( + textwrap.dedent( + """\ + [dependency-groups] + mylibs = ["TopoRequires2"] + """ + ) + ) + result = script.pip( + "install", + "--no-index", + "-f", + data.find_links, + "-c", + script.scratch_path / "constraints.txt", + "--group", + "mylibs", + ) + assert "installed TopoRequires-0.0.1" in result.stdout + + def test_multiple_constraints_files(script: PipTestEnvironment, data: TestData) -> None: script.scratch_path.joinpath("outer.txt").write_text("-c inner.txt") script.scratch_path.joinpath("inner.txt").write_text("Upper==1.0") diff --git a/tests/functional/test_wheel.py b/tests/functional/test_wheel.py index da2bd2d7904..e1ede880496 100644 --- a/tests/functional/test_wheel.py +++ b/tests/functional/test_wheel.py @@ -3,6 +3,7 @@ import os import re import sys +import textwrap from pathlib import Path import pytest @@ -69,6 +70,41 @@ def test_pip_wheel_success(script: PipTestEnvironment, data: TestData) -> None: assert "Successfully built simple" in result.stdout, result.stdout +def test_pip_wheel_success_with_dependency_group( + script: PipTestEnvironment, data: TestData +) -> None: + """ + Test 'pip wheel' success. + """ + pyproject = script.scratch_path / "pyproject.toml" + pyproject.write_text( + textwrap.dedent( + """\ + [dependency-groups] + simple = ["simple==3.0"] + """ + ) + ) + result = script.pip( + "wheel", + "--no-index", + "-f", + data.find_links, + "--group", + "simple", + ) + wheel_file_name = f"simple-3.0-py{pyversion[0]}-none-any.whl" + wheel_file_path = script.scratch / wheel_file_name + assert re.search( + r"Created wheel for simple: " + rf"filename={re.escape(wheel_file_name)} size=\d+ sha256=[A-Fa-f0-9]{{64}}", + result.stdout, + ) + assert re.search(r"^\s+Stored in directory: ", result.stdout, re.M) + result.did_create(wheel_file_path) + assert "Successfully built simple" in result.stdout, result.stdout + + def test_pip_wheel_build_cache(script: PipTestEnvironment, data: TestData) -> None: """ Test 'pip wheel' builds and caches. diff --git a/tests/unit/test_req_dependency_group.py b/tests/unit/test_req_dependency_group.py new file mode 100644 index 00000000000..b596f6fc5d7 --- /dev/null +++ b/tests/unit/test_req_dependency_group.py @@ -0,0 +1,127 @@ +import errno +import textwrap +from pathlib import Path +from typing import Any + +import pytest + +from pip._internal.exceptions import InstallationError +from pip._internal.req.req_dependency_group import parse_dependency_groups + + +def test_parse_simple_dependency_groups( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + pyproject = tmp_path.joinpath("pyproject.toml") + pyproject.write_text( + textwrap.dedent( + """\ + [dependency-groups] + foo = ["bar"] + """ + ) + ) + monkeypatch.chdir(tmp_path) + + result = list(parse_dependency_groups([("pyproject.toml", "foo")])) + + assert len(result) == 1, result + assert result[0] == "bar" + + +def test_parse_cyclic_dependency_groups( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + pyproject = tmp_path.joinpath("pyproject.toml") + pyproject.write_text( + textwrap.dedent( + """\ + [dependency-groups] + foo = [{include-group="bar"}] + bar = [{include-group="foo"}] + """ + ) + ) + monkeypatch.chdir(tmp_path) + + with pytest.raises( + InstallationError, + match=( + r"\[dependency-groups\] resolution failed for " + r"'foo' from 'pyproject\.toml':" + ), + ) as excinfo: + parse_dependency_groups([("pyproject.toml", "foo")]) + + exception = excinfo.value + assert ( + "Cyclic dependency group include while resolving foo: foo -> bar, bar -> foo" + ) in str(exception) + + +def test_parse_with_no_dependency_groups_defined( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + pyproject = tmp_path.joinpath("pyproject.toml") + pyproject.write_text("") + monkeypatch.chdir(tmp_path) + + with pytest.raises( + InstallationError, + match=(r"\[dependency-groups\] table was missing from 'pyproject\.toml'\."), + ): + parse_dependency_groups([("pyproject.toml", "foo")]) + + +def test_parse_with_no_pyproject_file( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + monkeypatch.chdir(tmp_path) + + with pytest.raises(InstallationError, match=r"pyproject\.toml not found\."): + parse_dependency_groups([("pyproject.toml", "foo")]) + + +def test_parse_with_malformed_pyproject_file( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + pyproject = tmp_path.joinpath("pyproject.toml") + pyproject.write_text( + textwrap.dedent( + """\ + [dependency-groups # no closing bracket + foo = ["bar"] + """ + ) + ) + monkeypatch.chdir(tmp_path) + + with pytest.raises(InstallationError, match=r"Error parsing pyproject\.toml"): + parse_dependency_groups([("pyproject.toml", "foo")]) + + +def test_parse_gets_unexpected_oserror( + tmp_path: Path, monkeypatch: pytest.MonkeyPatch +) -> None: + pyproject = tmp_path.joinpath("pyproject.toml") + pyproject.write_text( + textwrap.dedent( + """\ + [dependency-groups] + foo = ["bar"] + """ + ) + ) + monkeypatch.chdir(tmp_path) + + # inject an implementation of `tomli.load()` which emits an 'OSError(EPIPE, ...)' + # as though we were loading from a fifo or other unusual filetype + def epipe_toml_load(*args: Any, **kwargs: Any) -> None: + raise OSError(errno.EPIPE, "Broken pipe") + + monkeypatch.setattr( + "pip._internal.req.req_dependency_group.tomli.load", epipe_toml_load + ) + + with pytest.raises(InstallationError, match=r"Error reading pyproject\.toml"): + parse_dependency_groups([("pyproject.toml", "foo")])