Skip to content

Commit

Permalink
Remove unused get_installed_distributions
Browse files Browse the repository at this point in the history
  • Loading branch information
sbidoul committed Sep 21, 2021
1 parent 0199c50 commit d051a00
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 106 deletions.
3 changes: 2 additions & 1 deletion src/pip/_internal/commands/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
from pip._internal.metadata import BaseDistribution, get_environment
from pip._internal.models.selection_prefs import SelectionPreferences
from pip._internal.network.session import PipSession
from pip._internal.utils.misc import stdlib_pkgs, tabulate, write_output
from pip._internal.utils.compat import stdlib_pkgs
from pip._internal.utils.misc import tabulate, write_output
from pip._internal.utils.parallel import map_multithread

if TYPE_CHECKING:
Expand Down
2 changes: 1 addition & 1 deletion src/pip/_internal/metadata/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
DirectUrlValidationError,
DirInfo,
)
from pip._internal.utils.misc import stdlib_pkgs # TODO: Move definition here.
from pip._internal.utils.compat import stdlib_pkgs # TODO: Move definition here.
from pip._internal.utils.misc import egg_link_path_from_sys_path
from pip._internal.utils.urls import url_to_path

Expand Down
32 changes: 1 addition & 31 deletions src/pip/_internal/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
Any,
BinaryIO,
Callable,
Container,
ContextManager,
Iterable,
Iterator,
Expand All @@ -40,7 +39,7 @@
from pip import __version__
from pip._internal.exceptions import CommandError
from pip._internal.locations import get_major_minor_version, site_packages, user_site
from pip._internal.utils.compat import WINDOWS, stdlib_pkgs
from pip._internal.utils.compat import WINDOWS
from pip._internal.utils.virtualenv import (
running_under_virtualenv,
virtualenv_no_global,
Expand Down Expand Up @@ -370,35 +369,6 @@ def egg_link_path_from_sys_path(raw_name: str) -> Optional[str]:
return None


def get_installed_distributions(
local_only: bool = True,
skip: Container[str] = stdlib_pkgs,
include_editables: bool = True,
editables_only: bool = False,
user_only: bool = False,
paths: Optional[List[str]] = None,
) -> List[Distribution]:
"""Return a list of installed Distribution objects.
Left for compatibility until direct pkg_resources uses are refactored out.
"""
from pip._internal.metadata import get_default_environment, get_environment
from pip._internal.metadata.pkg_resources import Distribution as _Dist

if paths is None:
env = get_default_environment()
else:
env = get_environment(paths)
dists = env.iter_installed_distributions(
local_only=local_only,
skip=skip,
include_editables=include_editables,
editables_only=editables_only,
user_only=user_only,
)
return [cast(_Dist, dist)._dist for dist in dists]


def get_distribution(req_name: str) -> Optional[Distribution]:
"""Given a requirement name, return the installed Distribution object.
Expand Down
74 changes: 1 addition & 73 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
egg_link_path,
format_size,
get_distribution,
get_installed_distributions,
get_prog,
hide_url,
hide_value,
Expand Down Expand Up @@ -187,9 +186,8 @@ def test_noegglink_in_sitepkgs_venv_global(self):

@patch("pip._internal.utils.misc.dist_in_usersite")
@patch("pip._internal.utils.misc.dist_is_local")
@patch("pip._internal.utils.misc.dist_is_editable")
class TestsGetDistributions:
"""Test get_installed_distributions() and get_distribution()."""
"""Test get_distribution()."""

class MockWorkingSet(List[Mock]):
def require(self, name):
Expand Down Expand Up @@ -219,78 +217,12 @@ def require(self, name):
)
)

def dist_is_editable(self, dist):
return dist.test_name == "editable"

def dist_is_local(self, dist):
return dist.test_name != "global" and dist.test_name != "user"

def dist_in_usersite(self, dist):
return dist.test_name == "user"

@patch("pip._vendor.pkg_resources.working_set", workingset)
def test_editables_only(
self, mock_dist_is_editable, mock_dist_is_local, mock_dist_in_usersite
):
mock_dist_is_editable.side_effect = self.dist_is_editable
mock_dist_is_local.side_effect = self.dist_is_local
mock_dist_in_usersite.side_effect = self.dist_in_usersite
dists = get_installed_distributions(editables_only=True)
assert len(dists) == 1, dists
assert dists[0].test_name == "editable"

@patch("pip._vendor.pkg_resources.working_set", workingset)
def test_exclude_editables(
self, mock_dist_is_editable, mock_dist_is_local, mock_dist_in_usersite
):
mock_dist_is_editable.side_effect = self.dist_is_editable
mock_dist_is_local.side_effect = self.dist_is_local
mock_dist_in_usersite.side_effect = self.dist_in_usersite
dists = get_installed_distributions(include_editables=False)
assert len(dists) == 1
assert dists[0].test_name == "normal"

@patch("pip._vendor.pkg_resources.working_set", workingset)
def test_include_globals(
self, mock_dist_is_editable, mock_dist_is_local, mock_dist_in_usersite
):
mock_dist_is_editable.side_effect = self.dist_is_editable
mock_dist_is_local.side_effect = self.dist_is_local
mock_dist_in_usersite.side_effect = self.dist_in_usersite
dists = get_installed_distributions(local_only=False)
assert len(dists) == 4

@patch("pip._vendor.pkg_resources.working_set", workingset)
def test_user_only(
self, mock_dist_is_editable, mock_dist_is_local, mock_dist_in_usersite
):
mock_dist_is_editable.side_effect = self.dist_is_editable
mock_dist_is_local.side_effect = self.dist_is_local
mock_dist_in_usersite.side_effect = self.dist_in_usersite
dists = get_installed_distributions(local_only=False, user_only=True)
assert len(dists) == 1
assert dists[0].test_name == "user"

@patch("pip._vendor.pkg_resources.working_set", workingset_stdlib)
def test_gte_py27_excludes(
self, mock_dist_is_editable, mock_dist_is_local, mock_dist_in_usersite
):
mock_dist_is_editable.side_effect = self.dist_is_editable
mock_dist_is_local.side_effect = self.dist_is_local
mock_dist_in_usersite.side_effect = self.dist_in_usersite
dists = get_installed_distributions()
assert len(dists) == 0

@patch("pip._vendor.pkg_resources.working_set", workingset_freeze)
def test_freeze_excludes(
self, mock_dist_is_editable, mock_dist_is_local, mock_dist_in_usersite
):
mock_dist_is_editable.side_effect = self.dist_is_editable
mock_dist_is_local.side_effect = self.dist_is_local
mock_dist_in_usersite.side_effect = self.dist_in_usersite
dists = get_installed_distributions(skip=("setuptools", "pip", "distribute"))
assert len(dists) == 0

@pytest.mark.parametrize(
"working_set, req_name",
itertools.chain(
Expand All @@ -306,14 +238,12 @@ def test_freeze_excludes(
)
def test_get_distribution(
self,
mock_dist_is_editable,
mock_dist_is_local,
mock_dist_in_usersite,
working_set,
req_name,
):
"""Ensure get_distribution() finds all kinds of distributions."""
mock_dist_is_editable.side_effect = self.dist_is_editable
mock_dist_is_local.side_effect = self.dist_is_local
mock_dist_in_usersite.side_effect = self.dist_in_usersite
with patch("pip._vendor.pkg_resources.working_set", working_set):
Expand All @@ -324,11 +254,9 @@ def test_get_distribution(
@patch("pip._vendor.pkg_resources.working_set", workingset)
def test_get_distribution_nonexist(
self,
mock_dist_is_editable,
mock_dist_is_local,
mock_dist_in_usersite,
):
mock_dist_is_editable.side_effect = self.dist_is_editable
mock_dist_is_local.side_effect = self.dist_is_local
mock_dist_in_usersite.side_effect = self.dist_in_usersite
dist = get_distribution("non-exist")
Expand Down

0 comments on commit d051a00

Please sign in to comment.