Skip to content

Commit

Permalink
Add --force-python option as shorthand for --python and --extra-python (
Browse files Browse the repository at this point in the history
#427)

* Add test for --force-python option

* Add --force-python option as shorthand for --python and --extra-python

* Document the --force-python option
  • Loading branch information
cjolowicz committed May 27, 2021
1 parent 1d0ff6f commit e9945ef
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
11 changes: 8 additions & 3 deletions docs/usage.rst
Original file line number Diff line number Diff line change
Expand Up @@ -163,22 +163,27 @@ If the Noxfile sets ``nox.options.reuse_existing_virtualenvs``, you can override

Running additional Python versions
----------------------------------
In addition to Nox supporting executing single sessions, it also supports runnings python versions that aren't specified using ``--extra-pythons``.

In addition to Nox supporting executing single sessions, it also supports running Python versions that aren't specified using ``--extra-pythons``.

.. code-block:: console
nox --extra-pythons 3.8 3.9
This will, in addition to specified python versions in the Noxfile, also create sessions for the specified versions.
This will, in addition to specified Python versions in the Noxfile, also create sessions for the specified versions.

This option can be combined with ``--python`` to replace, instead of appending, the Python interpreter for a given session::

nox --python 3.10 --extra-python 3.10 -s lint

Instead of passing both options, you can use the ``--force-python`` shorthand::

nox --force-python 3.10 -s lint

Also, you can specify ``python`` in place of a specific version. This will run the session
using the ``python`` specified for the current ``PATH``::

nox --python python --extra-python python -s lint
nox --force-python python -s lint


.. _opt-stop-on-first-error:
Expand Down
21 changes: 21 additions & 0 deletions nox/_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ def _color_finalizer(value: bool, args: argparse.Namespace) -> bool:
return sys.stdout.isatty()


def _force_pythons_finalizer(
value: Sequence[str], args: argparse.Namespace
) -> Sequence[str]:
"""Propagate ``--force-python`` to ``--python`` and ``--extra-python``."""
if value:
args.pythons = args.extra_pythons = value
return value


def _posargs_finalizer(
value: Sequence[Any], args: argparse.Namespace
) -> Union[Sequence[Any], List[Any]]:
Expand Down Expand Up @@ -335,6 +344,18 @@ def _session_completer(
nargs="*",
help="Additionally, run sessions using the given python interpreter versions.",
),
_option_set.Option(
"force_pythons",
"--force-pythons",
"--force-python",
group=options.groups["secondary"],
nargs="*",
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``."
),
finalizer_func=_force_pythons_finalizer,
),
*_option_set.make_flag_pair(
"stop_on_first_error",
("-x", "--stop-on-first-error"),
Expand Down
9 changes: 9 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,3 +578,12 @@ def test_main_color_conflict(capsys, monkeypatch):
_, err = capsys.readouterr()

assert "color" in err


def test_main_force_python(monkeypatch):
monkeypatch.setattr(sys, "argv", ["nox", "--force-python=3.10"])
with mock.patch("nox.workflow.execute", return_value=0) as execute:
with mock.patch.object(sys, "exit"):
nox.__main__.main()
config = execute.call_args[1]["global_config"]
assert config.pythons == config.extra_pythons == ["3.10"]

0 comments on commit e9945ef

Please sign in to comment.