From ccd7be1da6ac6cdeea45145a7d21642a3d9fad09 Mon Sep 17 00:00:00 2001 From: Pradyun Gedam Date: Mon, 10 Jun 2024 20:37:02 +0100 Subject: [PATCH] Change the source of truth for `pkg_resources`' extras (#12711) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Change the source of truth for `pkg_resources`' extras Instead of relying on the METADATA file, use the `requires.txt` file that pkg_resources uses under the hood to get the "target" extra names to pass into `pkg_resources`. * Add failing test for issue 12688 --------- Co-authored-by: Pradyun Gedam Co-authored-by: Stéphane Bidoul --- news/12688.bugfix.rst | 1 + src/pip/_internal/metadata/pkg_resources.py | 4 +--- tests/functional/test_install_extras.py | 21 +++++++++++++++++++++ 3 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 news/12688.bugfix.rst diff --git a/news/12688.bugfix.rst b/news/12688.bugfix.rst new file mode 100644 index 00000000000..72f8766baa3 --- /dev/null +++ b/news/12688.bugfix.rst @@ -0,0 +1 @@ +Accommodate for mismatches between different sources of truth for extra names, for packages generated by ``setuptools``. diff --git a/src/pip/_internal/metadata/pkg_resources.py b/src/pip/_internal/metadata/pkg_resources.py index 235556781c6..4ea84f93a6f 100644 --- a/src/pip/_internal/metadata/pkg_resources.py +++ b/src/pip/_internal/metadata/pkg_resources.py @@ -11,7 +11,6 @@ Mapping, NamedTuple, Optional, - cast, ) from pip._vendor import pkg_resources @@ -92,8 +91,7 @@ def __init__(self, dist: pkg_resources.Distribution) -> None: def _extra_mapping(self) -> Mapping[NormalizedName, str]: if self.__extra_mapping is None: self.__extra_mapping = { - canonicalize_name(extra): pkg_resources.safe_extra(cast(str, extra)) - for extra in self.metadata.get_all("Provides-Extra", []) + canonicalize_name(extra): extra for extra in self._dist.extras } return self.__extra_mapping diff --git a/tests/functional/test_install_extras.py b/tests/functional/test_install_extras.py index 4dd27bb9df3..4e74f54a2c3 100644 --- a/tests/functional/test_install_extras.py +++ b/tests/functional/test_install_extras.py @@ -1,6 +1,7 @@ import re import textwrap from os.path import join +from pathlib import Path import pytest @@ -252,3 +253,23 @@ def test_install_extras(script: PipTestEnvironment) -> None: "a", ) script.assert_installed(a="1", b="1", dep="1", meh="1") + + +def test_install_setuptools_extras_inconsistency( + script: PipTestEnvironment, tmp_path: Path +) -> None: + test_project_path = tmp_path.joinpath("test") + test_project_path.mkdir() + test_project_path.joinpath("setup.py").write_text( + textwrap.dedent( + """ + from setuptools import setup + setup( + name='test', + version='0.1', + extras_require={'extra_underscored': ['packaging']}, + ) + """ + ) + ) + script.pip("install", "--dry-run", test_project_path)