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.1.0.tar.gz (56.3 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.1.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: optionz-0.1.0.tar.gz
  • Upload date:
  • Size: 56.3 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.1.0.tar.gz
Algorithm Hash digest
SHA256 2245312bf771b6390f0220794441d6494c7b511d4d47f735e3e7bdb49a1aba41
MD5 ac85b3d9c78980155971a5297afd59a8
BLAKE2b-256 9c662b53f8f544931e54c416718a9d139efe3a5ee5012aedb50aedbae21b155b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: optionz-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.6 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6b4fb9b73906cf43dee572863c058625a4a311454842396aa859b4597ae7a4ca
MD5 4d3b9ca87e5627c21fc417eebdfd3e01
BLAKE2b-256 fa3916b0ae5139d6307b27429901560b098f641ae3ac3e4523e2519a8ffd42f7

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