Construct Checks for Optional Dependencies
Project description
optional_dependencies
Construct Checks for Optional Dependencies
Installation
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.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 ofimportlib.util.find_spec. -
optional_dependencies.utils.get_version(pkg_name: str, /) -> Version | Literal[InstalledState.NOT_INSTALLED]: for getting thepackaging.Versionof a package if it is installed, or returningNOT_INSTALLEDotherwise. -
optional_dependencies.utils.chain_checks(version: Version, /, *checks: bool): for chaining checks together and ensuring the returned value is still apackaging.VersionorNOT_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("package1"), is_installed("package1.subpackage"))
# <Version('...')>
# This package is not installed correctly
chain_checks(get_version("package2"), is_installed("package2.subpackage"))
# <InstalledState.NOT_INSTALLED: False>
The low-level API can be used with OptionalDependencyEnum
class OptDeps(OptionalDependencyEnum):
PACKAGING = auto()
THIS_IS_NOT_INSTALLED = chain_checks(
get_version("package2"), is_installed("package2.subpackage")
)
Citation
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
We welcome contributions!
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 optional_dependencies-0.4.0.tar.gz.
File metadata
- Download URL: optional_dependencies-0.4.0.tar.gz
- Upload date:
- Size: 68.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8c2a764021ec47466a9c0375608d6976aa7f0d87f9a611e511ae70861edbd921
|
|
| MD5 |
cd1604b90067ed06022966968468d660
|
|
| BLAKE2b-256 |
fd9a01c2aaee8ec00abe3dd51368c8a8c341b4352c1b23859d862edd62bd7ae5
|
Provenance
The following attestation bundles were made for optional_dependencies-0.4.0.tar.gz:
Publisher:
cd.yml on GalacticDynamics/optional_dependencies
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
optional_dependencies-0.4.0.tar.gz -
Subject digest:
8c2a764021ec47466a9c0375608d6976aa7f0d87f9a611e511ae70861edbd921 - Sigstore transparency entry: 462210030
- Sigstore integration time:
-
Permalink:
GalacticDynamics/optional_dependencies@13b5ea242bbf2f03fcd332ccd043349eadf77b0c -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/GalacticDynamics
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
cd.yml@13b5ea242bbf2f03fcd332ccd043349eadf77b0c -
Trigger Event:
release
-
Statement type:
File details
Details for the file optional_dependencies-0.4.0-py3-none-any.whl.
File metadata
- Download URL: optional_dependencies-0.4.0-py3-none-any.whl
- Upload date:
- Size: 8.6 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 |
aa5e4c2939681cf6b5f38de9ed7cdd60647526076452c29fbe170335fe34bf4a
|
|
| MD5 |
3e6d9e61bd2dc0147eb061ff5770cfeb
|
|
| BLAKE2b-256 |
701cc03aa5a23a3320f8b2875e1ddf6db679412533d95da2ebee15bfe611c1e3
|
Provenance
The following attestation bundles were made for optional_dependencies-0.4.0-py3-none-any.whl:
Publisher:
cd.yml on GalacticDynamics/optional_dependencies
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
optional_dependencies-0.4.0-py3-none-any.whl -
Subject digest:
aa5e4c2939681cf6b5f38de9ed7cdd60647526076452c29fbe170335fe34bf4a - Sigstore transparency entry: 462210049
- Sigstore integration time:
-
Permalink:
GalacticDynamics/optional_dependencies@13b5ea242bbf2f03fcd332ccd043349eadf77b0c -
Branch / Tag:
refs/tags/v0.4.0 - Owner: https://github.com/GalacticDynamics
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
cd.yml@13b5ea242bbf2f03fcd332ccd043349eadf77b0c -
Trigger Event:
release
-
Statement type: