Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Py.typed #1685

Closed
wants to merge 3 commits into from
Closed
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
8 changes: 8 additions & 0 deletions python/uv/__init__.py
Original file line number Diff line number Diff line change
@@ -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.
Comment on lines +2 to +4
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.
The Python API of `uv` is not guaranteed to be stable and may change at any time.

Fixed a typo and this seems sufficient. I was only strongly discouraging people from importing from __main__.

"""
from uv.__main__ import detect_virtualenv, find_uv_bin
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not import then from __main__, let's do it the other way around (define them here and import them there).

I think detect_virtualenv should remain private as well.


__all__ = ["detect_virtualenv", "find_uv_bin"]
56 changes: 29 additions & 27 deletions python/uv/__main__.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,17 @@
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
Copy link
Member

@zanieb zanieb Feb 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are explicitly not using pathlib because it's slower to import

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you revert those changes?



def find_uv_bin() -> str:
"""Return the uv binary path."""

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")
Expand All @@ -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__":
Expand All @@ -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)
1 change: 1 addition & 0 deletions python/uv/py.typed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@