Skip to content

Commit 9982815

Browse files
committed
Remove get_installed_distributions usages
The function itself is kept for now because it's currently used to test the pip.metadata subpackage...
1 parent 23be323 commit 9982815

File tree

6 files changed

+31
-33
lines changed

6 files changed

+31
-33
lines changed

src/pip/_internal/cli/autocompletion.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
from pip._internal.cli.main_parser import create_main_parser
1111
from pip._internal.commands import commands_dict, create_command
12-
from pip._internal.utils.misc import get_installed_distributions
12+
from pip._internal.metadata import get_default_environment
1313

1414

1515
def autocomplete() -> None:
@@ -45,10 +45,11 @@ def autocomplete() -> None:
4545
"uninstall",
4646
]
4747
if should_list_installed:
48+
env = get_default_environment()
4849
lc = current.lower()
4950
installed = [
5051
dist.key
51-
for dist in get_installed_distributions(local_only=True)
52+
for dist in env.iter_installed_distributions(local_only=True)
5253
if dist.key.startswith(lc) and dist.key not in cwords[1:]
5354
]
5455
# if there are no dists installed, fall back to option completion

src/pip/_internal/metadata/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ def local(self) -> bool:
108108
def in_usersite(self) -> bool:
109109
raise NotImplementedError()
110110

111+
@property
112+
def in_site_packages(self) -> bool:
113+
raise NotImplementedError()
114+
111115
def read_text(self, name: str) -> str:
112116
"""Read a file in the .dist-info (or .egg-info) directory.
113117

src/pip/_internal/metadata/pkg_resources.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ def local(self) -> bool:
6161
def in_usersite(self) -> bool:
6262
return misc.dist_in_usersite(self._dist)
6363

64+
@property
65+
def in_site_packages(self) -> bool:
66+
return misc.dist_in_site_packages(self._dist)
67+
6468
def read_text(self, name: str) -> str:
6569
if not self._dist.has_metadata(name):
6670
raise FileNotFoundError(name)

src/pip/_internal/operations/check.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
from pip._vendor.pkg_resources import RequirementParseError
1010

1111
from pip._internal.distributions import make_distribution_for_install_requirement
12+
from pip._internal.metadata import get_default_environment
1213
from pip._internal.req.req_install import InstallRequirement
13-
from pip._internal.utils.misc import get_installed_distributions
1414

1515
if TYPE_CHECKING:
1616
from pip._vendor.packaging.utils import NormalizedName
@@ -30,16 +30,12 @@
3030
PackageDetails = namedtuple('PackageDetails', ['version', 'requires'])
3131

3232

33-
def create_package_set_from_installed(**kwargs: Any) -> Tuple["PackageSet", bool]:
34-
"""Converts a list of distributions into a PackageSet.
35-
"""
36-
# Default to using all packages installed on the system
37-
if kwargs == {}:
38-
kwargs = {"local_only": False, "skip": ()}
39-
33+
def create_package_set_from_installed() -> Tuple["PackageSet", bool]:
34+
"""Converts a list of distributions into a PackageSet."""
4035
package_set = {}
4136
problems = False
42-
for dist in get_installed_distributions(**kwargs):
37+
env = get_default_environment()
38+
for dist in env.iter_installed_distributions(local_only=False, skip=()):
4339
name = canonicalize_name(dist.project_name)
4440
try:
4541
package_set[name] = PackageDetails(dist.version, dist.requires())

src/pip/_internal/resolution/resolvelib/factory.py

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
from pip._vendor.packaging.requirements import Requirement as PackagingRequirement
2323
from pip._vendor.packaging.specifiers import SpecifierSet
2424
from pip._vendor.packaging.utils import NormalizedName, canonicalize_name
25-
from pip._vendor.pkg_resources import Distribution
2625
from pip._vendor.resolvelib import ResolutionImpossible
2726

2827
from pip._internal.cache import CacheEntry, WheelCache
@@ -35,6 +34,7 @@
3534
UnsupportedWheel,
3635
)
3736
from pip._internal.index.package_finder import PackageFinder
37+
from pip._internal.metadata import BaseDistribution, get_default_environment
3838
from pip._internal.models.link import Link
3939
from pip._internal.models.wheel import Wheel
4040
from pip._internal.operations.prepare import RequirementPreparer
@@ -46,11 +46,6 @@
4646
from pip._internal.resolution.base import InstallRequirementProvider
4747
from pip._internal.utils.compatibility_tags import get_supported
4848
from pip._internal.utils.hashes import Hashes
49-
from pip._internal.utils.misc import (
50-
dist_in_site_packages,
51-
dist_in_usersite,
52-
get_installed_distributions,
53-
)
5449
from pip._internal.utils.virtualenv import running_under_virtualenv
5550

5651
from .base import Candidate, CandidateVersion, Constraint, Requirement
@@ -122,9 +117,10 @@ def __init__(
122117
] = {}
123118

124119
if not ignore_installed:
120+
env = get_default_environment()
125121
self._installed_dists = {
126-
canonicalize_name(dist.project_name): dist
127-
for dist in get_installed_distributions(local_only=False)
122+
dist.canonical_name: dist
123+
for dist in env.iter_installed_distributions(local_only=False)
128124
}
129125
else:
130126
self._installed_dists = {}
@@ -155,15 +151,15 @@ def _make_extras_candidate(
155151

156152
def _make_candidate_from_dist(
157153
self,
158-
dist: Distribution,
154+
dist: BaseDistribution,
159155
extras: FrozenSet[str],
160156
template: InstallRequirement,
161157
) -> Candidate:
162158
try:
163-
base = self._installed_candidate_cache[dist.key]
159+
base = self._installed_candidate_cache[dist.canonical_name]
164160
except KeyError:
165161
base = AlreadyInstalledCandidate(dist, template, factory=self)
166-
self._installed_candidate_cache[dist.key] = base
162+
self._installed_candidate_cache[dist.canonical_name] = base
167163
if not extras:
168164
return base
169165
return self._make_extras_candidate(base, extras)
@@ -520,7 +516,7 @@ def get_wheel_cache_entry(
520516
supported_tags=get_supported(),
521517
)
522518

523-
def get_dist_to_uninstall(self, candidate: Candidate) -> Optional[Distribution]:
519+
def get_dist_to_uninstall(self, candidate: Candidate) -> Optional[BaseDistribution]:
524520
# TODO: Are there more cases this needs to return True? Editable?
525521
dist = self._installed_dists.get(candidate.project_name)
526522
if dist is None: # Not installed, no uninstallation required.
@@ -533,21 +529,19 @@ def get_dist_to_uninstall(self, candidate: Candidate) -> Optional[Distribution]:
533529
return dist
534530

535531
# We're installing into user site. Remove the user site installation.
536-
if dist_in_usersite(dist):
532+
if dist.in_usersite:
537533
return dist
538534

539535
# We're installing into user site, but the installed incompatible
540536
# package is in global site. We can't uninstall that, and would let
541537
# the new user installation to "shadow" it. But shadowing won't work
542538
# in virtual environments, so we error out.
543-
if running_under_virtualenv() and dist_in_site_packages(dist):
544-
raise InstallationError(
545-
"Will not install to the user site because it will "
546-
"lack sys.path precedence to {} in {}".format(
547-
dist.project_name,
548-
dist.location,
549-
)
539+
if running_under_virtualenv() and dist.in_site_packages:
540+
message = (
541+
f"Will not install to the user site because it will lack "
542+
f"sys.path precedence to {dist.raw_name} in {dist.location}"
550543
)
544+
raise InstallationError(message)
551545
return None
552546

553547
def _report_requires_python_error(

src/pip/_internal/resolution/resolvelib/resolver.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
)
2323
from pip._internal.utils.deprecation import deprecated
2424
from pip._internal.utils.filetypes import is_archive_file
25-
from pip._internal.utils.misc import dist_is_editable
2625

2726
from .base import Candidate, Requirement
2827
from .factory import Factory
@@ -122,7 +121,7 @@ def resolve(
122121
elif parse_version(installed_dist.version) != candidate.version:
123122
# The installation is different in version -- reinstall.
124123
ireq.should_reinstall = True
125-
elif candidate.is_editable or dist_is_editable(installed_dist):
124+
elif candidate.is_editable or installed_dist.editable:
126125
# The incoming distribution is editable, or different in
127126
# editable-ness to installation -- reinstall.
128127
ireq.should_reinstall = True

0 commit comments

Comments
 (0)