diff --git a/news/13356.vendor.rst b/news/13356.vendor.rst new file mode 100644 index 00000000000..85664222875 --- /dev/null +++ b/news/13356.vendor.rst @@ -0,0 +1 @@ +Fix issues with using tomllib from the stdlib if available, rather than tomli 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"):