Skip to content

Commit

Permalink
Various cleanups (#1416)
Browse files Browse the repository at this point in the history
  • Loading branch information
ofek authored Apr 22, 2024
1 parent ceee533 commit 59fef2a
Show file tree
Hide file tree
Showing 18 changed files with 39 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-hatch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ jobs:
CARGO: cargo
CARGO_BUILD_TARGET: ${{ matrix.job.target }}
PYAPP_REPO: pyapp
PYAPP_VERSION: "0.17.0"
PYAPP_VERSION: "0.18.0"
PYAPP_UV_ENABLED: "true"
PYAPP_PASS_LOCATION: "true"

Expand Down
6 changes: 3 additions & 3 deletions docs/blog/posts/release-hatch-1100.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@ See the [tutorial](../../tutorials/testing/overview.md) for a detailed walk-thro

## UV

The package installer [UV](https://github.com/astral-sh/uv), brought to you by the same folks behind [Ruff](https://github.com/astral-sh/ruff), is now supported. In any environment, you can enable the `uv` option to use UV in place of [virtualenv](https://github.com/pypa/virtualenv) & [pip](https://github.com/pypa/pip) for virtual environment creation and dependency management, respectively. This often results in a significant performance benefit.
The package installer [UV](https://github.com/astral-sh/uv), brought to you by the same folks behind [Ruff](https://github.com/astral-sh/ruff), is now supported. In any environment, you can set the `installer` option to `uv` to use UV in place of [virtualenv](https://github.com/pypa/virtualenv) & [pip](https://github.com/pypa/pip) for virtual environment creation and dependency management, respectively. This often results in a significant performance benefit.

For example, if you wanted to enable this functionality for the [default](../../config/environment/overview.md#inheritance) environment, you could set the following:

```toml config-example
[tool.hatch.envs.default]
uv = true
installer = "uv"
```

Semi-internal environments like those used for [testing](../../config/internal/testing.md) and [static analysis](../../config/internal/static-analysis.md) have this option enabled by default.
Semi-internal environments like those used for [testing](../../config/internal/testing.md) and [static analysis](../../config/internal/static-analysis.md) have this UV enabled by default.

See the [how-to guide](../../how-to/environment/select-installer.md) for more information about switching the installer.

Expand Down
2 changes: 1 addition & 1 deletion docs/config/internal/static-analysis.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ By default, [UV is enabled](../../how-to/environment/select-installer.md). You m

```toml config-example
[tool.hatch.envs.hatch-static-analysis]
uv = false
installer = "pip"
```

## Default settings
Expand Down
2 changes: 1 addition & 1 deletion docs/config/internal/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,5 @@ By default, [UV is enabled](../../how-to/environment/select-installer.md). You m

```toml config-example
[tool.hatch.envs.hatch-test]
uv = false
installer = "pip"
```
4 changes: 2 additions & 2 deletions docs/history/hatch.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Upgrade default CPython distributions to 20240415
- Upgrade default PyPy distributions to 7.3.15
- Upgrade Ruff to 0.4.1
- Upgrade PyApp to 0.17.0 for binary builds
- Bump the minimum supported version of Hatchling to 1.24.1
- Upgrade PyApp to 0.18.0 for binary builds
- Bump the minimum supported version of Hatchling to 1.24.2

***Fixed:***

Expand Down
2 changes: 1 addition & 1 deletion docs/how-to/environment/dependency-resolution.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ PIP_EXTRA_INDEX_URL = "https://token:{env:GITLAB_API_TOKEN}@gitlab.com/api/v4/gr

## UV

If you're [using UV](select-installer.md), a different set of [environment variables](https://github.com/astral-sh/uv/tree/0.1.31#environment-variables) are available to configure its behavior. The previous example would look like this instead:
If you're [using UV](select-installer.md), a different set of [environment variables](https://github.com/astral-sh/uv/tree/0.1.35#environment-variables) are available to configure its behavior. The previous example would look like this instead:

```toml config-example
[tool.hatch.envs.default.env-vars]
Expand Down
17 changes: 7 additions & 10 deletions docs/how-to/environment/select-installer.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ The [virtual](../../plugins/environment/virtual.md) environment type by default
!!! warning "caveat"
UV is under active development and may not work for all dependencies.

To do so, enable the `uv` [option](../../plugins/environment/virtual.md#options). For example, if you wanted to enable this functionality for the [default](../../config/environment/overview.md#inheritance) environment, you could set the following:
To do so, set the `installer` [option](../../plugins/environment/virtual.md#options) to `uv`. For example, if you wanted to enable this functionality for the [default](../../config/environment/overview.md#inheritance) environment, you could set the following:

```toml config-example
[tool.hatch.envs.default]
uv = true
installer = "uv"
```

!!! tip
All environments that enable UV will have the path to `uv` available as the `HATCH_UV` environment variable.
All environments that enable UV will have the path to UV available as the `HATCH_UV` environment variable.

## Configuring the version

Expand All @@ -32,22 +32,19 @@ dependencies = [

## Externally managed

If you want to manage UV yourself, you can expose it to Hatch by setting the `HATCH_ENV_TYPE_VIRTUAL_UV_PATH` environment variable which should be the absolute path to a UV binary for Hatch to use instead. This implicitly [enables](#enabling-uv) the `uv` option.
If you want to manage UV yourself, you can expose it to Hatch by setting the `HATCH_ENV_TYPE_VIRTUAL_UV_PATH` environment variable which should be the absolute path to a UV binary for Hatch to use instead. This implicitly [enables UV](#enabling-uv).

## Installer script alias

If you have [scripts](../../config/environment/overview.md#scripts) or [commands](../../config/environment/overview.md#commands) that call `pip`, it may be useful to alias the `uv pip` command to `pip` so that you can use the same commands for both methods of configuration and retain your muscle memory. The following is an example of a matrix that [conditionally](../../config/environment/advanced.md#option-overrides) enables UV and sets the alias:

```toml config-example
[[tool.hatch.envs.example.matrix]]
installer = ["uv", "pip"]
tool = ["uv", "pip"]

[tool.hatch.envs.example.overrides]
matrix.installer.uv = [
{ value = true, if = ["uv"] },
{ value = false, if = ["pip"] },
]
matrix.installer.scripts = [
matrix.tool.installer = { value = "{matrix:tool}" }
matrix.tool.scripts = [
{ key = "pip", value = "{env:HATCH_UV} pip {args}", if = ["uv"] },
]
```
Expand Down
4 changes: 2 additions & 2 deletions docs/how-to/run/python-scripts.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ Syncing dependencies

## Environment configuration

You may use the `[tool.hatch]` table directly to control the script's [environment](../../config/environment/overview.md). For example, if you wanted to disable UV (which is [enabled](../environment/select-installer.md) by default for scripts), you could add the following:
You may use the `[tool.hatch]` table directly to control the script's [environment](../../config/environment/overview.md). For example, if you wanted to disable UV (which is [enabled](../environment/select-installer.md#enabling-uv) by default for scripts), you could add the following:

```python tab="script.py"
# /// script
# ...
# [tool.hatch]
# uv = false
# installer = "pip"
# ///
```
2 changes: 1 addition & 1 deletion docs/plugins/environment/virtual.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type = "virtual"
| `python-sources` | `['external', 'internal']` | This may be set to an array of strings that are either the literal `internal` or `external`. External considers only Python executables that are already on `PATH`. Internal considers only [internally managed Python distributions](#internal-distributions). |
| `path` | | An explicit path to the virtual environment. The path may be absolute or relative to the project root. Any environments that [inherit](../../config/environment/overview.md#inheritance) this option will also use this path. The environment variable `HATCH_ENV_TYPE_VIRTUAL_PATH` may be used, which will take precedence. |
| `system-packages` | `false` | Whether or not to give the virtual environment access to the system `site-packages` directory |
| `uv` | `false` | Whether or not to use [UV](https://github.com/astral-sh/uv) in place of virtualenv & pip for virtual environment creation and dependency management, respectively. If you intend to provide UV yourself, you may set the `HATCH_ENV_TYPE_VIRTUAL_UV_PATH` environment variable which should be the absolute path to a UV binary. This environment variable implicitly enables the `uv` option (if unset). |
| `installer` | `pip` | When set to `uv`, [UV](https://github.com/astral-sh/uv) will be used in place of virtualenv & pip for virtual environment creation and dependency management, respectively. If you intend to provide UV yourself, you may set the `HATCH_ENV_TYPE_VIRTUAL_UV_PATH` environment variable which should be the absolute path to a UV binary. This environment variable implicitly sets the `installer` option to `uv` (if unset). |

## Location

Expand Down
8 changes: 4 additions & 4 deletions hatch.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
config-path = "ruff_defaults.toml"

[envs.default]
uv = true
installer = "uv"
post-install-commands = [
"uv pip install {verbosity:flag:-1} -e ./backend",
]
Expand Down Expand Up @@ -90,7 +90,7 @@ build-check = [

[envs.backend]
detached = true
uv = true
installer = "uv"
dependencies = [
"build~=0.7.0",
"httpx",
Expand All @@ -104,7 +104,7 @@ version = "cd backend && hatch version {args}"

[envs.upkeep]
detached = true
uv = true
installer = "uv"
dependencies = [
"httpx",
"ruff",
Expand All @@ -126,7 +126,7 @@ update-ruff = [

[envs.release]
detached = true
uv = true
installer = "uv"
dependencies = [
"hatchling",
]
Expand Down
6 changes: 3 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["hatchling>=1.24.1", "hatch-vcs>=0.3.0"]
requires = ["hatchling>=1.24.2", "hatch-vcs>=0.3.0"]
build-backend = "hatchling.build"

[project]
Expand Down Expand Up @@ -39,7 +39,7 @@ classifiers = [
]
dependencies = [
"click>=8.0.6",
"hatchling>=1.24.1",
"hatchling>=1.24.2",
"httpx>=0.22.0",
"hyperlink>=21.0.0",
"keyring>=23.5.0",
Expand All @@ -51,7 +51,7 @@ dependencies = [
"tomli-w>=1.0",
"tomlkit>=0.11.1",
"userpath~=1.7",
"uv>=0.1.33",
"uv>=0.1.35",
"virtualenv>=20.16.2",
"zstandard<1",
]
Expand Down
2 changes: 1 addition & 1 deletion src/hatch/cli/run/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def run(ctx: click.Context, args: tuple[str, ...]):

config = metadata.get('tool', {}).get('hatch', {})
config['skip-install'] = True
config.setdefault('uv', True)
config.setdefault('installer', 'uv')
config.setdefault('dependencies', [])
config['dependencies'].extend(metadata.get('dependencies', []))

Expand Down
2 changes: 1 addition & 1 deletion src/hatch/env/internal/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
def get_default_config() -> dict[str, Any]:
return {
'skip-install': True,
'uv': True,
'installer': 'uv',
'dependencies': ['build[virtualenv]>=1.0.3'],
'scripts': {
'build-all': 'python -m build',
Expand Down
2 changes: 1 addition & 1 deletion src/hatch/env/internal/static_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
def get_default_config() -> dict[str, Any]:
return {
'skip-install': True,
'uv': True,
'installer': 'uv',
'dependencies': [f'ruff=={RUFF_DEFAULT_VERSION}'],
'scripts': {
'format-check': 'ruff format{env:HATCH_FMT_ARGS:} --check --diff {args:.}',
Expand Down
2 changes: 1 addition & 1 deletion src/hatch/env/internal/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

def get_default_config() -> dict[str, Any]:
return {
'uv': True,
'installer': 'uv',
'dependencies': [
'coverage-enable-subprocess==1.0',
'coverage[toml]~=7.4',
Expand Down
2 changes: 1 addition & 1 deletion src/hatch/env/internal/uv.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
def get_default_config() -> dict[str, Any]:
return {
'skip-install': True,
'uv': True,
'installer': 'uv',
}
8 changes: 6 additions & 2 deletions src/hatch/env/virtual.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ def __init__(self, *args, **kwargs):

@cached_property
def use_uv(self) -> bool:
return self.config.get('uv', bool(self.explicit_uv_path))
return self.installer == 'uv' or bool(self.explicit_uv_path)

@cached_property
def installer(self) -> str:
return self.config.get('installer', 'pip')

@cached_property
def explicit_uv_path(self) -> str:
Expand Down Expand Up @@ -125,7 +129,7 @@ def uv_path(self) -> str:

@staticmethod
def get_option_types() -> dict:
return {'system-packages': bool, 'path': str, 'python-sources': list, 'uv': bool}
return {'system-packages': bool, 'path': str, 'python-sources': list, 'installer': str, 'uv-path': str}

def activate(self):
self.virtual_env.activate()
Expand Down
4 changes: 2 additions & 2 deletions tests/cli/env/test_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def test_uv_shipped(hatch, helpers, temp_dir, config_file):
helpers.update_project_environment(
project,
'default',
{'skip-install': True, 'uv': True, **project.config.envs['default']},
{'skip-install': True, 'installer': 'uv', **project.config.envs['default']},
)
helpers.update_project_environment(project, 'test', {})

Expand Down Expand Up @@ -205,7 +205,7 @@ def test_uv_env(hatch, helpers, temp_dir, config_file):
helpers.update_project_environment(
project,
'default',
{'skip-install': True, 'uv': True, **project.config.envs['default']},
{'skip-install': True, 'installer': 'uv', **project.config.envs['default']},
)
helpers.update_project_environment(project, 'hatch-uv', {'dependencies': ['uv>=0.1.31']})
helpers.update_project_environment(project, 'test', {})
Expand Down

0 comments on commit 59fef2a

Please sign in to comment.