Skip to content

Commit 9d847c4

Browse files
authored
Respect UV_PYTHON_PREFERENCE environment variable (#159)
Signed-off-by: Bernát Gábor <bgabor8@bloomberg.net>
1 parent d8307c2 commit 9d847c4

File tree

8 files changed

+61
-16
lines changed

8 files changed

+61
-16
lines changed

.github/workflows/check.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,17 @@ jobs:
3737
enable-cache: true
3838
cache-dependency-glob: "pyproject.toml"
3939
- name: Install tox
40-
run: uv tool install --python-preference only-managed --python 3.13 tox --with tox-uv
40+
run: uv tool install --python-preference only-managed --python 3.13 tox --with .
4141
- name: Install Python
4242
if: startsWith(matrix.env, '3.') && matrix.env != '3.13'
4343
run: uv python install --python-preference only-managed ${{ matrix.env }}
4444
- name: Setup test suite
4545
run: tox run -vv --notest --skip-missing-interpreters false -e ${{ matrix.env }}
46+
env:
47+
UV_PYTHON_PREFERENCE: "only-managed"
4648
- name: Run test suite
4749
run: tox run --skip-pkg-install -e ${{ matrix.env }}
4850
env:
4951
PYTEST_ADDOPTS: "-vv --durations=20"
5052
DIFF_AGAINST: HEAD
53+
UV_PYTHON_PREFERENCE: "only-managed"

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ repos:
1313
rev: v2.3.0
1414
hooks:
1515
- id: codespell
16-
additional_dependencies: ["tomli>=2.0.2"]
16+
additional_dependencies: ["tomli>=2.2.1"]
1717
- repo: https://github.com/tox-dev/tox-ini-fmt
1818
rev: "1.4.1"
1919
hooks:

pyproject.toml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
build-backend = "hatchling.build"
33
requires = [
44
"hatch-vcs>=0.4",
5-
"hatchling>=1.25",
5+
"hatchling>=1.27",
66
]
77

88
[project]
@@ -40,10 +40,10 @@ dynamic = [
4040
"version",
4141
]
4242
dependencies = [
43-
"packaging>=24.1",
44-
"tox>=4.21.2,<5",
43+
"packaging>=24.2",
44+
"tox>=4.24.1,<5",
4545
"typing-extensions>=4.12.2; python_version<'3.10'",
46-
"uv>=0.4.18,<1",
46+
"uv>=0.5.21,<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"
@@ -62,14 +62,14 @@ dev = [
6262
test = [
6363
"covdefaults>=2.3",
6464
"devpi-process>=1.0.2",
65-
"diff-cover>=9.2",
66-
"pytest>=8.3.3",
67-
"pytest-cov>=5",
65+
"diff-cover>=9.2.1",
66+
"pytest>=8.3.4",
67+
"pytest-cov>=6",
6868
"pytest-mock>=3.14",
6969
]
70-
type = [ "mypy==1.11.2", { include-group = "test" } ]
71-
lint = [ "pre-commit-uv>=4.1.3" ]
72-
pkg-meta = [ "check-wheel-contents>=0.6", "twine>=5.1.1", "uv>=0.4.18" ]
70+
type = [ "mypy==1.14.1", { include-group = "test" } ]
71+
lint = [ "pre-commit-uv>=4.1.4" ]
72+
pkg-meta = [ "check-wheel-contents>=0.6.1", "twine>=6.1", "uv>=0.5.21" ]
7373

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

src/tox_uv/_venv.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def register_config(self) -> None:
7373
self.conf.add_config(
7474
keys=["uv_python_preference"],
7575
of_type=cast("Type[Optional[PythonPreference]]", Optional[PythonPreference]), # noqa: UP006
76-
default="system",
76+
default=lambda conf, name: self.environment_variables.get("UV_PYTHON_PREFERENCE", "system"), # noqa: ARG005
7777
desc=(
7878
"Whether to prefer using Python installations that are already"
7979
" present on the system, or those that are downloaded and"

tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ def demo_pkg_inline(root: Path) -> Path:
3333
return root / "demo_pkg_inline"
3434

3535

36+
@pytest.fixture
37+
def clear_python_preference_env_var(monkeypatch: pytest.MonkeyPatch) -> None:
38+
monkeypatch.delenv("UV_PYTHON_PREFERENCE", raising=False)
39+
40+
3641
pytest_plugins = [
3742
"tox.pytest",
3843
]

tests/test_tox_uv_lock.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from tox.pytest import ToxProjectCreator
1111

1212

13+
@pytest.mark.usefixtures("clear_python_preference_env_var")
1314
def test_uv_lock_list_dependencies_command(tox_project: ToxProjectCreator) -> None:
1415
project = tox_project({
1516
"tox.ini": """
@@ -70,6 +71,7 @@ def test_uv_lock_list_dependencies_command(tox_project: ToxProjectCreator) -> No
7071
assert calls[i] == expected[i]
7172

7273

74+
@pytest.mark.usefixtures("clear_python_preference_env_var")
7375
@pytest.mark.parametrize("verbose", ["", "-v", "-vv", "-vvv"])
7476
def test_uv_lock_command(tox_project: ToxProjectCreator, verbose: str) -> None:
7577
project = tox_project({
@@ -131,6 +133,7 @@ def test_uv_lock_command(tox_project: ToxProjectCreator, verbose: str) -> None:
131133
assert show_uv_output is (bool(verbose))
132134

133135

136+
@pytest.mark.usefixtures("clear_python_preference_env_var")
134137
def test_uv_lock_with_dev(tox_project: ToxProjectCreator) -> None:
135138
project = tox_project({
136139
"tox.ini": """
@@ -166,6 +169,7 @@ def test_uv_lock_with_dev(tox_project: ToxProjectCreator) -> None:
166169
assert calls == expected
167170

168171

172+
@pytest.mark.usefixtures("clear_python_preference_env_var")
169173
@pytest.mark.parametrize(
170174
"name",
171175
[
@@ -229,6 +233,7 @@ def test_uv_lock_with_install_pkg(tox_project: ToxProjectCreator, name: str) ->
229233
assert calls == expected
230234

231235

236+
@pytest.mark.usefixtures("clear_python_preference_env_var")
232237
def test_uv_sync_extra_flags(tox_project: ToxProjectCreator) -> None:
233238
project = tox_project({
234239
"tox.ini": """
@@ -281,6 +286,7 @@ def test_uv_sync_extra_flags(tox_project: ToxProjectCreator) -> None:
281286
assert calls == expected
282287

283288

289+
@pytest.mark.usefixtures("clear_python_preference_env_var")
284290
def test_uv_sync_extra_flags_toml(tox_project: ToxProjectCreator) -> None:
285291
project = tox_project({
286292
"tox.toml": """
@@ -333,6 +339,7 @@ def test_uv_sync_extra_flags_toml(tox_project: ToxProjectCreator) -> None:
333339
assert calls == expected
334340

335341

342+
@pytest.mark.usefixtures("clear_python_preference_env_var")
336343
def test_uv_sync_dependency_groups(tox_project: ToxProjectCreator) -> None:
337344
project = tox_project({
338345
"tox.toml": """

tests/test_tox_uv_venv.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,36 @@ def test_uv_venv_pass_env(tox_project: ToxProjectCreator) -> None:
4141
assert "PKG_CONFIG_PATH" in pass_through
4242

4343

44+
@pytest.mark.usefixtures("clear_python_preference_env_var")
45+
def test_uv_venv_preference_system_by_default(tox_project: ToxProjectCreator) -> None:
46+
project = tox_project({"tox.ini": "[testenv]"})
47+
48+
result = project.run("c", "-k", "uv_python_preference")
49+
result.assert_success()
50+
51+
parser = ConfigParser()
52+
parser.read_string(result.out)
53+
got = parser["testenv:py"]["uv_python_preference"]
54+
55+
assert got == "system"
56+
57+
58+
def test_uv_venv_preference_override_via_env_var(
59+
tox_project: ToxProjectCreator, monkeypatch: pytest.MonkeyPatch
60+
) -> None:
61+
project = tox_project({"tox.ini": "[testenv]"})
62+
monkeypatch.setenv("UV_PYTHON_PREFERENCE", "only-managed")
63+
64+
result = project.run("c", "-k", "uv_python_preference")
65+
result.assert_success()
66+
67+
parser = ConfigParser()
68+
parser.read_string(result.out)
69+
got = parser["testenv:py"]["uv_python_preference"]
70+
71+
assert got == "only-managed"
72+
73+
4474
def test_uv_venv_spec(tox_project: ToxProjectCreator) -> None:
4575
ver = sys.version_info
4676
project = tox_project({"tox.ini": f"[testenv]\npackage=skip\nbase_python={ver.major}.{ver.minor}"})

tox.ini

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[tox]
22
requires =
3-
tox>=4.2
4-
tox-uv>=1.11.3
3+
tox>=4.24.1
4+
tox-uv>=1.19.1
55
env_list =
66
fix
77
3.13
@@ -36,7 +36,7 @@ dependency_groups = test
3636
description = format the code base to adhere to our styles, and complain about what we cannot do automatically
3737
skip_install = true
3838
deps =
39-
pre-commit-uv>=4.1.3
39+
pre-commit-uv>=4.1.4
4040
commands =
4141
pre-commit run --all-files --show-diff-on-failure
4242

0 commit comments

Comments
 (0)