Skip to content

Commit e884c00

Browse files
authored
list: disable pip version self check unless given --outdated/--uptodate (#12646)
1 parent 6522547 commit e884c00

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

news/11677.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``pip list`` no longer performs the pip version check unless ``--outdated`` or ``--uptodate`` is given.

src/pip/_internal/commands/list.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ def add_options(self) -> None:
135135
self.parser.insert_option_group(0, index_opts)
136136
self.parser.insert_option_group(0, self.cmd_opts)
137137

138+
def handle_pip_version_check(self, options: Values) -> None:
139+
if options.outdated or options.uptodate:
140+
super().handle_pip_version_check(options)
141+
138142
def _build_package_finder(
139143
self, options: Values, session: PipSession
140144
) -> PackageFinder:

tests/unit/test_commands.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from typing import Callable, List
23
from unittest import mock
34

@@ -104,6 +105,10 @@ def test_index_group_handle_pip_version_check(
104105
options.disable_pip_version_check = disable_pip_version_check
105106
options.no_index = no_index
106107

108+
# See test test_list_pip_version_check() below.
109+
if command_name == "list":
110+
expected_called = False
111+
107112
command.handle_pip_version_check(options)
108113
if expected_called:
109114
mock_version_check.assert_called_once()
@@ -120,3 +125,20 @@ def is_requirement_command(command: Command) -> bool:
120125
return isinstance(command, RequirementCommand)
121126

122127
check_commands(is_requirement_command, ["download", "install", "wheel"])
128+
129+
130+
@pytest.mark.parametrize("flag", ["", "--outdated", "--uptodate"])
131+
@mock.patch("pip._internal.cli.req_command.pip_self_version_check")
132+
@mock.patch.dict(os.environ, {"PIP_DISABLE_PIP_VERSION_CHECK": "no"})
133+
def test_list_pip_version_check(version_check_mock: mock.Mock, flag: str) -> None:
134+
"""
135+
Ensure that pip list doesn't perform a version self-check unless given
136+
--outdated or --uptodate (as they require hitting the network anyway).
137+
"""
138+
command = create_command("list")
139+
command.run = lambda *args, **kwargs: 0 # type: ignore[method-assign]
140+
command.main([flag])
141+
if flag != "":
142+
version_check_mock.assert_called_once()
143+
else:
144+
version_check_mock.assert_not_called()

0 commit comments

Comments
 (0)