Skip to content

Commit

Permalink
list: disable pip version self check unless given --outdated/--uptoda…
Browse files Browse the repository at this point in the history
…te (#12646)
  • Loading branch information
ichard26 committed Apr 26, 2024
1 parent 6522547 commit e884c00
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
1 change: 1 addition & 0 deletions news/11677.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
``pip list`` no longer performs the pip version check unless ``--outdated`` or ``--uptodate`` is given.
4 changes: 4 additions & 0 deletions src/pip/_internal/commands/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ def add_options(self) -> None:
self.parser.insert_option_group(0, index_opts)
self.parser.insert_option_group(0, self.cmd_opts)

def handle_pip_version_check(self, options: Values) -> None:
if options.outdated or options.uptodate:
super().handle_pip_version_check(options)

def _build_package_finder(
self, options: Values, session: PipSession
) -> PackageFinder:
Expand Down
22 changes: 22 additions & 0 deletions tests/unit/test_commands.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
from typing import Callable, List
from unittest import mock

Expand Down Expand Up @@ -104,6 +105,10 @@ def test_index_group_handle_pip_version_check(
options.disable_pip_version_check = disable_pip_version_check
options.no_index = no_index

# See test test_list_pip_version_check() below.
if command_name == "list":
expected_called = False

command.handle_pip_version_check(options)
if expected_called:
mock_version_check.assert_called_once()
Expand All @@ -120,3 +125,20 @@ def is_requirement_command(command: Command) -> bool:
return isinstance(command, RequirementCommand)

check_commands(is_requirement_command, ["download", "install", "wheel"])


@pytest.mark.parametrize("flag", ["", "--outdated", "--uptodate"])
@mock.patch("pip._internal.cli.req_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:
"""
Ensure that pip list doesn't perform a version self-check unless given
--outdated or --uptodate (as they require hitting the network anyway).
"""
command = create_command("list")
command.run = lambda *args, **kwargs: 0 # type: ignore[method-assign]
command.main([flag])
if flag != "":
version_check_mock.assert_called_once()
else:
version_check_mock.assert_not_called()

0 comments on commit e884c00

Please sign in to comment.