Skip to content

Commit 1b35375

Browse files
committed
Ignore basepython for default factors (tox-dev#425)
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#425. The only sane thing to do here is ignore the request and use the correct interpreter so this is what's done. A log is included to alert people to their potential misconfigurations. Signed-off-by: Stephen Finucane <[email protected]> Closes: tox-dev#425
1 parent e8730fb commit 1b35375

File tree

4 files changed

+42
-26
lines changed

4 files changed

+42
-26
lines changed

changelog/425.bugfix.rst

Lines changed: 3 additions & 0 deletions
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

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ Complete list of settings that you can put into ``testenv*`` sections:
8282
.. confval:: basepython=NAME-OR-PATH
8383

8484
name or path to a Python interpreter which will be used for creating
85-
the virtual environment. **default**: interpreter used for tox invocation.
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.
8688

8789
.. confval:: commands=ARGVLIST
8890

@@ -540,9 +542,6 @@ However, a better approach looks like this:
540542
envlist = {py27,py36}-django{15,16}
541543
542544
[testenv]
543-
basepython =
544-
py27: python2.7
545-
py36: python3.6
546545
deps =
547546
pytest
548547
django15: Django>=1.5,<1.6
@@ -606,23 +605,12 @@ Factors and factor-conditional settings
606605
++++++++++++++++++++++++++++++++++++++++
607606

608607
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:
608+
be used to set values conditionally. In list settings such as ``deps`` or
609+
``commands`` you can freely intermix optional lines with unconditional ones:
623610

624611
.. code-block:: ini
625612
613+
[testenv]
626614
deps =
627615
pytest
628616
django15: Django>=1.5,<1.6
@@ -632,16 +620,20 @@ optional lines with unconditional ones:
632620
Reading it line by line:
633621

634622
- ``pytest`` will be included unconditionally,
635-
- ``Django>=1.5,<1.6`` will be included for environments containing ``django15`` factor,
623+
- ``Django>=1.5,<1.6`` will be included for environments containing
624+
``django15`` factor,
636625
- ``Django>=1.6,<1.7`` similarly depends on ``django16`` factor,
637626
- ``unittest`` will be loaded for Python 3.6 environments.
638627

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

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

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

646638
Complex factor conditions
647639
+++++++++++++++++++++++++

tests/test_config.py

Lines changed: 13 additions & 0 deletions
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

Lines changed: 11 additions & 3 deletions
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)