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 Apr 18, 2024
1 parent b4231de commit 584fc59
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 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 @@ -9,16 +9,15 @@
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.metadata.base import DistributionVersion
from pip._internal.network.session import PipSession

class _DistWithLatestInfo(BaseDistribution):
"""Give the distribution object a couple of extra fields.
Expand Down Expand Up @@ -136,11 +135,15 @@ def add_options(self) -> None:
self.parser.insert_option_group(0, self.cmd_opts)

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

0 comments on commit 584fc59

Please sign in to comment.