Skip to main content

Option and Result types for Python.

Project description

isos

isos is a lightweight Python library that brings the Result Pattern to your code. It introduces two core types — Option and Result — which behave as sum types (also known as tagged unions or discriminated unions) that make handling missing values and errors explicit, safe, and expressive.

Sum types allow you to represent values that can be one of several variants, where each variant can hold different types of data. This is particularly useful for modeling scenarios where a value can be in different states, like presence/absence (Option) or success/failure (Result).

Why use the Result pattern?

In Python, a function can return Optional[T] — either a value of type T or None. But unless you enforce strict type checking, there’s no clear indication that the value may be absent. This often leads to AttributeError or TypeError when None sneaks through.

Similarly, Python uses exceptions to signal errors, but you don’t always know when a function might raise one. You either have to read the source or wrap everything in a try/except.

By returning a Result or Option instead, you:

  • Make absence and errors visible in the type system.
  • Force explicit handling of failure cases.
  • Pass, transform, and compose results safely.
  • Write code that is more predictable, robust, and self-documenting.

Examples

Option

from isos import Some, Null, Option

def find_user(user_id: int) -> Option[str]:
    users = {1: "Alice", 2: "Bob"}
    return Some(users[user_id]) if user_id in users else Null()

# Handling the result:
user = find_user(1)
if user.is_some():
    print(f"Found user: {user.unwrap()}")
else:
    print("User not found")

# You can also provide a default:
name = find_user(42).unwrap_or("Guest")
print(name)  # -> "Guest"

Result

from isos import Ok, Err, Error, Result
from typing import final

# Define a custom error
@final
class DivisionByZero(Error):
    MESSAGE = "Cannot divide by zero"

def safe_divide(a: float, b: float) -> Result[float]:
def safe_divide(a: float, b: float) -> Result[float]:
    if b == 0:
        return Err(DivisionByZero())
    return Ok(a / b)

result = safe_divide(10, 2)

if result.is_ok():
    print(f"Result is {result.unwrap()}")
else:
    print(f"Error: {result.unwrap_error()}")

# Transforming results
result = safe_divide(10, 0).map(lambda x: x * 2)
# Still Err(DivisionByZero)

# Chaining
result = safe_divide(20, 2).and_then(lambda x: safe_divide(x, 2))
print(result)  # -> Ok(5.0)

Pattern Matching

Python 3.10 introduced pattern matching, which works great with Option and Result:

from isos import Some, Null, Ok, Err

# Pattern matching with Option
def describe_user(user_opt: Option[str]) -> str:
    match user_opt:
        case Some(name):
            return f"User found: {name}"
        case Null():
            return "No user found"
        case _:
            raise TypeError("user_opt is not an Option")

print(describe_user(Some("Alice")))  # -> "User found: Alice"
print(describe_user(Null()))         # -> "No user found"

# Pattern matching with Result
def handle_division(result: Result[float]) -> str:
    match result:
        case Ok(value):
            return f"Success: {value}"
        case Err(error):
            return f"Failed: {error.MESSAGE}"
        case _:
            raise TypeError("result is not a Result")

print(handle_division(safe_divide(10, 2)))  # -> "Success: 5.0"
print(handle_division(safe_divide(1, 0)))   # -> "Failed: Cannot divide by zero"

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

isos-0.3.0.tar.gz (9.7 kB view details)

Uploaded Source

Built Distribution

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

isos-0.3.0-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file isos-0.3.0.tar.gz.

File metadata

  • Download URL: isos-0.3.0.tar.gz
  • Upload date:
  • Size: 9.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for isos-0.3.0.tar.gz
Algorithm Hash digest
SHA256 693628d4dfad1d479fa82f56a374bdcff86473097e0e739484f635c57bf61ff7
MD5 2348c7b517c29b9c0dfbd1bd5d4d71d9
BLAKE2b-256 d3c4eff8c765f5c4a6aeee2176a402737f0bf07a78ba3077bf79a1feaf57b2d5

See more details on using hashes here.

Provenance

The following attestation bundles were made for isos-0.3.0.tar.gz:

Publisher: release.yaml on gsmyridis/isos

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file isos-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: isos-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for isos-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 80008803cebd637a646f65e532f43c31cccfce88ab424d4a0a6f29638bb7ba88
MD5 b9a1a049944a2990a2fb33368900e611
BLAKE2b-256 10ecaf0c0816986da8860227f2592b091d43650de5713977798c593cbaa461bd

See more details on using hashes here.

Provenance

The following attestation bundles were made for isos-0.3.0-py3-none-any.whl:

Publisher: release.yaml on gsmyridis/isos

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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