Skip to content

Commit

Permalink
Revert to supporting simple Python factors
Browse files Browse the repository at this point in the history
Instead of parsing factors with virtualenv's
'PythonSpec.from_string_spec', use our own regex to support the simple
factors that have been supported since tox v1.

Signed-off-by: Stephen Finucane <stephen@that.guru>
Fixes: #2657
Fixes: #2848
  • Loading branch information
stephenfin committed Jan 10, 2023
1 parent 34b79a2 commit 4e64555
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
5 changes: 5 additions & 0 deletions docs/changelog/2848.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
tox has reverted support for complex Python factors such as ``py3.10``, ``cpython3.10.1`` or ``pypy3-64``. This was
initially supported with the release of tox 4.0 but has proven complicated to support. Instead, the simple factors
supported by tox 3 such as ``py310`` or ``pypy3`` should be used. Users who wish to specify more specific Python version
information should configure the ``base_python`` setting in the relevant ``[testenv]`` section(s) in ``tox.ini`` - by
:user:`stephenfin`.
9 changes: 5 additions & 4 deletions src/tox/tox_env/python/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,21 @@
from __future__ import annotations

import logging
import re
import sys
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Any, List, NamedTuple, cast

from packaging.tags import INTERPRETER_SHORT_NAMES
from virtualenv.discovery.py_spec import PythonSpec

from tox.config.main import Config
from tox.tox_env.api import ToxEnv, ToxEnvCreateArgs
from tox.tox_env.errors import Fail, Recreate, Skip
from tox.util.ci import is_ci

PY_FACTORS_RE = re.compile("^(?!py$)(?P<impl>py|pypy|jython)(?P<version>[2-9][0-9]?[0-9]?)?$")


class VersionInfo(NamedTuple):
major: int
Expand Down Expand Up @@ -131,9 +133,8 @@ def default_base_python(self, conf: Config, env_name: str | None) -> list[str]:
def extract_base_python(cls, env_name: str) -> str | None:
candidates: list[str] = []
for factor in env_name.split("-"):
spec = PythonSpec.from_string_spec(factor)
impl = spec.implementation or "python"
if impl.lower() in INTERPRETER_SHORT_NAMES and env_name is not None and spec.path is None:
match = PY_FACTORS_RE.match(factor)
if match:
candidates.append(factor)
if candidates:
if len(candidates) > 1:
Expand Down
6 changes: 0 additions & 6 deletions tests/tox_env/python/test_python_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,13 @@ def test_base_python_env_no_conflict(env: str, base_python: list[str], ignore_co
@pytest.mark.parametrize(
("env", "base_python", "expected", "conflict"),
[
("cpython", ["pypy"], "cpython", ["pypy"]),
("pypy", ["cpython"], "pypy", ["cpython"]),
("pypy2", ["pypy3"], "pypy2", ["pypy3"]),
("py3", ["py2"], "py3", ["py2"]),
("py38", ["py39"], "py38", ["py39"]),
("py38", ["py38", "py39"], "py38", ["py39"]),
("py38", ["python3"], "py38", ["python3"]),
("py310", ["py38", "py39"], "py310", ["py38", "py39"]),
("py3.11.1", ["py3.11.2"], "py3.11.1", ["py3.11.2"]),
("py3-64", ["py3-32"], "py3-64", ["py3-32"]),
("py310-magic", ["py39"], "py310", ["py39"]),
],
ids=lambda a: "|".join(a) if isinstance(a, list) else str(a),
Expand All @@ -110,9 +107,6 @@ def test_base_python_env_conflict(
conflict: list[str],
ignore_conflict: bool,
) -> None:
if env == "py3-64":
raise pytest.skip("bug #2657")

if ignore_conflict:
result = Python._validate_base_python(env, base_python, ignore_conflict)
assert result == [expected]
Expand Down
12 changes: 6 additions & 6 deletions tests/tox_env/python/virtual_env/test_virtualenv_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ def virtualenv_opt(monkeypatch: MonkeyPatch, mocker: MockerFixture) -> VirtualEn

def test_virtualenv_default_settings(tox_project: ToxProjectCreator, virtualenv_opt: VirtualEnvOptions) -> None:
proj = tox_project({"tox.ini": "[testenv]\npackage=skip"})
result = proj.run("r", "-e", "py", "--discover", sys.executable, str(proj.path / "a"))
result = proj.run("r", "-e", "py3", "--discover", sys.executable, str(proj.path / "a"))
result.assert_success()

conf = result.env_conf("py")
conf = result.env_conf("py3")
assert conf["system_site_packages"] is False
assert conf["always_copy"] is False
assert conf["download"] is False
Expand All @@ -46,7 +46,7 @@ def test_virtualenv_default_settings(tox_project: ToxProjectCreator, virtualenv_
assert virtualenv_opt.download is False
assert virtualenv_opt.copies is False
assert virtualenv_opt.no_periodic_update is True
assert virtualenv_opt.python == ["py"]
assert virtualenv_opt.python == ["py3"]
assert virtualenv_opt.try_first_with == [str(sys.executable), str(proj.path / "a")]


Expand All @@ -60,10 +60,10 @@ def test_virtualenv_flipped_settings(
)
monkeypatch.setenv("VIRTUALENV_CLEAR", "0")

result = proj.run("r", "-e", "py")
result = proj.run("r", "-e", "py3")
result.assert_success()

conf = result.env_conf("py")
conf = result.env_conf("py3")
assert conf["system_site_packages"] is True
assert conf["always_copy"] is True
assert conf["download"] is True
Expand All @@ -72,7 +72,7 @@ def test_virtualenv_flipped_settings(
assert virtualenv_opt.system_site is True
assert virtualenv_opt.download is True
assert virtualenv_opt.copies is True
assert virtualenv_opt.python == ["py"]
assert virtualenv_opt.python == ["py3"]


def test_virtualenv_env_ignored_if_set(
Expand Down

0 comments on commit 4e64555

Please sign in to comment.