-
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.
Open
Implement a --group
option for installing from [dependency-groups]
found in pyproject.toml
files
#13065
Changes from 5 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,3 @@ | ||
- Add a ``--group`` option which allows installation from PEP 735 Dependency | ||
Groups. Only ``pyproject.toml`` files in the current working directory are | ||
supported. |
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
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
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,49 @@ | ||
from typing import Any, Dict, List | ||
|
||
from pip._vendor import tomli | ||
from pip._vendor.dependency_groups import resolve as resolve_dependency_group | ||
|
||
from pip._internal.exceptions import InstallationError | ||
|
||
|
||
def parse_dependency_groups(groups: List[str]) -> List[str]: | ||
""" | ||
Parse dependency groups data in a way which is sensitive to the `pip` context and | ||
raises InstallationErrors if anything goes wrong. | ||
""" | ||
pyproject = _load_pyproject() | ||
|
||
if "dependency-groups" not in pyproject: | ||
raise InstallationError( | ||
"[dependency-groups] table was missing. Cannot resolve '--group' options." | ||
) | ||
raw_dependency_groups = pyproject["dependency-groups"] | ||
if not isinstance(raw_dependency_groups, dict): | ||
raise InstallationError( | ||
"[dependency-groups] table was malformed. Cannot resolve '--group' options." | ||
) | ||
|
||
try: | ||
return list(resolve_dependency_group(raw_dependency_groups, *groups)) | ||
except (ValueError, TypeError, LookupError) as e: | ||
raise InstallationError(f"[dependency-groups] resolution failed: {e}") from e | ||
|
||
|
||
def _load_pyproject() -> Dict[str, Any]: | ||
""" | ||
This helper loads pyproject.toml from the current working directory. | ||
|
||
It does not allow specification of the path to be used and raises an | ||
InstallationError if the operation fails. | ||
""" | ||
try: | ||
with open("pyproject.toml", "rb") as fp: | ||
return tomli.load(fp) | ||
except FileNotFoundError: | ||
raise InstallationError( | ||
"pyproject.toml not found. Cannot resolve '--group' options." | ||
) | ||
except tomli.TOMLDecodeError as e: | ||
raise InstallationError(f"Error parsing pyproject.toml: {e}") from e | ||
except OSError as e: | ||
raise InstallationError(f"Error reading pyproject.toml: {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.
Should this be in
RequirementCommand
?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.
I didn't include it because I didn't think we'd want the option to be presented for
pip wheel
.It seems slightly strange to me that the wheel command is a
RequirementCommand
, so it's possible that there's something that I haven't understood. It read to me likepip wheel
can take requirements for the build environment itself -- did I follow that correctly?If this is the right call, have I split the option up in a good/sensible way?
If not, I'm happy to expose the option(s, since at least one other is under discussion) on
pip wheel
as well.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.
I think we do want it on
pip wheel
-- the mental model I have ispip create-wheelhouse
is the longer name forpip wheel
.Essentially, it's creating a bundle of wheels that you can pass to pip install with the same requirement strings/arguments. And,
--group
makes sense within that context IMO.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.
Okay, I'll shuffle it over and add tests in that case!
I've never used
pip wheel
, so I considered it likely that I hadn't understood it.