Skip to main content

Construct Checks for Optional Dependencies

Project description

optional_dependencies

Construct Checks for Optional Dependencies

Installation

PyPI platforms PyPI version

pip install optional_dependencies

Documentation

High-Level API

This package allows for easy construction of checks for optional dependencies. As every Python project can have its own unique set of optional dependencies, optional_dependencies provides the enum.Enum base class OptionalDependencyEnum for enumerating the optional dependencies.

A constructed OptionalDependencyEnum enum.Enum has members that are the package names with values that are either a packaging.Version or a NOT_INSTALLED sentinel value.

As an example:

# Note that `auto` is a convenience re-export of enum.auto
from optional_dependencies import OptionalDependencyEnum, auto


class OptDeps(OptionalDependencyEnum):
    PACKAGING = auto()
    THIS_IS_NOT_INSTALLED = auto()


OptDeps.PACKAGING
# <OptDeps.PACKAGING: <Version('...')>>

OptDeps.PACKAGING.is_installed
# True

OptDeps.PACKAGING.version
# <Version('...')>

OptDeps.THIS_IS_NOT_INSTALLED
# <OptDeps.THIS_IS_NOT_A_PACKAGE: <InstalledState.NOT_INSTALLED: False>>

enum.auto on a OptionalDependencyEnum passes version parsing to packaging.utils.canonicalize_name then importlib.metadata.version then packaging.version.parse. If the package cannot be found then it is considered InstalledState.NOT_INSTALLED

InstalledState.NOT_INSTALLED is an enum.Enum member that has a truthy value of False. This can be useful for boolean checks, as packaging.Version always has a truthy value of True.

if not OptDeps.THIS_IS_NOT_INSTALLED:
    print("NOT_INSTALLED has a truthy value of False")
# NOT_INSTALLED has a truthy value of False

if OptDeps.PACKAGING:  # truthy value of `True`
    print(OptDeps.PACKAGING)
# OptDeps.PACKAGING

Low-Level API

Sometimes the high-level API is insufficient to determine whether an optional dependency is present. For example, this can sometimes happen with compiled packages, where the package appears to be installed, but something is wrong. In these cases you can customize the enum members using the low-level API.

The low-level functions are:

  • optional_dependencies.utils.is_installed(pkg_name: str, /) -> bool: a regularized form of importlib.util.find_spec.

  • optional_dependencies.utils.get_version(pkg_name: str) -> Version | Literal[InstalledState.NOT_INSTALLED]: for getting the packaging.Version of a package if it is installed, or returning NOT_INSTALLED otherwise.

  • optional_dependencies.utils.chain_checks: for chaining checks together and ensuring the returned value is still a packaging.Version or NOT_INSTALLED.

As a pseudo-code example of a package with c-compiled code that :

from optional_dependencies.utils import is_installed, get_version, chain_checks

# A subpackage needs to be checked.
chain_checks(get_version("package"), is_installed("package.subpackage"))
# <Version('...')>

chain_checks(get_version("package"), is_installed("package.subpackage"))
# <InstalledState.NOT_INSTALLED: False>

Citation

DOI

If you found this library to be useful and want to support the development and maintenance of lower-level code libraries for the scientific community, please consider citing this work.

Development

codecov Actions Status

We welcome contributions!

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

optional_dependencies-0.3.0.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

optional_dependencies-0.3.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file optional_dependencies-0.3.0.tar.gz.

File metadata

  • Download URL: optional_dependencies-0.3.0.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for optional_dependencies-0.3.0.tar.gz
Algorithm Hash digest
SHA256 2d82517703b717b0de9f1de470a9597d4aca3b16bd4bd41adfbd81ab1b50ece7
MD5 cb8a8d845cbb6ae8979cbf5eff2152e0
BLAKE2b-256 7ec973f9886c42ecd2f8f4cde83af3ee408e8bc704911cd6cb202b613501ab64

See more details on using hashes here.

File details

Details for the file optional_dependencies-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for optional_dependencies-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6cc21b1dec9e20699cb5a4b3fa50670a36c50b2e97721a8846661f0787cf4318
MD5 055380e4cb376e05f52f684558df9341
BLAKE2b-256 795b5acef1e630b0365292d24135a49c2452d21fcfdd8dca017eed8f14f0e892

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