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.1.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.1-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: isos-0.3.1.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.1.tar.gz
Algorithm Hash digest
SHA256 05cf95fa537b0dffdfa40b7a8df34d0b76617cb3d51002cf7657bb6da224c097
MD5 bf028c2be26b8fd364ab792ef975c0be
BLAKE2b-256 4dfaff78975c7e720d3bf6543c223bd345434b4482e6484fa0289e2a6beb6eb9

See more details on using hashes here.

Provenance

The following attestation bundles were made for isos-0.3.1.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.1-py3-none-any.whl.

File metadata

  • Download URL: isos-0.3.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 126bfbe5556d819ed950edb3600ab3b85cc50e78b76904a69f2d44b601e80ce4
MD5 fff898a225ad638f78a86afd2cecae13
BLAKE2b-256 b207a00fc3d09b70cb45e1603540c703de911fc4abb4eb3f02a6161901413e5f

See more details on using hashes here.

Provenance

The following attestation bundles were made for isos-0.3.1-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