From 2e2d40b600e1945fdd7d25f1f559b4a605b249bb Mon Sep 17 00:00:00 2001 From: Tolker-KU Date: Sun, 26 Dec 2021 22:21:12 +0100 Subject: [PATCH] Deprecate use of session.install() without a virtual env --- nox/sessions.py | 10 ++++++++++ tests/test_sessions.py | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/nox/sessions.py b/nox/sessions.py index f476bb2d..043a9e28 100644 --- a/nox/sessions.py +++ b/nox/sessions.py @@ -20,6 +20,7 @@ import re import sys import unicodedata +import warnings from typing import ( Any, Callable, @@ -488,6 +489,15 @@ def install(self, *args: str, **kwargs: Any) -> None: raise ValueError( "A session without a virtualenv can not install dependencies." ) + if isinstance(venv, PassthroughEnv): + warnings.warn( + f"Session {self.name} does not have a virtual environment, " + "so use of session.install() is deprecated since it would modify " + "the global Python environment. If you're really sure that is " + 'what you want to do, use session.run("pip", "install", ...) instead.', + category=FutureWarning, + stacklevel=2, + ) if not args: raise ValueError("At least one argument required to install().") diff --git a/tests/test_sessions.py b/tests/test_sessions.py index d5a895d2..b88656ae 100644 --- a/tests/test_sessions.py +++ b/tests/test_sessions.py @@ -578,6 +578,38 @@ class SessionNoSlots(nox.sessions.Session): external="error", ) + def test_install_no_venv_deprecated(self): + runner = nox.sessions.SessionRunner( + name="test", + signatures=["test"], + func=mock.sentinel.func, + global_config=_options.options.namespace(posargs=[]), + manifest=mock.create_autospec(nox.manifest.Manifest), + ) + runner.venv = mock.create_autospec(nox.virtualenv.PassthroughEnv) + runner.venv.env = {} + + class SessionNoSlots(nox.sessions.Session): + pass + + session = SessionNoSlots(runner=runner) + + with mock.patch.object(session, "_run", autospec=True) as run: + with pytest.warns(FutureWarning, + match=r"use of session\.install\(\) is deprecated since it would modify the global Python environment" + ): + session.install("requests", "urllib3") + run.assert_called_once_with( + "python", + "-m", + "pip", + "install", + "requests", + "urllib3", + silent=True, + external="error", + ) + def test_notify(self): session, runner = self.make_session_and_runner()