From 24d9a09270cf3112c4bbd19979af834075d0041e Mon Sep 17 00:00:00 2001 From: chrysle Date: Sun, 31 Dec 2023 16:45:53 +0100 Subject: [PATCH] Allow `--force-python` on unparametrized sessions --- nox/_options.py | 2 ++ nox/manifest.py | 4 ++++ tests/test_manifest.py | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/nox/_options.py b/nox/_options.py index 24987b44..e23cfe7e 100644 --- a/nox/_options.py +++ b/nox/_options.py @@ -433,6 +433,7 @@ def _session_completer( ), _option_set.Option( "force_pythons", + "-P", "--force-pythons", "--force-python", group=options.groups["python"], @@ -441,6 +442,7 @@ def _session_completer( help=( "Run sessions with the given interpreters instead of those listed in the" " Noxfile. This is a shorthand for ``--python=X.Y --extra-python=X.Y``." + " It will also work on sessions that don't have any interpreter parametrized." ), finalizer_func=_force_pythons_finalizer, ), diff --git a/nox/manifest.py b/nox/manifest.py index 1eff9528..fa9f7978 100644 --- a/nox/manifest.py +++ b/nox/manifest.py @@ -230,6 +230,10 @@ def make_session( # Otherwise, add the extra specified python. assert isinstance(func.python, str) func.python = _unique_list(func.python, *extra_pythons) + elif not func.python and self._config.force_pythons: + # If a python is forced by the user, but the underlying function + # has no version parametrised, add it as sole occupant to func.python + func.python = _unique_list(*extra_pythons) # If the func has the python attribute set to a list, we'll need # to expand them. diff --git a/tests/test_manifest.py b/tests/test_manifest.py index 5d87bfd4..5452288b 100644 --- a/tests/test_manifest.py +++ b/tests/test_manifest.py @@ -49,6 +49,7 @@ def create_mock_config(): cfg.force_venv_backend = None cfg.default_venv_backend = None cfg.extra_pythons = None + cfg.force_pythons = None cfg.posargs = [] return cfg @@ -287,6 +288,41 @@ def session_func(): assert expected == [session.func.python for session in manifest._all_sessions] +@pytest.mark.parametrize( + "python,force_pythons,expected", + [ + (None, [], [None]), + (None, ["3.8"], ["3.8"]), + (None, ["3.8", "3.9"], ["3.8", "3.9"]), + (False, [], [False]), + (False, ["3.8"], ["3.8"]), + (False, ["3.8", "3.9"], ["3.8", "3.9"]), + ("3.5", [], ["3.5"]), + ("3.5", ["3.8"], ["3.5", "3.8"]), + ("3.5", ["3.8", "3.9"], ["3.5", "3.8", "3.9"]), + (["3.5", "3.9"], [], ["3.5", "3.9"]), + (["3.5", "3.9"], ["3.8"], ["3.5", "3.9", "3.8"]), + (["3.5", "3.9"], ["3.8", "3.4"], ["3.5", "3.9", "3.8", "3.4"]), + (["3.5", "3.9"], ["3.5", "3.9"], ["3.5", "3.9"]), + ], +) +def test_force_pythons(python, force_pythons, expected): + cfg = create_mock_config() + cfg.force_pythons = force_pythons + cfg.extra_pythons = force_pythons + + manifest = Manifest({}, cfg) + + def session_func(): + pass + + func = Func(session_func, python=python) + for session in manifest.make_session("my_session", func): + manifest.add_session(session) + + assert expected == [session.func.python for session in manifest._all_sessions] + + def test_add_session_parametrized(): manifest = Manifest({}, create_mock_config())