Skip to content

Commit

Permalink
Avoid heavy index imports for pip uninstall & list
Browse files Browse the repository at this point in the history
pip uninstall and list currently depend on req_install.py which always imports
the expensive network and index machinery. However, it's only in rare situations
that these commands actually hit the network:

- `pip list --outdated`
- `pip list --uptodate`
- `pip uninstall --requirement <url>`

This commit builds on the previous two refactoring commits and modifies these
commands to avoid the expensive imports unless truly necessary.
  • Loading branch information
ichard26 committed May 5, 2024
1 parent 55cfa54 commit 1071614
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
10 changes: 10 additions & 0 deletions news/24ca5f01-e27e-41b1-9a9c-7e3a828e22d9.trivial.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pip uninstall and list currently depend on req_install.py which always imports
the expensive network and index machinery. However, it's only in rare situations
that these commands actually hit the network:

- ``pip list --outdated``
- ``pip list --uptodate``
- ``pip uninstall --requirement <url>``

This patch refactors req_install.py so these commands can avoid the expensive
imports unless truly necessary.
13 changes: 8 additions & 5 deletions src/pip/_internal/commands/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@
from pip._internal.cli.index_command import IndexGroupCommand
from pip._internal.cli.status_codes import SUCCESS
from pip._internal.exceptions import CommandError
from pip._internal.index.collector import LinkCollector
from pip._internal.index.package_finder import PackageFinder
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.compat import stdlib_pkgs
from pip._internal.utils.misc import tabulate, write_output

if TYPE_CHECKING:
from pip._internal.index.package_finder import PackageFinder
from pip._internal.network.session import PipSession

class _DistWithLatestInfo(BaseDistribution):
"""Give the distribution object a couple of extra fields.
Expand Down Expand Up @@ -140,11 +139,15 @@ def handle_pip_version_check(self, options: Values) -> None:
super().handle_pip_version_check(options)

def _build_package_finder(
self, options: Values, session: PipSession
) -> PackageFinder:
self, options: Values, session: "PipSession"
) -> "PackageFinder":
"""
Create a package finder appropriate to this list command.
"""
# Lazy import the heavy index modules as most list invocations won't need 'em.
from pip._internal.index.collector import LinkCollector
from pip._internal.index.package_finder import PackageFinder

link_collector = LinkCollector.create(session, options=options)

# Pass allow_yanked=False to ignore yanked versions.
Expand Down
2 changes: 1 addition & 1 deletion src/pip/_internal/commands/uninstall.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from pip._internal.cli import cmdoptions
from pip._internal.cli.base_command import Command
from pip._internal.cli.req_command import SessionCommandMixin
from pip._internal.cli.index_command import SessionCommandMixin
from pip._internal.cli.status_codes import SUCCESS
from pip._internal.exceptions import InstallationError
from pip._internal.req import parse_requirements
Expand Down
3 changes: 1 addition & 2 deletions tests/functional/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ def test_entrypoints_work(entrypoint: str, script: PipTestEnvironment) -> None:
sorted(
set(commands_dict).symmetric_difference(
# Exclude commands that are expected to use the network.
# TODO: uninstall and list should only import network modules as needed
{"install", "uninstall", "download", "search", "index", "wheel", "list"}
{"install", "download", "search", "index", "wheel"}
)
),
)
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def is_requirement_command(command: Command) -> bool:


@pytest.mark.parametrize("flag", ["", "--outdated", "--uptodate"])
@mock.patch("pip._internal.cli.req_command.pip_self_version_check")
@mock.patch("pip._internal.cli.index_command._pip_self_version_check")
@mock.patch.dict(os.environ, {"PIP_DISABLE_PIP_VERSION_CHECK": "no"})
def test_list_pip_version_check(version_check_mock: mock.Mock, flag: str) -> None:
"""
Expand Down

0 comments on commit 1071614

Please sign in to comment.