Skip to main content

Some utility functions for Optional values.

Project description

Optionz

A nullable type is needed when there can be an absence of a value. Python uses None to represent the empty value and Optional[T], i.e., T | None to represent a nullable T. The other option is to use exceptions, but they are not expressed as values in the type system, which can be brittle and error-prone.

Python approach is a compromise between the "million dollar mistake", in which null is a valid member of every type and the more structured Maybe[T], that models nullables as a tagged union of Just[T] and Nothing.

This library provides some utility functions for working with Optional values in a more functional style, inspired by the Maybe monad in Haskell and Rust's equivalent Option type.

The main philosophy is that we do not want to introduce a new type like returns and other similar libraries. Instead, we want to provide a similar functionality using plain Optional values so that your code can adopt it incrementally without feeling like an alien in the Python ecosystem.

As much as I like functional programming and the Maybe monad, I think Python's approach is fine and offers most of the same static guarantees. The crucial difference is that Maybe is a tagged union of two types and Python's Optional is a union of sets. They behave mostly the same, but the latter do not allow nesting: Optional[Optional[T]] flattens to Optional[T], while Maybe[Maybe[T]] is a whole new type. This is a difference that rarely matters in practice, and I don't any anyone is clearly superior to the other.

Installation

Install Optionz using pip/uv/poetry whatever you like. For example:

pip install optionz

Optionz consists of a single file, so you can also just copy opt.py to your project and import it from there. It do not define any new type so there is no conflict with code that import vs ones that vendorize it.

Usage

Import opt and use the functions as needed.

import opt

opt.unwrap(42) # 42
opt.unwrap(None) # raises ValueError

Documentation

The documentation is available at https://optionz.rtfd.io/ and includes more examples and explanations of the functions provided by the library.

License

Optionz is licensed under the MIT License. See LICENSE for more details.

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

optionz-0.2.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

optionz-0.2.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file optionz-0.2.0.tar.gz.

File metadata

  • Download URL: optionz-0.2.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for optionz-0.2.0.tar.gz
Algorithm Hash digest
SHA256 8aec0eb249a52f552b8c5411145a0b995f54a9ef09d092c5b26b68d56cec612e
MD5 d4ca7de15803490e104eae64d33e4bc4
BLAKE2b-256 a54e9fcd0206180c8f2f55832c1131927f2ca0c39f5edfefb5d305fb0950bb6f

See more details on using hashes here.

File details

Details for the file optionz-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: optionz-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.21 {"installer":{"name":"uv","version":"0.11.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for optionz-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 35a2c5b9786bfae41151c407d6ec4f9e248128564b07436cb6301983f48612c0
MD5 3b04b85d8bddac46c9a6efcf197fc5b3
BLAKE2b-256 9edf62cf70a7df4a6d34bf592a2f9c2590360fbc90ee779dd02079e34dee3ecd

See more details on using hashes here.

Supported by

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