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

Modernize Python code #1769

Closed
wants to merge 1 commit 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
2 changes: 1 addition & 1 deletion python/uv/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
def find_uv_bin() -> str:
"""Return the uv binary path."""

uv_exe = "uv" + sysconfig.get_config_var("EXE")
uv_exe = f"uv{sysconfig.get_config_var('EXE')}"

path = os.path.join(sysconfig.get_path("scripts"), uv_exe)
if os.path.isfile(path):
Expand Down
19 changes: 9 additions & 10 deletions python/uv/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,34 @@ 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:
# If it's already set, then use it
if value := os.getenv("VIRTUAL_ENV"):
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 ""


def _run() -> None:
uv = os.fsdecode(find_uv_bin())

env = os.environ.copy()
venv = _detect_virtualenv()
if venv:
if venv := _detect_virtualenv():
env.setdefault("VIRTUAL_ENV", venv)

cmd = [uv, *sys.argv[1:]]

if sys.platform == "win32":
import subprocess

completed_process = subprocess.run([uv, *sys.argv[1:]], env=env)
sys.exit(completed_process.returncode)
else:
os.execvpe(uv, [uv, *sys.argv[1:]], env=env)
completed_process = subprocess.run(cmd, env=env)
raise SystemExit(completed_process.returncode)

os.execvpe(uv, cmd, env=env)
Comment on lines 33 to +39
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, I kinda prefer the if/else here -- I feel like it makes it more explicit that it's a binary choice: for Windows we do one thing; for all other platforms, we do a different thing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But raise SystemExit makes it redundant, 🤔 so I prefer this 😆 the raise makes it explicit that the execution of that branch ends there.

Copy link
Member

@AlexWaygood AlexWaygood Feb 20, 2024

Choose a reason for hiding this comment

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

I agree with you that calling sys.exit is redundant, since all it does internally is raise SystemExit, so I too prefer raising SystemExit directly as a matter of style. But I'd still personally find it more readable, in this specific instance, to do this:

    if sys.platform == "win32":
        import subprocess

        completed_process = subprocess.run(cmd, env=env)
        raise SystemExit(completed_process.returncode)
    else:
        os.execvpe(uv, cmd, env=env)

even though, as you say, the else is, strictly speaking, redundant.

Make a bikesheddy PR, expect bikesheddy review comments ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I prefer my style, but if there's one more maintainer vote on the previous form can revert 👍



if __name__ == "__main__":
Expand Down