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.1.tar.gz (5.5 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.1-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: optionz-0.1.1.tar.gz
  • Upload date:
  • Size: 5.5 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.1.tar.gz
Algorithm Hash digest
SHA256 e7751378d4a476b297c999c2ff724975f453ab5d87e733794edc46770e11619d
MD5 45bd198e108d5580eb37da7ad02bbf11
BLAKE2b-256 92bbacdee4276489bfbc912f93a085c524528d9db2d74c6931ab6f2f226fb997

See more details on using hashes here.

File details

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

File metadata

  • Download URL: optionz-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 5.9 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 89552be99363e1df121ab315263f384176f2a2c8b68fba729be77f258de9590a
MD5 43700ee009ed8168e9169b02560f6a6b
BLAKE2b-256 81ba09dda01be63e7751124b01e41d435bdb4dcd6ab5bfa7b437a3d3ea62e22a

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