Skip to main content

Hatchling plugin to create optional-dependencies pinned to minimum versions

Project description

hatch-min-requirements

License PyPI Python Version CI codecov

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.

Usage

In your pyproject.toml make the following changes:

  • Append hatch-min-requirements to [build-system.requires].
  • Add a [tool.hatch.metadata.hooks.min_requirements] table.
# pyproject.toml
[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]

Considerations

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

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

# pyproject.toml
[build-system]
requires = ["hatchling", "hatch-min-requirements", "pip"]

To explicitly opt out of using pip, and use standard library tools only, you can set the MIN_REQS_TRY_PIP environment variable to 0 or False.

TODO

  • make the min-reqs extra configurable
  • add offline and no-pip options to the min_requirements table in pyproject

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hatch_min_requirements-0.0.1rc1.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file hatch_min_requirements-0.0.1rc1.tar.gz.

File metadata

File hashes

Hashes for hatch_min_requirements-0.0.1rc1.tar.gz
Algorithm Hash digest
SHA256 a40d76f1183aa4a61d993a2fd7633ff50751b5d1cd1d6e90593a0e055e5b7128
MD5 44c75fabee789a4511be07d34177257d
BLAKE2b-256 43e4e10e7548fb09e3505ed93b5c0785fb13c00d41af04387a7ca227f58219c5

See more details on using hashes here.

File details

Details for the file hatch_min_requirements-0.0.1rc1-py3-none-any.whl.

File metadata

File hashes

Hashes for hatch_min_requirements-0.0.1rc1-py3-none-any.whl
Algorithm Hash digest
SHA256 d5798842f39826e40a45e9c869b79d80a51a3bd23084a5bb0c4debbe4391e3a0
MD5 3d8375bd17898a200f6d43a0cf245f1e
BLAKE2b-256 fec12f666583f75a222fa78a9bac286a3ed68c8b7d4355f32325b1b747b9a776

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page