Hatchling plugin to create optional-dependencies pinned to minimum versions
Project description
hatch-min-requirements
Hatchling plugin to create optional-dependencies pinned to minimum versions
Rationale
When creating a library, it is often useful to specify the minimum version of a dependency that is required. However, pip's default behavior is to install the latest version of a package that satisfies your requirement. As a result, if aren't carefully testing your minimum dependencies, you may inadvertently introduce changes to your package that are not compatible with the minimum version you specified.
This plugin will inspect your packages dependencies and dynamically add an extra
(named min-reqs by default) to the project.optional-dependencies table of
your pyproject.toml file. This extra will contain all of your dependendencies
pinned to their minimum version.
This makes it easy to test your package against your minimum stated dependencies on CI, or to install your package with the minimum dependencies for local development.
See also:
You can achieve a similar aim with hatch-pip-compile,
using uv as the pip-compile resolver with the --resolution
flag set to lowest or lowest-direct:
[tool.hatch.env]
requires = ["hatch-pip-compile"]
[tool.hatch.envs.default]
type = "pip-compile"
pip-compile-resolver = "uv"
pip-compile-args = ["--resolution=lowest-direct"]
Usage
In your pyproject.toml make the following changes:
- Append
hatch-min-requirementsto[build-system.requires]. - Add a
[tool.hatch.metadata.hooks.min_requirements]table.
[build-system]
requires = ["hatchling", "hatch-min-requirements"]
build-backend = "hatchling.build"
[tool.hatch.metadata.hooks.min_requirements]
Then, you can install your package using the min-reqs extra and it will
dynamically use the minimum compatible versions of your dependencies.
pip install -e .[min-reqs]
Environment variables
Environment variables can be used to configure the behavior. Described in detail below:
| Variable | Default | Description |
|---|---|---|
MIN_REQS_EXTRA_NAME |
min-reqs |
The name of the extra to add to pyproject.toml |
MIN_REQS_PIN_UNCONSTRAINED |
True |
Pin unconstrained dependencies to minimum available version on PyPI. (e.g. numpy -> numpy==1.3.0) |
MIN_REQS_OFFLINE |
False |
Do not connect to PyPI to fetch available versions |
MIN_REQS_TRY_PIP |
True |
Use pip to fetch available versions in online mode. Set to 0 to use stdlib tools only |
Utilities
This package provides two convenience functions that can be used directly (without being a hatch plugin).
-
hatch_min_requirements.sub_min_compatible_versionTakes a pip requirement string and returns a new requirement string with the minimum compatible version substituted in.
>>> sub_min_compatible_version("numpy") 'numpy==1.3.0' >>> sub_min_compatible_version("numpy>=1.4.1") 'numpy==1.4.1' >>> sub_min_compatible_version("numpy>1.3") 'numpy==1.4.1' >>> sub_min_compatible_version("numpy[extra1,extra2]>=1.20,<2.0") 'numpy[extra1,extra2]==1.20.0' >>> sub_min_compatible_version("numpy[extra]<2; python_version == '3.6'") "numpy[extra]==1.3.0 ; python_version == '3.6'"
-
hatch_min_requirements.patch_pyprojectTakes a path to a
pyproject.tomlfile and patches it to include themin-reqsextra. The original file is backed up with a.BAKextension.>>> patch_pyproject("path/to/pyproject.toml")
Considerations
Dependencies with no constraints
In cases of dependencies declared without constraints (e.g. foo), the plugin
will search for the minimum available version of the package from PyPI. The
goal here is to encourage accurate requirement pinning. If you want to disable
this behavior and leave unconstrained specifiers as is, you can either set the
MIN_REQS_PIN_UNCONSTRAINED environment variable to 0 or False, or use
offline mode with MIN_REQS_OFFLINE=1 (see below).
Offline Mode
In cases such as upper-bounds (<X.Y), non-inclusive lower bounds (>X.Y), and
exclusions (!=X.Y), it's not possible to declare a minimum version without
fetching available versions from PyPI. By default, this plugin will attempt
to connect to PyPI in order to determine compatible minimum version strings. If
you want to disable this behavior, you can set the MIN_REQS_OFFLINE
environment variable to 1 or True.
MIN_REQS_OFFLINE=1 pip install -e .[min-reqs]
In offline mode, no attempt is made to guess the next compatible version of a package after a non-inclusive lower bound. Instead, the plugin will simply use your dependency as stated (meaning you won't be testing lower bounds). If you want to test lower bounds without connecting to PyPI, you should pin your dependencies with inclusive lower bounds:
[project]
dependencies = [
"foo>=1.2.3" # will be pinned to "foo==1.2.3"
"baz~=1.2" # will be pinned to "baz==1.2"
"bar>1.2.3" # will be unchanged
]
Usage of pip vs standard-lib tools
Fetching the available versions of a package is not trivial, and pip is the
de facto tool for doing so. If pip is available in the build environment,
this plugin will use it to fetch the available versions of a package. But, you
must opt in to this behavior by adding pip to your build-system.requires
in pyproject.toml:
[build-system]
requires = ["hatchling", "hatch-min-requirements", "pip"]
To explicitly opt out of using pip (even if it's available) and use standard library tools only, you can
set the MIN_REQS_TRY_PIP environment variable to 0 or False.
TODO
- add
offlineandno-pipoptions to themin_requirementstable in pyproject
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file hatch_min_requirements-0.2.0.tar.gz.
File metadata
- Download URL: hatch_min_requirements-0.2.0.tar.gz
- Upload date:
- Size: 15.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
047cf2b78281b4ab33d5475c2898351164b2884d0e82a7f211ce0901658c8b13
|
|
| MD5 |
ca8903fd7ec5c7025867807794aa3c40
|
|
| BLAKE2b-256 |
069266ab11ea4f72f321a1bbc73a4392f863cc2f01d6bdfcc55227074653d9ee
|
Provenance
The following attestation bundles were made for hatch_min_requirements-0.2.0.tar.gz:
Publisher:
ci.yml on tlambert03/hatch-min-requirements
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
hatch_min_requirements-0.2.0.tar.gz -
Subject digest:
047cf2b78281b4ab33d5475c2898351164b2884d0e82a7f211ce0901658c8b13 - Sigstore transparency entry: 439194209
- Sigstore integration time:
-
Permalink:
tlambert03/hatch-min-requirements@a19c674b1c08139cddf94feca297274b4b2cb6d7 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/tlambert03
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@a19c674b1c08139cddf94feca297274b4b2cb6d7 -
Trigger Event:
push
-
Statement type:
File details
Details for the file hatch_min_requirements-0.2.0-py3-none-any.whl.
File metadata
- Download URL: hatch_min_requirements-0.2.0-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
00183f35759caef4f9533c2337f7ad99f43b29828b441937e2092ca16910175a
|
|
| MD5 |
1e13a00d47f461005a951fd6bcc5a505
|
|
| BLAKE2b-256 |
e2cffcc2853d0db5d0ba4ed900f8128d48c92b2bc9c232152b9ad88f3c744e1d
|
Provenance
The following attestation bundles were made for hatch_min_requirements-0.2.0-py3-none-any.whl:
Publisher:
ci.yml on tlambert03/hatch-min-requirements
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
hatch_min_requirements-0.2.0-py3-none-any.whl -
Subject digest:
00183f35759caef4f9533c2337f7ad99f43b29828b441937e2092ca16910175a - Sigstore transparency entry: 439194220
- Sigstore integration time:
-
Permalink:
tlambert03/hatch-min-requirements@a19c674b1c08139cddf94feca297274b4b2cb6d7 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/tlambert03
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@a19c674b1c08139cddf94feca297274b4b2cb6d7 -
Trigger Event:
push
-
Statement type: