Skip to content

Commit b9101b5

Browse files
committed
Fix UV_PYTHON_PREFERENCE reading breaks tox when env_site_packages_dir is in set_env
Signed-off-by: Bernát Gábor <[email protected]>
1 parent 9d847c4 commit b9101b5

File tree

5 files changed

+25
-10
lines changed

5 files changed

+25
-10
lines changed

.pre-commit-config.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ repos:
1010
- id: check-github-workflows
1111
args: ["--verbose"]
1212
- repo: https://github.com/codespell-project/codespell
13-
rev: v2.3.0
13+
rev: v2.4.0
1414
hooks:
1515
- id: codespell
1616
additional_dependencies: ["tomli>=2.2.1"]
1717
- repo: https://github.com/tox-dev/tox-ini-fmt
18-
rev: "1.4.1"
18+
rev: "1.5.0"
1919
hooks:
2020
- id: tox-ini-fmt
2121
args: ["-p", "fix"]

pyproject.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ dependencies = [
4343
"packaging>=24.2",
4444
"tox>=4.24.1,<5",
4545
"typing-extensions>=4.12.2; python_version<'3.10'",
46-
"uv>=0.5.21,<1",
46+
"uv>=0.5.22,<1",
4747
]
4848
urls.Changelog = "https://github.com/tox-dev/tox-uv/releases"
4949
urls.Documentation = "https://github.com/tox-dev/tox-uv#tox-uv"
@@ -69,7 +69,7 @@ test = [
6969
]
7070
type = [ "mypy==1.14.1", { include-group = "test" } ]
7171
lint = [ "pre-commit-uv>=4.1.4" ]
72-
pkg-meta = [ "check-wheel-contents>=0.6.1", "twine>=6.1", "uv>=0.5.21" ]
72+
pkg-meta = [ "check-wheel-contents>=0.6.1", "twine>=6.1", "uv>=0.5.22" ]
7373

7474
[tool.hatch]
7575
build.hooks.vcs.version-file = "src/tox_uv/version.py"

src/tox_uv/_venv.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import json
66
import logging
7+
import os
78
import sys
89
from abc import ABC
910
from functools import cached_property
@@ -73,7 +74,9 @@ def register_config(self) -> None:
7374
self.conf.add_config(
7475
keys=["uv_python_preference"],
7576
of_type=cast("Type[Optional[PythonPreference]]", Optional[PythonPreference]), # noqa: UP006
76-
default=lambda conf, name: self.environment_variables.get("UV_PYTHON_PREFERENCE", "system"), # noqa: ARG005
77+
# use os.environ here instead of self.environment_variables as this value is needed to create the virtual
78+
# environment, if environment variables use env_site_packages_dir we would run into a chicken-egg problem.
79+
default=lambda conf, name: os.environ.get("UV_PYTHON_PREFERENCE", "system"), # noqa: ARG005
7780
desc=(
7881
"Whether to prefer using Python installations that are already"
7982
" present on the system, or those that are downloaded and"
@@ -240,10 +243,10 @@ def env_python(self) -> Path:
240243
def env_site_package_dir(self) -> Path:
241244
if sys.platform == "win32": # pragma: win32 cover
242245
return self.venv_dir / "Lib" / "site-packages"
243-
else: # pragma: win32 no cover # noqa: RET505
244-
py = self._py_info
245-
impl = "pypy" if py.implementation == "pypy" else "python"
246-
return self.venv_dir / "lib" / f"{impl}{py.version_dot}" / "site-packages"
246+
# pragma: win32 no cover
247+
py = self._py_info
248+
impl = "pypy" if py.implementation == "pypy" else "python"
249+
return self.venv_dir / "lib" / f"{impl}{py.version_dot}" / "site-packages"
247250

248251
def env_version_spec(self) -> str:
249252
base = self.base_python.version_info

tests/test_tox_uv_venv.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ def test_uv_venv_preference_override_via_env_var(
7171
assert got == "only-managed"
7272

7373

74+
def test_uv_venv_preference_override_via_env_var_and_set_env_depends_on_py(
75+
tox_project: ToxProjectCreator, monkeypatch: pytest.MonkeyPatch
76+
) -> None:
77+
project = tox_project({"tox.ini": "[testenv]\nset_env=A={env_site_packages_dir}"})
78+
monkeypatch.setenv("UV_PYTHON_PREFERENCE", "only-managed")
79+
80+
result = project.run("c", "-k", "set_env")
81+
result.assert_success()
82+
83+
assert str(project.path) in result.out
84+
85+
7486
def test_uv_venv_spec(tox_project: ToxProjectCreator) -> None:
7587
ver = sys.version_info
7688
project = tox_project({"tox.ini": f"[testenv]\npackage=skip\nbase_python={ver.major}.{ver.minor}"})

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[tox]
22
requires =
33
tox>=4.24.1
4-
tox-uv>=1.19.1
4+
tox-uv>=1.20
55
env_list =
66
fix
77
3.13

0 commit comments

Comments
 (0)