From b769f840aaa6cd65961b32d87c99e9d12953ad42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bern=C3=A1t=20G=C3=A1bor?= Date: Wed, 22 Jan 2025 09:19:34 -0800 Subject: [PATCH] Fix UV_PYTHON_PREFERENCE reading breaks tox when env_site_packages_dir is in set_env (#160) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Bernát Gábor --- .pre-commit-config.yaml | 4 ++-- pyproject.toml | 4 ++-- src/tox_uv/_venv.py | 13 ++++++++----- tests/test_tox_uv_venv.py | 12 ++++++++++++ tox.ini | 2 +- 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 9200d50..4a60dc4 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -10,12 +10,12 @@ repos: - id: check-github-workflows args: ["--verbose"] - repo: https://github.com/codespell-project/codespell - rev: v2.3.0 + rev: v2.4.0 hooks: - id: codespell additional_dependencies: ["tomli>=2.2.1"] - repo: https://github.com/tox-dev/tox-ini-fmt - rev: "1.4.1" + rev: "1.5.0" hooks: - id: tox-ini-fmt args: ["-p", "fix"] diff --git a/pyproject.toml b/pyproject.toml index 60278cb..00e2bae 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -43,7 +43,7 @@ dependencies = [ "packaging>=24.2", "tox>=4.24.1,<5", "typing-extensions>=4.12.2; python_version<'3.10'", - "uv>=0.5.21,<1", + "uv>=0.5.22,<1", ] urls.Changelog = "https://github.com/tox-dev/tox-uv/releases" urls.Documentation = "https://github.com/tox-dev/tox-uv#tox-uv" @@ -69,7 +69,7 @@ test = [ ] type = [ "mypy==1.14.1", { include-group = "test" } ] lint = [ "pre-commit-uv>=4.1.4" ] -pkg-meta = [ "check-wheel-contents>=0.6.1", "twine>=6.1", "uv>=0.5.21" ] +pkg-meta = [ "check-wheel-contents>=0.6.1", "twine>=6.1", "uv>=0.5.22" ] [tool.hatch] build.hooks.vcs.version-file = "src/tox_uv/version.py" diff --git a/src/tox_uv/_venv.py b/src/tox_uv/_venv.py index aa6fd42..e2da7a9 100644 --- a/src/tox_uv/_venv.py +++ b/src/tox_uv/_venv.py @@ -4,6 +4,7 @@ import json import logging +import os import sys from abc import ABC from functools import cached_property @@ -73,7 +74,9 @@ def register_config(self) -> None: self.conf.add_config( keys=["uv_python_preference"], of_type=cast("Type[Optional[PythonPreference]]", Optional[PythonPreference]), # noqa: UP006 - default=lambda conf, name: self.environment_variables.get("UV_PYTHON_PREFERENCE", "system"), # noqa: ARG005 + # use os.environ here instead of self.environment_variables as this value is needed to create the virtual + # environment, if environment variables use env_site_packages_dir we would run into a chicken-egg problem. + default=lambda conf, name: os.environ.get("UV_PYTHON_PREFERENCE", "system"), # noqa: ARG005 desc=( "Whether to prefer using Python installations that are already" " present on the system, or those that are downloaded and" @@ -240,10 +243,10 @@ def env_python(self) -> Path: def env_site_package_dir(self) -> Path: if sys.platform == "win32": # pragma: win32 cover return self.venv_dir / "Lib" / "site-packages" - else: # pragma: win32 no cover # noqa: RET505 - py = self._py_info - impl = "pypy" if py.implementation == "pypy" else "python" - return self.venv_dir / "lib" / f"{impl}{py.version_dot}" / "site-packages" + # pragma: win32 no cover + py = self._py_info + impl = "pypy" if py.implementation == "pypy" else "python" + return self.venv_dir / "lib" / f"{impl}{py.version_dot}" / "site-packages" def env_version_spec(self) -> str: base = self.base_python.version_info diff --git a/tests/test_tox_uv_venv.py b/tests/test_tox_uv_venv.py index ea1e47d..84de722 100644 --- a/tests/test_tox_uv_venv.py +++ b/tests/test_tox_uv_venv.py @@ -71,6 +71,18 @@ def test_uv_venv_preference_override_via_env_var( assert got == "only-managed" +def test_uv_venv_preference_override_via_env_var_and_set_env_depends_on_py( + tox_project: ToxProjectCreator, monkeypatch: pytest.MonkeyPatch +) -> None: + project = tox_project({"tox.ini": "[testenv]\nset_env=A={env_site_packages_dir}"}) + monkeypatch.setenv("UV_PYTHON_PREFERENCE", "only-managed") + + result = project.run("c", "-k", "set_env") + result.assert_success() + + assert str(project.path) in result.out + + def test_uv_venv_spec(tox_project: ToxProjectCreator) -> None: ver = sys.version_info project = tox_project({"tox.ini": f"[testenv]\npackage=skip\nbase_python={ver.major}.{ver.minor}"}) diff --git a/tox.ini b/tox.ini index 1cd4f95..98d8672 100644 --- a/tox.ini +++ b/tox.ini @@ -1,7 +1,7 @@ [tox] requires = tox>=4.24.1 - tox-uv>=1.19.1 + tox-uv>=1.20 env_list = fix 3.13