-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Implement a --group
option for installing from [dependency-groups]
found in pyproject.toml
files
#13065
Open
sirosen
wants to merge
9
commits into
pypa:main
Choose a base branch
from
sirosen:dependency-groups
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+884
−1
Open
Implement a --group
option for installing from [dependency-groups]
found in pyproject.toml
files
#13065
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
505b59a
Add 'dependency-groups==1.3.0' to vendored libs
sirosen 38aec3a
Implement Dependency Group option: `--group`
sirosen 164e731
Add unit tests for dependency group loading
sirosen 1d80247
Add initial functional tests for dependency-groups
sirosen 282f69e
Add a news fragment for `--group` support
sirosen 6238f2c
Support `--group` on `pip wheel`
sirosen 9dbdabb
Review feedback: use dedent in tests
sirosen cb10ef8
Update `--group` option to take `[path:]name`
sirosen 8aea313
Update --group news fragment
sirosen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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, it must end in "pyproject.toml:". ' | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not having read the implementation, it is not entirely clear to me what ending in "pyproject.toml:" means. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
'Defaults to using "pyproject.toml" in the current directory.', | ||||||
) | ||||||
|
||||||
ignore_requires_python: Callable[..., Option] = partial( | ||||||
Option, | ||||||
"--ignore-requires-python", | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024-present Stephen Rosen <sirosen0@gmail.com> | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from ._implementation import ( | ||
CyclicDependencyError, | ||
DependencyGroupInclude, | ||
DependencyGroupResolver, | ||
resolve, | ||
) | ||
|
||
__all__ = ( | ||
"CyclicDependencyError", | ||
"DependencyGroupInclude", | ||
"DependencyGroupResolver", | ||
"resolve", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1