From e884c0021ea6902db9dc316820453fcd9cd42144 Mon Sep 17 00:00:00 2001 From: Richard Si Date: Fri, 26 Apr 2024 16:06:54 -0400 Subject: [PATCH] list: disable pip version self check unless given --outdated/--uptodate (#12646) --- news/11677.feature.rst | 1 + src/pip/_internal/commands/list.py | 4 ++++ tests/unit/test_commands.py | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+) create mode 100644 news/11677.feature.rst diff --git a/news/11677.feature.rst b/news/11677.feature.rst new file mode 100644 index 00000000000..b68a0db876b --- /dev/null +++ b/news/11677.feature.rst @@ -0,0 +1 @@ +``pip list`` no longer performs the pip version check unless ``--outdated`` or ``--uptodate`` is given. diff --git a/src/pip/_internal/commands/list.py b/src/pip/_internal/commands/list.py index e551dda9a96..f510919910e 100644 --- a/src/pip/_internal/commands/list.py +++ b/src/pip/_internal/commands/list.py @@ -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: diff --git a/tests/unit/test_commands.py b/tests/unit/test_commands.py index 7a5c4e8319d..4f6366513d7 100644 --- a/tests/unit/test_commands.py +++ b/tests/unit/test_commands.py @@ -1,3 +1,4 @@ +import os from typing import Callable, List from unittest import mock @@ -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() @@ -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()