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 Aug 1, 2021
1 parent a176189 commit f095f9c
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 109 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 @@ -22,7 +22,7 @@
DirectUrlValidationError,
DirInfo,
)
from pip._internal.utils.misc import stdlib_pkgs # TODO: Move definition here.
from pip._internal.utils.compat import stdlib_pkgs
from pip._internal.utils.misc import egg_link_path_from_sys_path
from pip._internal.utils.urls import url_to_path

Expand Down
33 changes: 1 addition & 32 deletions src/pip/_internal/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
AnyStr,
BinaryIO,
Callable,
Container,
ContextManager,
Iterable,
Iterator,
Expand All @@ -41,7 +40,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 @@ -395,36 +394,6 @@ def egg_link_path_from_sys_path(raw_name: str) -> Optional[str]:
return None


def get_installed_distributions(
local_only=True, # type: bool
skip=stdlib_pkgs, # type: Container[str]
include_editables=True, # type: bool
editables_only=False, # type: bool
user_only=False, # type: bool
paths=None, # type: Optional[List[str]]
):
# type: (...) -> 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):
# type: (str) -> Optional[Distribution]
"""Given a requirement name, return the installed Distribution object.
Expand Down
76 changes: 1 addition & 75 deletions tests/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
egg_link_path,
format_size,
get_distribution,
get_installed_distributions,
get_prog,
hide_url,
hide_value,
Expand Down Expand Up @@ -186,9 +185,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):
def require(self, name):
Expand All @@ -212,80 +210,12 @@ def require(self, name):
Mock(test_name='normal', project_name='distribute')
))

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 @@ -301,15 +231,13 @@ def test_freeze_excludes(self, mock_dist_is_editable,
)
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 @@ -320,11 +248,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 f095f9c

Please sign in to comment.