diff --git a/src/pip/_internal/commands/list.py b/src/pip/_internal/commands/list.py index 11bfad579ee..266354d46b9 100644 --- a/src/pip/_internal/commands/list.py +++ b/src/pip/_internal/commands/list.py @@ -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: diff --git a/src/pip/_internal/metadata/base.py b/src/pip/_internal/metadata/base.py index fb48aeb0cc3..1782718d0ee 100644 --- a/src/pip/_internal/metadata/base.py +++ b/src/pip/_internal/metadata/base.py @@ -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 diff --git a/src/pip/_internal/utils/misc.py b/src/pip/_internal/utils/misc.py index ffc4991f0e1..a9df1d5b5ac 100644 --- a/src/pip/_internal/utils/misc.py +++ b/src/pip/_internal/utils/misc.py @@ -22,7 +22,6 @@ AnyStr, BinaryIO, Callable, - Container, ContextManager, Iterable, Iterator, @@ -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, @@ -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. diff --git a/tests/unit/test_utils.py b/tests/unit/test_utils.py index 4e5bba2e1e4..c18ac0f08ec 100644 --- a/tests/unit/test_utils.py +++ b/tests/unit/test_utils.py @@ -30,7 +30,6 @@ egg_link_path, format_size, get_distribution, - get_installed_distributions, get_prog, hide_url, hide_value, @@ -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): @@ -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( @@ -301,7 +231,6 @@ 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, @@ -309,7 +238,6 @@ def test_get_distribution( ): """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): @@ -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")