Skip to content

Commit f3a4de5

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 12df3fd commit f3a4de5

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

news/13356.vendor.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix issues with using tomllib from the stdlib if available, rather than tomli

src/pip/_internal/req/req_dependency_group.py

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1+
import sys
12
from typing import Any, Dict, Iterable, Iterator, List, Tuple
23

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

611
from pip._internal.exceptions import InstallationError
@@ -65,10 +70,10 @@ def _load_pyproject(path: str) -> Dict[str, Any]:
6570
"""
6671
try:
6772
with open(path, "rb") as fp:
68-
return tomli.load(fp)
73+
return tomllib.load(fp)
6974
except FileNotFoundError:
7075
raise InstallationError(f"{path} not found. Cannot resolve '--group' option.")
71-
except tomli.TOMLDecodeError as e:
76+
except tomllib.TOMLDecodeError as e:
7277
raise InstallationError(f"Error parsing {path}: {e}") from e
7378
except OSError as e:
7479
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)