Skip to content

Commit 53b46e9

Browse files
authored
fix: no-managed-python collision with python-preference (#253) (#254)
1 parent 31090f2 commit 53b46e9

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

src/tox_uv/_venv.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,20 @@ def register_config(self) -> None:
7272
desc="create virtual environments that also have access to globally installed packages.",
7373
)
7474

75+
def uv_python_preference_default(conf: object, name: object) -> str: # noqa: ARG001
76+
return (
77+
"none"
78+
if {"UV_NO_MANAGED_PYTHON", "UV_MANAGED_PYTHON"} & set(os.environ)
79+
else os.environ.get("UV_PYTHON_PREFERENCE", "system")
80+
)
81+
7582
# The cast(...) might seems superfluous but removing it makes mypy crash. The problem isy on tox typing side.
7683
self.conf.add_config(
7784
keys=["uv_python_preference"],
7885
of_type=cast("Type[Optional[PythonPreference]]", Optional[PythonPreference]), # type: ignore[valid-type] # noqa: UP006
7986
# use os.environ here instead of self.environment_variables as this value is needed to create the virtual
8087
# environment, if environment variables use env_site_packages_dir we would run into a chicken-egg problem.
81-
default=lambda conf, name: os.environ.get("UV_PYTHON_PREFERENCE", "system"), # noqa: ARG005
88+
default=uv_python_preference_default,
8289
desc=(
8390
"Whether to prefer using Python installations that are already"
8491
" present on the system, or those that are downloaded and"

tests/test_tox_uv_venv.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,25 @@ def test_uv_venv_preference_system_by_default(tox_project: ToxProjectCreator) ->
5757
assert got == "system"
5858

5959

60+
@pytest.mark.usefixtures("clear_python_preference_env_var")
61+
@pytest.mark.parametrize("env_var", ["UV_NO_MANAGED_PYTHON", "UV_MANAGED_PYTHON"])
62+
def test_uv_venv_preference_not_set_if_uv_no_managed_python(
63+
tox_project: ToxProjectCreator, monkeypatch: pytest.MonkeyPatch, env_var: str
64+
) -> None:
65+
# --(no-)managed-python cannot be used together with --python-preference
66+
project = tox_project({"tox.ini": "[testenv]"})
67+
monkeypatch.setenv(env_var, "True")
68+
69+
result = project.run("c", "-k", "uv_python_preference")
70+
result.assert_success()
71+
72+
parser = ConfigParser()
73+
parser.read_string(result.out)
74+
got = parser["testenv:py"]
75+
76+
assert got.get("uv_python_preference") == "none"
77+
78+
6079
def test_uv_venv_preference_override_via_env_var(
6180
tox_project: ToxProjectCreator, monkeypatch: pytest.MonkeyPatch
6281
) -> None:

0 commit comments

Comments
 (0)