Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/changelog/+relative-try-first.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Discovery now finds an interpreter given as a relative path such as ``./venv/bin/python`` when ``try_first_with`` lists
the same file under another name or case, its base interpreter, or another virtual environment on that base, instead of
finding nothing.
6 changes: 3 additions & 3 deletions docs/explanation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ order. It stops as soon as it finds an interpreter that matches your spec.
flowchart TD
Start["get_interpreter()"] --> AbsPath{"Is spec an<br>absolute path?"}
AbsPath -->|Yes| TryAbs["Use path directly"]
AbsPath -->|No| TryFirst["try_first_with paths"]
TryFirst --> RelPath{"Does spec name a<br>relative directory?"}
AbsPath -->|No| RelPath{"Does spec name a<br>relative directory?"}
RelPath -->|Yes| TryRel["Resolve relative to cwd"]
RelPath -->|No| Bare{"Bare executable name?"}
RelPath -->|No| TryFirst["try_first_with paths"]
TryFirst --> Bare{"Bare executable name?"}
Bare -->|Yes| PATH
Bare -->|No| Current["Current interpreter"]
Current --> Win{"Windows?"}
Expand Down
12 changes: 7 additions & 5 deletions src/python_discovery/_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,17 @@ def _propose_explicit(
env: Mapping[str, str],
tested_exes: set[str],
) -> Generator[tuple[PythonInfo | None, bool], None, None]:
# Unparsed bare names reach spec.path; only an explicit directory should search the working directory. Probe that
# path before try_first_with: only it can satisfy the spec, and a hint on the same file or base interpreter would
# otherwise fail first and get it skipped as a duplicate.
if spec.path is not None and os.path.dirname(spec.path) and (exe_raw := _check_exe(spec.path, tested_exes)):
yield PythonInfo.from_exe(exe_raw, cache, env=env), True

for py_exe in try_first_with:
if Path(py_exe).exists() and (exe_raw := _check_exe(py_exe, tested_exes)):
yield PythonInfo.from_exe(exe_raw, cache, env=env), True

if spec.path is not None:
# Unparsed bare names reach spec.path; only an explicit directory should search the working directory.
if os.path.dirname(spec.path) and (exe_raw := _check_exe(spec.path, tested_exes)):
yield PythonInfo.from_exe(exe_raw, cache, env=env), True
else:
if spec.path is None:
yield from _propose_current_and_windows(spec, cache, env, tested_exes)


Expand Down
30 changes: 30 additions & 0 deletions tests/test_discovery.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,36 @@ def test_discovery_absolute_path_with_try_first(
assert Path(interpreter.executable) == good_exe


@pytest.mark.parametrize(
"hint",
[
pytest.param("sibling", id="sibling-venv"),
pytest.param("base", id="base-interpreter"),
pytest.param("case", id="other-case", marks=pytest.mark.skipif(not IS_WIN, reason="case-insensitive paths")),
],
)
def test_discovery_relative_path_with_try_first(
tmp_path: Path, session_cache: DiskCache, monkeypatch: pytest.MonkeyPatch, hint: str
) -> None:
for name in ("good", "bad"):
check_call([sys.executable, "-m", "venv", "--without-pip", str(tmp_path / name)])
monkeypatch.chdir(tmp_path)
spec = str(Path("good", _SCRIPTS_DIR, _PYTHON_NAME))
good_exe = tmp_path / spec
first = {
"sibling": tmp_path / "bad" / _VENV_PYTHON,
"base": PythonInfo.current_system(session_cache).system_executable,
"case": str(good_exe).upper(),
}[hint]
env = {**os.environ, "PATH": "", "UV_PYTHON_INSTALL_DIR": str(tmp_path / "missing")}

found = get_interpreter(spec, try_first_with=[str(first)], cache=session_cache, env=env)
listed = iter_interpreters(spec, try_first_with=[str(first)], cache=session_cache, env=env)

assert found is not None
assert (Path(found.executable), [Path(info.executable) for info in listed]) == (good_exe, [good_exe])


def test_discovery_venvs_on_one_base_keep_their_executable(tmp_path: Path, session_cache: DiskCache) -> None:
scripts_dir = _SCRIPTS_DIR
exe_name = _PYTHON_NAME
Expand Down