Skip to content

Commit fafce99

Browse files
paveldikovpre-commit-ci[bot]gaborbernat
authored
fix(tox_env.python): do not process absolute paths to interpreter as PythonSpec (#3311)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Bernát Gábor <[email protected]> Co-authored-by: Bernát Gábor <[email protected]>
1 parent fdc9eb0 commit fafce99

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

docs/changelog/3191.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
``base_python`` now accepts absolute paths to interpreter executable - by :user:`paveldikov`.

src/tox/tox_env/python/api.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import re
77
import sys
88
from abc import ABC, abstractmethod
9+
from pathlib import Path
910
from typing import TYPE_CHECKING, Any, List, NamedTuple, cast
1011

1112
from virtualenv.discovery.py_spec import PythonSpec
@@ -14,7 +15,6 @@
1415
from tox.tox_env.errors import Fail, Recreate, Skip
1516

1617
if TYPE_CHECKING:
17-
from pathlib import Path
1818

1919
from tox.config.main import Config
2020

@@ -169,6 +169,8 @@ def _validate_base_python(
169169
if env_base_python is not None:
170170
spec_name = PythonSpec.from_string_spec(env_base_python)
171171
for base_python in base_pythons:
172+
if Path(base_python).is_absolute():
173+
return [base_python]
172174
spec_base = PythonSpec.from_string_spec(base_python)
173175
if any(
174176
getattr(spec_base, key) != getattr(spec_name, key)

tests/tox_env/python/test_python_api.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from __future__ import annotations
22

3+
import os
34
import sys
45
from typing import TYPE_CHECKING, Callable
56
from unittest.mock import patch
@@ -125,6 +126,24 @@ def test_base_python_env_no_conflict(env: str, base_python: list[str], ignore_co
125126
assert result is base_python
126127

127128

129+
@pytest.mark.parametrize(
130+
("env", "base_python", "platform"),
131+
[
132+
("py312-unix", ["/opt/python312/bin/python"], "posix"),
133+
("py312-win", [r"C:\Program Files\Python312\python.exe"], "nt"),
134+
("py311-win", [r"\\a\python311\python.exe"], "nt"),
135+
("py310-win", [r"\\?\UNC\a\python310\python.exe"], "nt"),
136+
("py310", ["//a/python310/bin/python"], None),
137+
],
138+
ids=lambda a: "|".join(a) if isinstance(a, list) else str(a),
139+
)
140+
def test_base_python_absolute(env: str, base_python: list[str], platform: str | None) -> None:
141+
if platform and platform != os.name:
142+
pytest.skip(f"Not applicable to this platform. ({platform} != {os.name})")
143+
result = Python._validate_base_python(env, base_python, False) # noqa: SLF001
144+
assert result == base_python
145+
146+
128147
@pytest.mark.parametrize("ignore_conflict", [True, False])
129148
@pytest.mark.parametrize(
130149
("env", "base_python", "expected", "conflict"),

0 commit comments

Comments
 (0)