Skip to main content

unopt: Utility functions to unwrap Optional[T]

Overview

unopt provides several utility functions to "unwrap" the Optional[T] (or T | None) objects: removes the Optional type hint and obtains the underlying object.

unopt functions are inspired by the Rust's Option<T> functionality, but the behavior is tuned to Python's convention. E.g., unwrap() raises an exception instead of aborting.

Install

pip install unopt

Examples

from unopt import *

foo: Optional[int] = 123
bar: Optional[int] = None

# unwrap() returns the given object if it is not None.
assert unwrap(foo) == 123
unwrap(bar)  # Raises UnwrapError

# unwrap_or() returns the default value if the given object is None.
assert unwrap_or(foo, 456) == 123
assert unwrap_or(bar, 456) == 456

# unwrap_or_else() returns the default value obtained by invoking the given function.
assert unwrap_or_else(foo, lambda: 456) == 123
assert unwrap_or_else(bar, lambda: 456) == 456

# unwrap_unchecked() just casts the given object without value checking.
assert unwrap_unchecked(foo) == 123
assert unwrap_unchecked(bar) is None  # Unsafe

Metadata

Release files for unopt 0.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for unopt 0.3.0
File Size Uploaded Provenance Actions
unopt-0.3.0.tar.gz 3.8 kB Without provenance Details for unopt-0.3.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for unopt 0.3.0
File Interpreter ABI Platform Actions
unopt-0.3.0-py3-none-any.whl Python 3 none any Details for unopt-0.3.0-py3-none-any.whl

Total release size: 7.6 kB

Release files / unopt-0.3.0.tar.gz

Download URL unopt-0.3.0.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
7d9de68d44b6390c67727def1ece0f8ebcbfeb8bcf622db11e05e7a0ac8384e4
BLAKE2b-256 checksum
How to use checksums
cf9982b557594538ac52d8240dec962a8555aeb7da5ba1f664c28a1da58f6b94
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Release files / unopt-0.3.0-py3-none-any.whl

Download URL unopt-0.3.0-py3-none-any.whl
Size 3.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b1fa0fd51c4fdf504add9685b25bd617e0b054f3cc12763edce488a70c4cc7ff
BLAKE2b-256 checksum
How to use checksums
a46cc4ff11374270a91c8e20d162a25ee483b9ff37c34a5ac8b50392bd0235f6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.14

Security for unopt 0.3.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page