Skip to main content

Result and Option types for Python

Project description

patina

Documentation Status

This is an implementation of Rust's Result and Option types in Python. Most methods have been implemented, and the (very good) original documentation has been adapted into docstrings.

The documentation for this package can be read here. All doctests are run and type-checked as part of the CI pipeline as unit tests. The tests are direct ports of those in the Rust documentation.

Why?

2 reasons:

  • Python (in 3.10) now has pattern matching, wouldn't it be cool if we could make the most of that?
  • Sometimes it's nice to have types for your errors.
  • Being able to map over possible failure can be very powerful.

Check this out:

from patina import Some, None_

# This value is an Option[str]
maybe_value = api_call_that_might_produce_a_value()

match maybe_value.map(str.upper):  # Make it uppercase if it exists
    case Some(val):
        print("We got a val:", val)
    case None_():  # Don't forget the parentheses (otherwise it's binding a name)
        print("There was no val :(")

A similar thing can be done with the Result type (matching on Ok or Err). This can be handy if you want to be more explicit about the fact that a function might fail. If the function returns a Result, we can explicitly type the possible error values.

If this all sounds good, I recommend looking into functional programming, particularly of the ML variety (e.g. Haskell, OCaml, SML) or Rust.

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

patina-0.2.1.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

patina-0.2.1-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

File details

Details for the file patina-0.2.1.tar.gz.

File metadata

  • Download URL: patina-0.2.1.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.0 Linux/5.10.19_1

File hashes

Hashes for patina-0.2.1.tar.gz
Algorithm Hash digest
SHA256 6671b4a33f3383e85d351fe585829b12593b617c56f98f073102c845936d16b2
MD5 35d06ae1578428f602cbb3bfeb1149d5
BLAKE2b-256 a15b51606ebb17d451d47e4d2281d2895c0aec897c12432d23f77cb030a410fe

See more details on using hashes here.

File details

Details for the file patina-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: patina-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 12.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.0 Linux/5.10.19_1

File hashes

Hashes for patina-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ed1abded50fd495d4d4b885cd8667ba33ff2f8a16d152df3ecb5fe641c115542
MD5 eec06f3573362ff53f9337c7830eb366
BLAKE2b-256 c35cf8c870bb97134b8d570e8a44cf199e0cd8737d2eaa5a5793fa1c0102a205

See more details on using hashes here.

Supported by

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