Metadata-Version: 2.1
Name: unopt
Version: 0.1.0
Summary: Utility functions to unwrap Optional values.
Home-page: https://github.com/odashi/unopt-py
Author: Yusuke Oda
Author-email: yus.takara@gmail.com
License: MIT License
Keywords: none,optional,rust
Platform: UNKNOWN
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Requires-Python: >=3.7
Description-Content-Type: text/markdown
License-File: LICENSE

# 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.

## Examples

```python
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
```

