Skip to content

Commit 5cc540b

Browse files
committed
Use extras directly from metadata when using pkg_resources
Instead of relying on `pkg_resources`'s internal data structure, use the metadata has to be constructed regardless to lookup the information about relevant extras.
1 parent 908e913 commit 5cc540b

File tree

2 files changed

+22
-15
lines changed

2 files changed

+22
-15
lines changed

src/pip/_internal/metadata/pkg_resources.py

+16-9
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,19 @@ def run_script(self, script_name: str, namespace: str) -> None:
7575
class Distribution(BaseDistribution):
7676
def __init__(self, dist: pkg_resources.Distribution) -> None:
7777
self._dist = dist
78-
self._extra_mapping = {
79-
canonicalize_name(extra): extra for extra in self._dist.extras
80-
}
78+
# This is populated lazily, to avoid loading metadata for all possible
79+
# distributions eagerly.
80+
self.__extra_mapping: Optional[Mapping[NormalizedName, str]] = None
81+
82+
@property
83+
def _extra_mapping(self) -> Mapping[NormalizedName, str]:
84+
if self.__extra_mapping is None:
85+
self.__extra_mapping = {
86+
canonicalize_name(extra): pkg_resources.safe_extra(extra)
87+
for extra in self.metadata.get_all("Provides-Extra", [])
88+
}
89+
90+
return self.__extra_mapping
8191

8292
@classmethod
8393
def from_directory(cls, directory: str) -> BaseDistribution:
@@ -219,13 +229,10 @@ def _metadata_impl(self) -> email.message.Message:
219229

220230
def iter_dependencies(self, extras: Collection[str] = ()) -> Iterable[Requirement]:
221231
if extras:
222-
sanitised_extras = {canonicalize_name(e) for e in extras} & set(
223-
self._extra_mapping
232+
relevant_extras = set(self._extra_mapping) & set(
233+
map(canonicalize_name, extras)
224234
)
225-
extras = [
226-
self._extra_mapping[canonicalize_name(extra)]
227-
for extra in sanitised_extras
228-
]
235+
extras = [self._extra_mapping[extra] for extra in relevant_extras]
229236
return self._dist.requires(extras)
230237

231238
def iter_provided_extras(self) -> Iterable[str]:

tests/unit/metadata/test_metadata_pkg_resources.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ def require(self, name: str) -> None:
3939

4040
workingset = _MockWorkingSet(
4141
(
42-
mock.Mock(test_name="global", project_name="global", extras=[]),
43-
mock.Mock(test_name="editable", project_name="editable", extras=[]),
44-
mock.Mock(test_name="normal", project_name="normal", extras=[]),
45-
mock.Mock(test_name="user", project_name="user", extras=[]),
42+
mock.Mock(test_name="global", project_name="global"),
43+
mock.Mock(test_name="editable", project_name="editable"),
44+
mock.Mock(test_name="normal", project_name="normal"),
45+
mock.Mock(test_name="user", project_name="user"),
4646
)
4747
)
4848

4949
workingset_stdlib = _MockWorkingSet(
5050
(
51-
mock.Mock(test_name="normal", project_name="argparse", extras=[]),
52-
mock.Mock(test_name="normal", project_name="wsgiref", extras=[]),
51+
mock.Mock(test_name="normal", project_name="argparse"),
52+
mock.Mock(test_name="normal", project_name="wsgiref"),
5353
)
5454
)
5555

0 commit comments

Comments
 (0)