Skip to content

Commit

Permalink
Use extras directly from metadata when using pkg_resources
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
pradyunsg committed Oct 6, 2023
1 parent d8bb25d commit ccbb835
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
25 changes: 16 additions & 9 deletions src/pip/_internal/metadata/pkg_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,19 @@ def run_script(self, script_name: str, namespace: str) -> None:
class Distribution(BaseDistribution):
def __init__(self, dist: pkg_resources.Distribution) -> None:
self._dist = dist
self._extra_mapping = {
canonicalize_name(extra): extra for extra in self._dist.extras
}
# This is populated lazily, to avoid loading metadata for all possible
# distributions eagerly.
self.__extra_mapping = None

@property
def _extra_mapping(self):
if self.__extra_mapping is None:
self.__extra_mapping = {
canonicalize_name(extra): pkg_resources.safe_extra(extra)
for extra in self.metadata.get_all("Provides-Extra", [])
}

return self.__extra_mapping

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

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

def iter_provided_extras(self) -> Iterable[str]:
Expand Down
12 changes: 6 additions & 6 deletions tests/unit/metadata/test_metadata_pkg_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ def require(self, name: str) -> None:

workingset = _MockWorkingSet(
(
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=[]),
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"),
)
)

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

Expand Down

0 comments on commit ccbb835

Please sign in to comment.