From 23d20ea18ab4e43a4a4cb2b721d818a8dcd62542 Mon Sep 17 00:00:00 2001 From: Eli Schwartz Date: Mon, 28 Apr 2025 11:42:02 -0400 Subject: [PATCH] Fix new dependency-groups feature to use the stdlib tomllib where possible Previously, commit 88c9f31ad8a5ffe0bb31ab500b8ddd1b9ff6a5dd modified pip to use the stdlib on versions of python where this module is in the stdlib. As justified there: Although a tomli copy is vendored, doing this conditional import allows: - automatically upgrading the code, when the time comes to drop py3.10 support - slightly simplifying debundling support, as it's no longer necessary to depend on a tomli(-wheel)? package on sufficiently newer versions of python. https://github.com/pypa/pip/pull/13065 added a new feature, including a vendored "dependency_groups" library that likewise supports using the stdlib tomllib via `dependency_groups/_toml_compat.py`. But the code in pip itself to use dependency_groups manually loads pyproject.toml and passes it to dependency_groups, and fails to use the same compatibility dispatch as both the pre-existing pip code and dependency_groups itself. Add back the conditional logic. --- news/13356.vendor.rst | 1 + src/pip/_internal/req/req_dependency_group.py | 11 ++++++++--- tests/unit/test_req_dependency_group.py | 2 +- 3 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 news/13356.vendor.rst diff --git a/src/pip/_internal/req/req_dependency_group.py b/src/pip/_internal/req/req_dependency_group.py index 8f124de5b81..e81dd45522a 100644 --- a/src/pip/_internal/req/req_dependency_group.py +++ b/src/pip/_internal/req/req_dependency_group.py @@ -1,6 +1,11 @@ +import sys from typing import Any, Dict, Iterable, Iterator, List, Tuple -from pip._vendor import tomli +if sys.version_info >= (3, 11): + import tomllib +else: + from pip._vendor import tomli as tomllib + from pip._vendor.dependency_groups import DependencyGroupResolver from pip._internal.exceptions import InstallationError @@ -65,10 +70,10 @@ def _load_pyproject(path: str) -> Dict[str, Any]: """ try: with open(path, "rb") as fp: - return tomli.load(fp) + return tomllib.load(fp) except FileNotFoundError: raise InstallationError(f"{path} not found. Cannot resolve '--group' option.") - except tomli.TOMLDecodeError as e: + except tomllib.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/tests/unit/test_req_dependency_group.py b/tests/unit/test_req_dependency_group.py index b596f6fc5d7..1b180f8d7f8 100644 --- a/tests/unit/test_req_dependency_group.py +++ b/tests/unit/test_req_dependency_group.py @@ -120,7 +120,7 @@ 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 + "pip._internal.req.req_dependency_group.tomllib.load", epipe_toml_load ) with pytest.raises(InstallationError, match=r"Error reading pyproject\.toml"):