Add your description here
Project description
nada
nada is Spanish for "nothing". It contains a small pythonic implementation of rust's Option and Result types.
In constrast to other similar python libraries, nada's Option and Result types are always just Unions with no additional wrapper for Some, Ok, or Err (although those can be used as well). Simply add IsSome or IsOk as base methods and enjoy your guardrails! This makes it slightly more pythonic and easier for those unfamiliar with functional programming to grasp the concepts.
Installation
pip install nada-types
uv add nada-types
Requires Python 3.12 or later.
Usage
Option Types
Option can be used with the IsSome base class or with the Some wrapper
from nada import Some, Nada, Option, IsSome, is_some, is_none
from dataclasses import dataclass
@dataclass
class User(IsSome):
name: str
# The Option type is an alias for User | Nada
def find_user(user_id: int) -> Option[User]:
if user_id == 1:
return User("Alice")
return Nada
# Type narrowing with guard functions
user = find_user(1)
if is_some(user):
# Type checker knows this is User
# Access User fields directly, no unwrapping needed
print(user.name)
if is_none(user):
print("User not found") # Type checker knows this is NadaType
# Unwrapping values
name = Some("Alice").unwrap() # Returns User("Alice")
name = Nada.unwrap_or("Unknown") # Returns User("Unknown")
name = Some("Alice").expect("Missing") # Returns User("Alice")
# Pattern matching
match find_user(1):
case User(name=name):
print(f"Found: {name}")
case NadaType():
print("Not found")
You can also use the Some wrapper for builtin or external types
from dataclasses import dataclass
from nada import Option, Nada, Some, is_some
def get_user(user_id: int) -> Option[Some[str]]:
if user_id == 1:
return Some("Alice")
return Nada
user = get_user(1)
if is_some(user):
print(user)
Result Types
Use Result when an operation may fail. Create custom success and error types by inheriting from IsOk and IsErr:
from dataclasses import dataclass
from nada import Result, IsOk, IsErr, is_ok, is_err
@dataclass
class ParsedData(IsOk):
value: dict
class ParseError(IsErr, Exception):
def __init__(self, line: int, message: str):
super().__init__(f"Line {line}: {message}")
self.line = line
# The result type is an alias for ParsedData | ParseError
def parse(text: str) -> Result[ParsedData, ParseError]:
if not text:
return ParseError(line=0, message="Empty input")
return ParsedData(value={"text": text})
# Type narrowing with guard functions
result = parse("")
if is_ok(result):
print(result.value) # Type checker knows this is ParsedData
if is_err(result):
print(f"Error at line {result.line}") # Type checker knows this is ParseError
# Pattern matching
match parse("hello"):
case ParsedData(value=v):
print(f"Result: {v}")
case ParseError() as e:
print(f"Error: {e}")
You can also use the Ok and Err wrappers for builtin or external types:
from nada import Ok, Err, Result, is_ok, is_err
def divide(a: int, b: int) -> Result[Ok[float], Err[ZeroDivisionError]]:
if b == 0:
return Err(ZeroDivisionError("Cannot divide by zero"))
return Ok(a / b)
result = divide(10, 2)
if is_ok(result):
print(result.val) # Type checker knows this is Ok[float]
if is_err(result):
print(result.err) # Type checker knows this is Err[ZeroDivisionError]
# Unwrapping values
value = Ok(42).unwrap() # Returns 42
value = Err(ValueError()).unwrap_or(0) # Returns 0
Type Narrowing with Properties
In addition to guard functions, you can use literal-typed properties with is True checks:
from nada import Some, Nada
value = Some(42)
if value.is_some is True:
print(value.val) # Type is narrowed
if value.is_none is True:
print("No value")
API Reference
Option Types
| Type | Description |
|---|---|
IsSome |
Base class for custom "some" types |
Some[T] |
Wraps a value of type T |
Nada |
Singleton representing absence of a value |
NadaType |
The type of Nada |
Option[T] |
Type alias for T | NadaType |
Result Types
| Type | Description |
|---|---|
IsOk |
Base class for custom success types |
IsErr |
Base class for custom error types |
Ok[T] |
Wraps a successful value |
Err[E] |
Wraps an exception (E must inherit from Exception) |
Result[T, E] |
Type alias for T | E |
Type Guards
| Function | Narrows to |
|---|---|
is_some(val) |
Types inheriting from IsSome |
is_none(val) |
NadaType |
is_ok(val) |
Types inheriting from IsOk |
is_err(val) |
Types inheriting from IsErr |
Common Methods
All Some, Ok, and Err types provide:
unwrap()- Returns the inner value, raises if not presentexpect(msg)- Likeunwrap()but with a custom error messageunwrap_or(default)- Returns the inner value or the provided default
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nada_types-0.1.0.tar.gz.
File metadata
- Download URL: nada_types-0.1.0.tar.gz
- Upload date:
- Size: 3.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b2bedc880fe659a01bc3507738000320475ef1e4497d4e9274a365ff103ea5e
|
|
| MD5 |
1eeb22f87e3723bdab794c1127dc6ecc
|
|
| BLAKE2b-256 |
be9491ca5a2d0a6bee48338f2890babecd7897d95c16db4ab54f700b306909c9
|
Provenance
The following attestation bundles were made for nada_types-0.1.0.tar.gz:
Publisher:
on_main.yml on contagon/nada
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nada_types-0.1.0.tar.gz -
Subject digest:
7b2bedc880fe659a01bc3507738000320475ef1e4497d4e9274a365ff103ea5e - Sigstore transparency entry: 1247240280
- Sigstore integration time:
-
Permalink:
contagon/nada@b89fc4bc39b6d2e6b68e201b3fe4213b394c6a0b -
Branch / Tag:
refs/heads/main - Owner: https://github.com/contagon
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
on_main.yml@b89fc4bc39b6d2e6b68e201b3fe4213b394c6a0b -
Trigger Event:
push
-
Statement type:
File details
Details for the file nada_types-0.1.0-py3-none-any.whl.
File metadata
- Download URL: nada_types-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
23ade91acb90e76ad9867575003c331372bc37534540c6859d2a810bca9655b4
|
|
| MD5 |
ece0195ae2c32e6677352a19ca5abfc9
|
|
| BLAKE2b-256 |
0346832b7ee48fd0640dc7eab822b3295bb41be452f013b865be2f34abb7602f
|
Provenance
The following attestation bundles were made for nada_types-0.1.0-py3-none-any.whl:
Publisher:
on_main.yml on contagon/nada
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
nada_types-0.1.0-py3-none-any.whl -
Subject digest:
23ade91acb90e76ad9867575003c331372bc37534540c6859d2a810bca9655b4 - Sigstore transparency entry: 1247240317
- Sigstore integration time:
-
Permalink:
contagon/nada@b89fc4bc39b6d2e6b68e201b3fe4213b394c6a0b -
Branch / Tag:
refs/heads/main - Owner: https://github.com/contagon
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
on_main.yml@b89fc4bc39b6d2e6b68e201b3fe4213b394c6a0b -
Trigger Event:
push
-
Statement type: