Skip to content

Commit

Permalink
Change the source of truth for pkg_resources' extras (#12711)
Browse files Browse the repository at this point in the history
* 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 <pradyunsg@users.noreply.github.com>
Co-authored-by: Stéphane Bidoul <stephane.bidoul@gmail.com>
  • Loading branch information
3 people authored Jun 10, 2024
1 parent 1a55574 commit ccd7be1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions news/12688.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Accommodate for mismatches between different sources of truth for extra names, for packages generated by ``setuptools``.
4 changes: 1 addition & 3 deletions src/pip/_internal/metadata/pkg_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
Mapping,
NamedTuple,
Optional,
cast,
)

from pip._vendor import pkg_resources
Expand Down Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions tests/functional/test_install_extras.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import re
import textwrap
from os.path import join
from pathlib import Path

import pytest

Expand Down Expand Up @@ -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)

0 comments on commit ccd7be1

Please sign in to comment.