Skip to content

Commit

Permalink
Fix UV_PYTHON_PREFERENCE reading breaks tox when env_site_packages_di…
Browse files Browse the repository at this point in the history
…r is in set_env (#160)

Signed-off-by: Bernát Gábor <[email protected]>
  • Loading branch information
gaborbernat authored Jan 22, 2025
1 parent 9d847c4 commit b769f84
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 10 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand Down
13 changes: 8 additions & 5 deletions src/tox_uv/_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import json
import logging
import os
import sys
from abc import ABC
from functools import cached_property
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions tests/test_tox_uv_venv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}"})
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[tox]
requires =
tox>=4.24.1
tox-uv>=1.19.1
tox-uv>=1.20
env_list =
fix
3.13
Expand Down

0 comments on commit b769f84

Please sign in to comment.