Skip to content

Revert 5cc540b #12709

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 9 additions & 17 deletions src/pip/_internal/metadata/pkg_resources.py
Original file line number Diff line number Diff line change
@@ -11,7 +11,6 @@
Mapping,
NamedTuple,
Optional,
cast,
)

from pip._vendor import pkg_resources
@@ -84,19 +83,9 @@ def run_script(self, script_name: str, namespace: str) -> None:
class Distribution(BaseDistribution):
def __init__(self, dist: pkg_resources.Distribution) -> None:
self._dist = dist
# This is populated lazily, to avoid loading metadata for all possible
# distributions eagerly.
self.__extra_mapping: Optional[Mapping[NormalizedName, str]] = None

@property
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", [])
}

return self.__extra_mapping
self._extra_mapping = {
canonicalize_name(extra): extra for extra in self._dist.extras
}

@classmethod
def from_directory(cls, directory: str) -> BaseDistribution:
@@ -242,10 +231,13 @@ def _metadata_impl(self) -> email.message.Message:

def iter_dependencies(self, extras: Collection[str] = ()) -> Iterable[Requirement]:
if extras:
relevant_extras = set(self._extra_mapping) & set(
map(canonicalize_name, extras)
sanitised_extras = {canonicalize_name(e) for e in extras} & set(
self._extra_mapping
)
extras = [self._extra_mapping[extra] for extra in relevant_extras]
extras = [
self._extra_mapping[canonicalize_name(extra)]
for extra in sanitised_extras
]
return self._dist.requires(extras)

def iter_provided_extras(self) -> Iterable[NormalizedName]:
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

@@ -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)
12 changes: 6 additions & 6 deletions tests/unit/metadata/test_metadata_pkg_resources.py
Original file line number Diff line number Diff line change
@@ -40,17 +40,17 @@ def require(self, name: str) -> None:

workingset = _MockWorkingSet(
(
mock.Mock(test_name="global", project_name="global"),
mock.Mock(test_name="editable", project_name="editable"),
mock.Mock(test_name="normal", project_name="normal"),
mock.Mock(test_name="user", project_name="user"),
mock.Mock(test_name="global", project_name="global", extras=[]),
mock.Mock(test_name="editable", project_name="editable", extras=[]),
mock.Mock(test_name="normal", project_name="normal", extras=[]),
mock.Mock(test_name="user", project_name="user", extras=[]),
)
)

workingset_stdlib = _MockWorkingSet(
(
mock.Mock(test_name="normal", project_name="argparse"),
mock.Mock(test_name="normal", project_name="wsgiref"),
mock.Mock(test_name="normal", project_name="argparse", extras=[]),
mock.Mock(test_name="normal", project_name="wsgiref", extras=[]),
)
)