Skip to content

Commit 1ce4661

Browse files
committed
Fix new dependency-groups feature to use the stdlib tomllib where possible
Previously, commit 88c9f31 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. #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.
1 parent c440c47 commit 1ce4661

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

src/pip/_internal/req/req_dependency_group.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
from typing import Any, Dict, Iterable, Iterator, List, Tuple
22

3-
from pip._vendor import tomli
3+
if sys.version_info >= (3, 11):
4+
import tomllib
5+
else:
6+
from pip._vendor import tomli as tomllib
7+
48
from pip._vendor.dependency_groups import DependencyGroupResolver
59

610
from pip._internal.exceptions import InstallationError
@@ -65,10 +69,10 @@ def _load_pyproject(path: str) -> Dict[str, Any]:
6569
"""
6670
try:
6771
with open(path, "rb") as fp:
68-
return tomli.load(fp)
72+
return tomllib.load(fp)
6973
except FileNotFoundError:
7074
raise InstallationError(f"{path} not found. Cannot resolve '--group' option.")
71-
except tomli.TOMLDecodeError as e:
75+
except tomllib.TOMLDecodeError as e:
7276
raise InstallationError(f"Error parsing {path}: {e}") from e
7377
except OSError as e:
7478
raise InstallationError(f"Error reading {path}: {e}") from e

tests/unit/test_req_dependency_group.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ def epipe_toml_load(*args: Any, **kwargs: Any) -> None:
120120
raise OSError(errno.EPIPE, "Broken pipe")
121121

122122
monkeypatch.setattr(
123-
"pip._internal.req.req_dependency_group.tomli.load", epipe_toml_load
123+
"pip._internal.req.req_dependency_group.tomllib.load", epipe_toml_load
124124
)
125125

126126
with pytest.raises(InstallationError, match=r"Error reading pyproject\.toml"):

0 commit comments

Comments
 (0)