Skip to content

Commit 98404f8

Browse files
committed
Ignore basepython for default factors (tox-dev#477)
tox provides a number of default factors - py27, py34, py35 etc. - that are tied to particular interpreter versions. It is possible to override these through individual sections or the global [testenv] section. For example, consider the following 'tox.ini' file: [tox] skipsdist = True minversion = 2.0 distribute = False envlist = py35,py27,pep8,py34-test [testenv] basepython = python3 install_command = pip install {opts} {packages} commands = python --version [testenv:py27] basepython = python2.7 Running any target except for 'py27' will result in the same interpreter being used. On Fedora 28 with the 'python3-tox' package: $ tox -qq -e py27 Python 2.7.15 $ tox -qq -e py35 Python 3.6.5 $ tox -qq -e py34-test Python 3.6.5 This is broken by design. Overriding these makes no sense and is a source of common misconfigurations, as noted in tox-dev#477. The only sane thing to do here is ignore the request and use the correct interpreter so this is what's done. Signed-off-by: Stephen Finucane <[email protected]> Closes: tox-dev#477
1 parent e8730fb commit 98404f8

File tree

5 files changed

+60
-43
lines changed

5 files changed

+60
-43
lines changed

changelog/477.bugfix.rst

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Ignore ``basepython`` for environments containing default factors, such as
2+
``py27`` or ``django18-py35``. This was a common source of misconfiguration and
3+
is rarely, if ever, desirable from a user perspective - by @stephenfin

doc/config.rst

+21-24
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,15 @@ Complete list of settings that you can put into ``testenv*`` sections:
8181

8282
.. confval:: basepython=NAME-OR-PATH
8383

84-
name or path to a Python interpreter which will be used for creating
85-
the virtual environment. **default**: interpreter used for tox invocation.
84+
Name or path to a Python interpreter which will be used for creating
85+
the virtual environment; if the environment name contains a :ref:`default
86+
factor <factors>`, this value will be ignored. **default**: interpreter
87+
used for tox invocation.
88+
89+
.. versionchanged:: 3.1
90+
91+
Environments that use a :ref:`default factor <factors>` now ignore this
92+
value, defaulting to the interpreter defined for that factor.
8693

8794
.. confval:: commands=ARGVLIST
8895

@@ -540,9 +547,6 @@ However, a better approach looks like this:
540547
envlist = {py27,py36}-django{15,16}
541548
542549
[testenv]
543-
basepython =
544-
py27: python2.7
545-
py36: python3.6
546550
deps =
547551
pytest
548552
django15: Django>=1.5,<1.6
@@ -606,23 +610,12 @@ Factors and factor-conditional settings
606610
++++++++++++++++++++++++++++++++++++++++
607611

608612
Parts of an environment name delimited by hyphens are called factors and can
609-
be used to set values conditionally:
610-
611-
.. code-block:: ini
612-
613-
basepython =
614-
py27: python2.7
615-
py36: python3.6
616-
617-
This conditional setting will lead to either ``python3.6`` or
618-
``python2.7`` used as base python, e.g. ``python3.6`` is selected if current
619-
environment contains ``py36`` factor.
620-
621-
In list settings such as ``deps`` or ``commands`` you can freely intermix
622-
optional lines with unconditional ones:
613+
be used to set values conditionally. In list settings such as ``deps`` or
614+
``commands`` you can freely intermix optional lines with unconditional ones:
623615

624616
.. code-block:: ini
625617
618+
[testenv]
626619
deps =
627620
pytest
628621
django15: Django>=1.5,<1.6
@@ -632,16 +625,20 @@ optional lines with unconditional ones:
632625
Reading it line by line:
633626

634627
- ``pytest`` will be included unconditionally,
635-
- ``Django>=1.5,<1.6`` will be included for environments containing ``django15`` factor,
628+
- ``Django>=1.5,<1.6`` will be included for environments containing
629+
``django15`` factor,
636630
- ``Django>=1.6,<1.7`` similarly depends on ``django16`` factor,
637631
- ``unittest`` will be loaded for Python 3.6 environments.
638632

639-
.. note::
633+
tox provides a number of default factors corresponding to Python interpreter
634+
versions. The conditional setting above will lead to either ``python3.6`` or
635+
``python2.7`` used as base python, e.g. ``python3.6`` is selected if current
636+
environment contains ``py36`` factor.
640637

641-
tox provides good defaults for basepython setting, so the above
642-
ini-file can be further reduced by omitting the ``basepython``
643-
setting.
638+
.. note::
644639

640+
It is not possible to override the ``basepython`` value for environments
641+
using default factors. If defined, this value will be ignored.
645642

646643
Complex factor conditions
647644
+++++++++++++++++++++++++

doc/example/general.rst

+12-16
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ a tox test run. Here is an example ``tox.ini`` configuration:
4949
.. code-block:: ini
5050
5151
[testenv:docs]
52-
basepython = python
5352
changedir = doc
5453
deps = sphinx
5554
commands = sphinx-build -W -b html -d {envtmpdir}/doctrees . {envtmpdir}/html
@@ -142,26 +141,23 @@ If you want to use this with Jenkins_, also checkout the :ref:`jenkins artifact
142141
.. _verlib: https://bitbucket.org/tarek/distutilsversion/
143142

144143
basepython defaults, overriding
145-
++++++++++++++++++++++++++++++++++++++++++
144+
+++++++++++++++++++++++++++++++
146145

147-
By default, for any ``pyXY`` test environment name
148-
the underlying "pythonX.Y" executable will be searched in
149-
your system ``PATH``. It must exist in order to successfully create
150-
virtualenv environments. On Windows a ``pythonX.Y`` named executable
151-
will be searched in typical default locations using the
152-
``C:\PythonX.Y\python.exe`` pattern.
146+
For any ``pyXY`` test environment name the underlying ``pythonX.Y`` executable
147+
will be searched in your system ``PATH``. Similarly, for ``jython`` and
148+
``pypy`` the respective ``jython`` and ``pypy-c`` names will be looked for.
149+
The executable must exist in order to successfully create *virtualenv*
150+
environments. On Windows a ``pythonX.Y`` named executable will be searched in
151+
typical default locations using the ``C:\PythonX.Y\python.exe`` pattern.
153152

154-
For ``jython`` and ``pypy`` the respective ``jython``
155-
and ``pypy-c`` names will be looked for.
156-
157-
You can override any of the default settings by defining
158-
the ``basepython`` variable in a specific test environment
159-
section, for example:
153+
All other targets will use the system ``python`` instead. You can override any
154+
of the default settings by defining the :confval:`basepython` variable in a
155+
specific test environment section, for example:
160156

161157
.. code-block:: ini
162158
163-
[testenv:py27]
164-
basepython=/my/path/to/python2.7
159+
[testenv:docs]
160+
basepython = python2.7
165161
166162
Avoiding expensive sdist
167163
------------------------

tests/test_config.py

+13
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,19 @@ def test_envbindir_jython(self, newconfig, bp):
10051005
if bp == "jython":
10061006
assert envconfig.envpython == envconfig.envbindir.join(bp)
10071007

1008+
def test_envbindir_override_ignored(self, newconfig):
1009+
config = newconfig(
1010+
"""
1011+
[testenv]
1012+
basepython=python3
1013+
[testenv:py27]
1014+
commands = python --version
1015+
"""
1016+
)
1017+
assert len(config.envconfigs) == 1
1018+
envconfig = config.envconfigs["py27"]
1019+
assert envconfig.basepython == "python2.7"
1020+
10081021
@pytest.mark.parametrize("plat", ["win32", "linux2"])
10091022
def test_passenv_as_multiline_list(self, newconfig, monkeypatch, plat):
10101023
monkeypatch.setattr(tox.INFO, "IS_WIN", plat == "win32")

tox/config.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -505,11 +505,19 @@ def setenv(testenv_config, value):
505505
)
506506

507507
def basepython_default(testenv_config, value):
508+
"""Configure a sane interpreter for the environment.
509+
510+
If the environment contains a default factor, this will always be the
511+
interpreter associated with that factor overriding anything manually
512+
set.
513+
"""
514+
for factor in testenv_config.factors:
515+
if factor in tox.PYTHON.DEFAULT_FACTORS:
516+
return tox.PYTHON.DEFAULT_FACTORS[factor]
517+
508518
if value is None:
509-
for factor in testenv_config.factors:
510-
if factor in tox.PYTHON.DEFAULT_FACTORS:
511-
return tox.PYTHON.DEFAULT_FACTORS[factor]
512519
return sys.executable
520+
513521
return str(value)
514522

515523
parser.add_testenv_attribute(

0 commit comments

Comments
 (0)