From 435dc32d259d3e4427bbdfa0b684a956a1ff85bf Mon Sep 17 00:00:00 2001 From: Vladimir Petrigo Date: Thu, 9 Jan 2025 19:30:21 +0100 Subject: [PATCH 1/9] hatch: cli: application: Switch to uv Address: juftin/hatch-pip-compile#85 # Conflicts: # src/hatch/cli/application.py --- src/hatch/cli/application.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hatch/cli/application.py b/src/hatch/cli/application.py index 58cc826fe..dbe1c63f1 100644 --- a/src/hatch/cli/application.py +++ b/src/hatch/cli/application.py @@ -160,7 +160,7 @@ def ensure_plugin_dependencies(self, dependencies: list[Requirement], *, wait_me if dependencies_in_sync(dependencies): return - pip_command = [sys.executable, '-u', '-m', 'pip'] + pip_command = [sys.executable, '-u', '-m', 'uv', 'pip'] pip_command.extend(['install', '--disable-pip-version-check']) From fc37df40f07c95437a48fdd608840ac0e269298f Mon Sep 17 00:00:00 2001 From: Vladimir Petrigo Date: Thu, 9 Jan 2025 19:31:15 +0100 Subject: [PATCH 2/9] tests: Update asserts after switching to uv --- tests/conftest.py | 4 ++++ tests/helpers/helpers.py | 1 + 2 files changed, 5 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index a2fbb9fdf..f3e8f0688 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -440,6 +440,10 @@ def _mock(command, **kwargs): mocked_subprocess_run(command, **kwargs) return mocked_subprocess_run + if command[:6] == [sys.executable, '-u', '-m', 'uv', 'pip', 'install']: + mocked_subprocess_run(command, **kwargs) + return mocked_subprocess_run + if command[:3] == [sys.executable, 'self', 'python-path']: return mocker.MagicMock(returncode=0, stdout=sys.executable.encode()) diff --git a/tests/helpers/helpers.py b/tests/helpers/helpers.py index a5557639e..dbd78127d 100644 --- a/tests/helpers/helpers.py +++ b/tests/helpers/helpers.py @@ -52,6 +52,7 @@ def assert_plugin_installation(subprocess_run, dependencies: list[str], *, verbo sys.executable, '-u', '-m', + 'uv', 'pip', 'install', '--disable-pip-version-check', From f32f360f80ed67446ea4cd293af2969b77b71f5e Mon Sep 17 00:00:00 2001 From: Vladimir Petrigo Date: Sat, 11 Jan 2025 23:11:31 +0100 Subject: [PATCH 3/9] hatch: tests: Update uv command search --- src/hatch/cli/application.py | 5 ++++- tests/conftest.py | 4 +++- tests/helpers/helpers.py | 12 +++--------- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/hatch/cli/application.py b/src/hatch/cli/application.py index dbe1c63f1..21f780e55 100644 --- a/src/hatch/cli/application.py +++ b/src/hatch/cli/application.py @@ -12,6 +12,8 @@ from hatch.utils.platform import Platform from hatch.utils.runner import ExecutionContext +from uv import find_uv_bin + if TYPE_CHECKING: from collections.abc import Generator @@ -160,7 +162,8 @@ def ensure_plugin_dependencies(self, dependencies: list[Requirement], *, wait_me if dependencies_in_sync(dependencies): return - pip_command = [sys.executable, '-u', '-m', 'uv', 'pip'] + uv_bin = find_uv_bin() + pip_command = [uv_bin, 'pip'] pip_command.extend(['install', '--disable-pip-version-check']) diff --git a/tests/conftest.py b/tests/conftest.py index f3e8f0688..b217b70d9 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -14,6 +14,7 @@ from click.testing import CliRunner as __CliRunner from filelock import FileLock from platformdirs import user_cache_dir, user_data_dir +from uv import find_uv_bin from hatch.config.constants import AppEnvVars, ConfigEnvVars, PublishEnvVars from hatch.config.user import ConfigFile @@ -440,7 +441,8 @@ def _mock(command, **kwargs): mocked_subprocess_run(command, **kwargs) return mocked_subprocess_run - if command[:6] == [sys.executable, '-u', '-m', 'uv', 'pip', 'install']: + uv_bin = find_uv_bin() + if command[:3] == [uv_bin, 'pip', 'install']: mocked_subprocess_run(command, **kwargs) return mocked_subprocess_run diff --git a/tests/helpers/helpers.py b/tests/helpers/helpers.py index dbd78127d..1891d5bc6 100644 --- a/tests/helpers/helpers.py +++ b/tests/helpers/helpers.py @@ -12,6 +12,7 @@ from unittest.mock import call import tomli_w +from uv import find_uv_bin from hatch.config.user import RootConfig from hatch.env.utils import add_verbosity_flag @@ -48,15 +49,8 @@ def get_current_timestamp(): def assert_plugin_installation(subprocess_run, dependencies: list[str], *, verbosity=0, count=1): - command = [ - sys.executable, - '-u', - '-m', - 'uv', - 'pip', - 'install', - '--disable-pip-version-check', - ] + uv_bin = find_uv_bin() + command = [uv_bin, 'pip', 'install', '--disable-pip-version-check'] add_verbosity_flag(command, verbosity, adjustment=-1) command.extend(dependencies) From 376784d31c2b75d77f84dabff29bd8bc9e992332 Mon Sep 17 00:00:00 2001 From: Vladimir Petrigo Date: Sat, 11 Jan 2025 23:13:06 +0100 Subject: [PATCH 4/9] hatch: Update to use find_uv_bin() --- src/hatch/cli/application.py | 7 ++++--- tests/helpers/helpers.py | 10 +++++++++- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/hatch/cli/application.py b/src/hatch/cli/application.py index 21f780e55..614744dd6 100644 --- a/src/hatch/cli/application.py +++ b/src/hatch/cli/application.py @@ -1,10 +1,13 @@ from __future__ import annotations import os +import pathlib import sys from functools import cached_property from typing import TYPE_CHECKING, cast +from uv import find_uv_bin + from hatch.cli.terminal import Terminal from hatch.config.user import ConfigFile, RootConfig from hatch.project.core import Project @@ -12,8 +15,6 @@ from hatch.utils.platform import Platform from hatch.utils.runner import ExecutionContext -from uv import find_uv_bin - if TYPE_CHECKING: from collections.abc import Generator @@ -165,7 +166,7 @@ def ensure_plugin_dependencies(self, dependencies: list[Requirement], *, wait_me uv_bin = find_uv_bin() pip_command = [uv_bin, 'pip'] - pip_command.extend(['install', '--disable-pip-version-check']) + pip_command.extend(['install', '--directory', str(pathlib.Path(sys.executable).parent.parent), '--disable-pip-version-check']) # Default to -1 verbosity add_verbosity_flag(pip_command, self.verbosity, adjustment=-1) diff --git a/tests/helpers/helpers.py b/tests/helpers/helpers.py index 1891d5bc6..4939e3bc5 100644 --- a/tests/helpers/helpers.py +++ b/tests/helpers/helpers.py @@ -3,6 +3,7 @@ import importlib import json import os +import pathlib import re import sys from datetime import datetime, timezone @@ -50,7 +51,14 @@ def get_current_timestamp(): def assert_plugin_installation(subprocess_run, dependencies: list[str], *, verbosity=0, count=1): uv_bin = find_uv_bin() - command = [uv_bin, 'pip', 'install', '--disable-pip-version-check'] + command = [ + uv_bin, + 'pip', + 'install', + '--disable-pip-version-check', + '--directory', + str(pathlib.Path(sys.executable).parent.parent), + ] add_verbosity_flag(command, verbosity, adjustment=-1) command.extend(dependencies) From c1f2e75b8ff519c5bf6de0f8f024df7a5a0136a9 Mon Sep 17 00:00:00 2001 From: Vladimir Petrigo Date: Tue, 14 Jan 2025 09:47:34 +0100 Subject: [PATCH 5/9] hatch: Update the implementation to use --python switch Switch from --directory to --python switch to simplify `uv pip install` command. --- src/hatch/cli/application.py | 2 +- tests/helpers/helpers.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/hatch/cli/application.py b/src/hatch/cli/application.py index 614744dd6..2c83783d6 100644 --- a/src/hatch/cli/application.py +++ b/src/hatch/cli/application.py @@ -166,7 +166,7 @@ def ensure_plugin_dependencies(self, dependencies: list[Requirement], *, wait_me uv_bin = find_uv_bin() pip_command = [uv_bin, 'pip'] - pip_command.extend(['install', '--directory', str(pathlib.Path(sys.executable).parent.parent), '--disable-pip-version-check']) + pip_command.extend(['install', '--disable-pip-version-check', '--python', sys.executable]) # Default to -1 verbosity add_verbosity_flag(pip_command, self.verbosity, adjustment=-1) diff --git a/tests/helpers/helpers.py b/tests/helpers/helpers.py index 4939e3bc5..1b2f5f75f 100644 --- a/tests/helpers/helpers.py +++ b/tests/helpers/helpers.py @@ -56,8 +56,8 @@ def assert_plugin_installation(subprocess_run, dependencies: list[str], *, verbo 'pip', 'install', '--disable-pip-version-check', - '--directory', - str(pathlib.Path(sys.executable).parent.parent), + '--python', + sys.executable, ] add_verbosity_flag(command, verbosity, adjustment=-1) command.extend(dependencies) From a589528509b2abe2bd986e04e387aa67cb5e88d9 Mon Sep 17 00:00:00 2001 From: Vladimir Petrigo Date: Fri, 14 Nov 2025 18:52:08 +0100 Subject: [PATCH 6/9] misc: Remove unused pathlib imports and switch to consistent string quoting --- src/hatch/cli/application.py | 5 ++--- tests/helpers/helpers.py | 9 ++++----- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/hatch/cli/application.py b/src/hatch/cli/application.py index e6e707a49..f08609a80 100644 --- a/src/hatch/cli/application.py +++ b/src/hatch/cli/application.py @@ -1,7 +1,6 @@ from __future__ import annotations import os -import pathlib import sys from functools import cached_property from typing import TYPE_CHECKING, cast @@ -167,9 +166,9 @@ def ensure_plugin_dependencies(self, dependencies: list[Dependency], *, wait_mes return uv_bin = find_uv_bin() - pip_command = [uv_bin, 'pip'] + pip_command = [uv_bin, "pip"] - pip_command.extend(['install', '--disable-pip-version-check', '--python', sys.executable]) + pip_command.extend(["install", "--disable-pip-version-check", "--python", sys.executable]) # Default to -1 verbosity add_verbosity_flag(pip_command, self.verbosity, adjustment=-1) diff --git a/tests/helpers/helpers.py b/tests/helpers/helpers.py index 087939c10..7b739d3c0 100644 --- a/tests/helpers/helpers.py +++ b/tests/helpers/helpers.py @@ -3,7 +3,6 @@ import importlib import json import os -import pathlib import re import sys from datetime import datetime, timezone @@ -53,10 +52,10 @@ def assert_plugin_installation(subprocess_run, dependencies: list[str], *, verbo uv_bin = find_uv_bin() command = [ uv_bin, - 'pip', - 'install', - '--disable-pip-version-check', - '--python', + "pip", + "install", + "--disable-pip-version-check", + "--python", sys.executable, ] add_verbosity_flag(command, verbosity, adjustment=-1) From 27e9218067d287e532216b36e8fc5e66b3507b96 Mon Sep 17 00:00:00 2001 From: Vladimir Petrigo Date: Wed, 26 Nov 2025 19:38:55 +0100 Subject: [PATCH 7/9] tests: Use consistent double-quoted strings in conftest.py --- tests/conftest.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index dcf43cab9..e65c3fc55 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -443,11 +443,11 @@ def _mock(command, **kwargs): return mocked_subprocess_run uv_bin = find_uv_bin() - if command[:3] == [uv_bin, 'pip', 'install']: + if command[:3] == [uv_bin, "pip", "install"]: mocked_subprocess_run(command, **kwargs) return mocked_subprocess_run - if command[:3] == [sys.executable, 'self', 'python-path']: + if command[:3] == [sys.executable, "self", "python-path"]: return mocker.MagicMock(returncode=0, stdout=sys.executable.encode()) return subprocess_run(command, **kwargs) # no cov From be65450f208d98f805f5bc28a6c4071aa41e1720 Mon Sep 17 00:00:00 2001 From: Vladimir Petrigo Date: Wed, 26 Nov 2025 23:04:13 +0100 Subject: [PATCH 8/9] tests: Handle commands with "hatchling" in conftest.py mock --- tests/conftest.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/conftest.py b/tests/conftest.py index e65c3fc55..f85073afe 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -438,6 +438,9 @@ def mock_plugin_installation(mocker): def _mock(command, **kwargs): if isinstance(command, list): + if any(arg.startswith("hatchling") for arg in command): + return subprocess_run(command, **kwargs) + if command[:5] == [sys.executable, "-u", "-m", "pip", "install"]: mocked_subprocess_run(command, **kwargs) return mocked_subprocess_run From c815f4e1f5aa93b061f1f0bc1ba6f07197149949 Mon Sep 17 00:00:00 2001 From: Vladimir Petrigo Date: Fri, 6 Mar 2026 15:42:17 +0100 Subject: [PATCH 9/9] fix: Make find_uv_bin import lazy --- src/hatch/cli/application.py | 4 ++-- tests/conftest.py | 3 ++- tests/helpers/helpers.py | 3 ++- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/hatch/cli/application.py b/src/hatch/cli/application.py index 23935f2c7..574e01688 100644 --- a/src/hatch/cli/application.py +++ b/src/hatch/cli/application.py @@ -5,8 +5,6 @@ from functools import cached_property from typing import TYPE_CHECKING, cast -from uv import find_uv_bin - from hatch.cli.terminal import Terminal from hatch.config.user import ConfigFile, RootConfig from hatch.project.core import Project @@ -147,6 +145,8 @@ def ensure_plugin_dependencies(self, dependencies: list[Dependency], *, wait_mes if not dependencies: return + from uv import find_uv_bin + from hatch.dep.sync import InstalledDistributions from hatch.env.utils import add_verbosity_flag diff --git a/tests/conftest.py b/tests/conftest.py index 2ea5bf52f..015dd88f5 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -14,7 +14,6 @@ from click.testing import CliRunner as __CliRunner from filelock import FileLock from platformdirs import user_cache_dir, user_data_dir -from uv import find_uv_bin from hatch.config.constants import AppEnvVars, ConfigEnvVars, PublishEnvVars from hatch.config.user import ConfigFile @@ -449,6 +448,8 @@ def mock_process(command, **kwargs): @pytest.fixture def mock_plugin_installation(mocker): + from uv import find_uv_bin + subprocess_run = subprocess.run mocked_subprocess_run = mocker.MagicMock(returncode=0) diff --git a/tests/helpers/helpers.py b/tests/helpers/helpers.py index 7b739d3c0..570fc1c53 100644 --- a/tests/helpers/helpers.py +++ b/tests/helpers/helpers.py @@ -12,7 +12,6 @@ from unittest.mock import call import tomli_w -from uv import find_uv_bin from hatch.config.user import RootConfig from hatch.env.utils import add_verbosity_flag @@ -49,6 +48,8 @@ def get_current_timestamp(): def assert_plugin_installation(subprocess_run, dependencies: list[str], *, verbosity=0, count=1): + from uv import find_uv_bin + uv_bin = find_uv_bin() command = [ uv_bin,