Skip to content

Commit

Permalink
Merge pull request #13064 from ichard26/block-upgrade-prompt
Browse files Browse the repository at this point in the history
Skip self version check on EXTERNALLY-MANAGED environments
  • Loading branch information
pradyunsg authored Dec 8, 2024
2 parents c432c33 + 69533e3 commit 2324303
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions news/11820.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The pip version self check is disabled on ``EXTERNALLY-MANAGED`` environments.
10 changes: 9 additions & 1 deletion src/pip/_internal/self_outdated_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
get_best_invocation_for_this_python,
)
from pip._internal.utils.filesystem import adjacent_tmp_file, check_path_owner, replace
from pip._internal.utils.misc import ensure_dir
from pip._internal.utils.misc import (
ExternallyManagedEnvironment,
check_externally_managed,
ensure_dir,
)

_WEEK = datetime.timedelta(days=7)

Expand Down Expand Up @@ -231,6 +235,10 @@ def pip_self_version_check(session: PipSession, options: optparse.Values) -> Non
installed_dist = get_default_environment().get_distribution("pip")
if not installed_dist:
return
try:
check_externally_managed()
except ExternallyManagedEnvironment:
return

upgrade_prompt = _self_version_check_logic(
state=SelfCheckState(cache_dir=options.cache_dir),
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/test_self_check_outdated.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
from pip._vendor.packaging.version import Version

from pip._internal import self_outdated_check
from pip._internal.self_outdated_check import UpgradePrompt, pip_self_version_check
from pip._internal.utils.misc import ExternallyManagedEnvironment


@pytest.mark.parametrize(
Expand Down Expand Up @@ -185,3 +187,15 @@ def test_writes_expected_statefile(self, tmpdir: Path) -> None:
"last_check": "2000-01-01T00:00:00+00:00",
"pypi_version": "1.0.0",
}


@patch("pip._internal.self_outdated_check._self_version_check_logic")
def test_suppressed_by_externally_managed(mocked_function: Mock, tmpdir: Path) -> None:
mocked_function.return_value = UpgradePrompt(old="1.0", new="2.0")
fake_options = Values({"cache_dir": str(tmpdir)})
with patch(
"pip._internal.self_outdated_check.check_externally_managed",
side_effect=ExternallyManagedEnvironment("nope"),
):
pip_self_version_check(session=Mock(), options=fake_options)
mocked_function.assert_not_called()

0 comments on commit 2324303

Please sign in to comment.