From a762b108748969b9c74657ddc6c075968ec99c4a Mon Sep 17 00:00:00 2001 From: Simon Brugman Date: Mon, 19 Feb 2024 12:19:17 +0100 Subject: [PATCH 1/3] Add `py.typed` to Python package `uv` See https://github.com/pola-rs/polars/issues/932 --- python/uv/py.typed | 1 + 1 file changed, 1 insertion(+) create mode 100644 python/uv/py.typed diff --git a/python/uv/py.typed b/python/uv/py.typed new file mode 100644 index 000000000000..8b137891791f --- /dev/null +++ b/python/uv/py.typed @@ -0,0 +1 @@ + From c31ecf0b9c4ca2cd42c3ea8c38e72642d3267ffe Mon Sep 17 00:00:00 2001 From: Simon Brugman Date: Mon, 19 Feb 2024 12:19:48 +0100 Subject: [PATCH 2/3] Expose __main__ functions with warning --- python/uv/__init__.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/python/uv/__init__.py b/python/uv/__init__.py index e69de29bb2d1..c118437e9216 100644 --- a/python/uv/__init__.py +++ b/python/uv/__init__.py @@ -0,0 +1,8 @@ +""" +The Python API of `uv` is not guaranteed to be stable and may chance at any time. It is +strongly discouraged to import from our `uv` Python module, but you can do so at your +own risk. +""" +from uv.__main__ import detect_virtualenv, find_uv_bin + +__all__ = ["detect_virtualenv", "find_uv_bin"] From 72f7005fcd0a2fdb402582f852127d4e2cfac3fa Mon Sep 17 00:00:00 2001 From: Simon Brugman Date: Mon, 19 Feb 2024 12:26:31 +0100 Subject: [PATCH 3/3] Python linting - Explicit `check=False` - Sort functions in their call order - Use Pathlib for paths --- python/uv/__main__.py | 56 ++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/python/uv/__main__.py b/python/uv/__main__.py index 0d57b35b1f8e..26a1ff1de7f0 100644 --- a/python/uv/__main__.py +++ b/python/uv/__main__.py @@ -1,25 +1,7 @@ import os import sys import sysconfig - - -def detect_virtualenv() -> str: - """ - Find the virtual environment path for the current Python executable. - """ - - # If it's already set, then just use it - value = os.getenv("VIRTUAL_ENV") - if value: - return value - - # Otherwise, check if we're in a venv - venv_marker = os.path.join(sys.prefix, "pyvenv.cfg") - - if os.path.exists(venv_marker): - return sys.prefix - - return "" +from pathlib import Path def find_uv_bin() -> str: @@ -27,9 +9,9 @@ def find_uv_bin() -> str: uv_exe = "uv" + sysconfig.get_config_var("EXE") - path = os.path.join(sysconfig.get_path("scripts"), uv_exe) - if os.path.isfile(path): - return path + path = Path(sysconfig.get_path("scripts")) / uv_exe + if path.is_file(): + return str(path) if sys.version_info >= (3, 10): user_scheme = sysconfig.get_preferred_scheme("user") @@ -40,11 +22,31 @@ def find_uv_bin() -> str: else: user_scheme = "posix_user" - path = os.path.join(sysconfig.get_path("scripts", scheme=user_scheme), uv_exe) - if os.path.isfile(path): - return path + path = Path(sysconfig.get_path("scripts", scheme=user_scheme)) / uv_exe + if path.is_file(): + return str(path) - raise FileNotFoundError(path) + msg = str(path) + raise FileNotFoundError(msg) + + +def detect_virtualenv() -> str: + """ + Find the virtual environment path for the current Python executable. + """ + + # If it's already set, then just use it + value = os.getenv("VIRTUAL_ENV") + if value: + return value + + # Otherwise, check if we're in a venv + venv_marker = Path(sys.prefix) / "pyvenv.cfg" + + if venv_marker.exists(): + return sys.prefix + + return "" if __name__ == "__main__": @@ -58,7 +60,7 @@ def find_uv_bin() -> str: if sys.platform == "win32": import subprocess - completed_process = subprocess.run([uv, *sys.argv[1:]], env=env) + completed_process = subprocess.run([uv, *sys.argv[1:]], env=env, check=False) sys.exit(completed_process.returncode) else: os.execvpe(uv, [uv, *sys.argv[1:]], env=env)