Registry / serialization / returns

returns

JSON →
library0.29.0pypypi✓ verified 24d ago

The `returns` library (version 0.26.0) is a functional programming toolkit for Python, enhancing type-safety and explicit error handling. It allows developers to make functions return meaningful, typed, and safe values like `Result`, `Maybe`, and `IO` monads, promoting composable and testable code. The project is actively maintained with frequent releases, often every few months, introducing new features and compatibility updates.

pip install returns
INSTALL
IMPORT
SIG · RETURNS
R
returns
serializationpythonv0.29.0
Install
2.6s avg
Import
60ms
Disk
78MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.26.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.062s · 70.9MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 2.6s · import 0.057s · 70MB
78MB installed
● package 78MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Result, Success, Failure
from returns.result import Result, Success, Failure
Maybe, Some, Nothing
from returns.maybe import Maybe, Some, Nothing
IO, IOResult
from returns.io import IO, IOResult
do
from returns.do_notation import do
safe
from returns.result import safe
maybe
from returns.maybe import maybe

This quickstart demonstrates the `do` notation for composing operations with `Result` types. Functions `divide` and `multiply` return `Result` objects, encapsulating either a `Success` value or a `Failure` message. The `calculate_compound` function uses `yield` within the `@do(Result)` decorator to sequentially process these results, automatically propagating any `Failure`.

from returns.result import Result, Success, Failure from returns.do_notation import do def divide(numerator: int, denominator: int) -> Result[float, str]: if denominator == 0: return Failure('Cannot divide by zero') return Success(numerator / denominator) def multiply(value: float, multiplier: int) -> Result[float, str]: if multiplier < 0: return Failure('Multiplier cannot be negative') return Success(value * multiplier) @do(Result) def calculate_compound(num: int, den: int, mult: int) -> Result[float, str]: divided_val = yield divide(num, den) final_val = yield multiply(divided_val, mult) return final_val # Example usage: assert calculate_compound(10, 2, 5) == Success(25.0) assert calculate_compound(10, 0, 5) == Failure('Cannot divide by zero') assert calculate_compound(10, 2, -1) == Failure('Multiplier cannot be negative') print(calculate_compound(10, 2, 5))
Debug
Known issues
breaking`returns` has specific Python version requirements that have changed over time. Python 3.7 support was dropped in `0.22.0`, and Python 3.9 support was dropped in `0.24.0`. Current versions require Python `>=3.10`.
fix
Ensure your Python environment is at version `3.10` or higher to use recent `returns` releases.
affects: >=0.22.0
breakingThe `success_type` and `failure_type` fields were removed from `IOResult`, `Maybe`, and `Result` types. Code that directly accessed these attributes will break.
fix
Refactor code to not rely on these specific type attributes. Type hints should now be inferred correctly without needing to inspect these fields directly.
affects: >=0.23.0
gotchaThe `Maybe` type now implements a `__bool__` method where only `Nothing` evaluates to `False`. `Some` values, regardless of their content, evaluate to `True`.
fix
Be explicit when checking `Maybe` values. Use `is_some()`, `is_nothing()`, or `match` statements instead of direct boolean evaluation to avoid unintended behavior, especially when dealing with `Some(False)` or `Some(0)`.
affects: >=0.26.0
gotcha`returns` is highly reliant on specific `mypy` versions for correct type inference and to prevent `mypy` errors. The compatible `mypy` version often changes with `returns` releases.
fix
Always check the `returns` changelog or documentation for the currently supported `mypy` version range. Install `returns` with the `[compatible-mypy]` extra, e.g., `pip install returns[compatible-mypy]` to ensure `mypy` is installed correctly.
affects: All versions
gotchaAttempting to unwrap a `Failure` or `Nothing` value (e.g., using `.unwrap()`, `.value_or()`, or accessing `.value` directly on `Failure`) without handling the error path will raise an `UnwrapFailedError`.
fix
Always handle the error case before unwrapping. Use methods like `.alt()`, `.bind_failure()`, `.lash()`, `.fix()`, `.map_failure()`, or `match` statements to safely process both `Success`/`Some` and `Failure`/`Nothing` paths.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'returns'
The 'returns' library is not installed in the current Python environment or the environment is not correctly activated.
fix
Install the library using pip: `pip install returns`
TypeError: unsupported operand type(s) for +: 'Result[int, str]' and 'int'
You are attempting to perform a direct operation on a monadic object (like `Result`, `Maybe`, `IO`) without first safely extracting its successful value or handling its failure state.
fix
Use methods like `.map()`, `.bind()`, `.unwrap()`, `.alt()`, or pattern matching to correctly operate on or extract the value from within the monad.
```python
from returns.result import Result, Success

def add_five(value: Result[int, str]) -> Result[int, str]:
    return value.map(lambda x: x + 5)

# print(add_five(Success(10))) # Expected: Success(15)
```
AttributeError: module 'returns' has no attribute 'Result'
The `Result` type (or similar types like `Maybe`, `IO`) is not directly exposed under the top-level `returns` module; it must be imported from its specific submodule.
fix
Import the specific type directly from its corresponding submodule, e.g., `returns.result` for `Result`.
```python
from returns.result import Result, Success, Failure
# from returns.maybe import Maybe, Some, Nothing
# from returns.io import IO, IOFailure, IOSuccess
```
returns.primitives.exceptions.UnwrapFailedError: Cannot unwrap a `Failure` from the `Result` monad.
You attempted to use the `.unwrap()` method on a `Result` object that is in a `Failure` state, which is designed to raise an exception when the underlying value cannot be successfully unwrapped.
fix
Before calling `.unwrap()`, check if the `Result` is a `Success` using `.is_success`, or handle both `Success` and `Failure` cases using `.map()`, `.bind()`, `.alt()`, or pattern matching.
```python
from returns.result import Result, Success, Failure

def divide(a: int, b: int) -> Result[float, str]:
    return Success(a / b) if b != 0 else Failure("Division by zero")

result = divide(10, 0)

# Correct handling:
if result.is_success:
    print(f"Result: {result.unwrap()}")
else:
    print(f"Error: {result.failure()}")

# Or using .alt() for a default value:
# print(divide(10, 0).alt(lambda _: -1.0).unwrap()) # Prints -1.0
```
Upgrade
Version history
0.29.0latest on PyPI · released Aug 2, 2026
Audit
Dependencies
pythonrequiredCore language runtime requirement.
mypyoptionalCrucial for type-checking and leveraging `returns`' full benefits; specific versions are often required.
Agent activity
11 hits · last 30 days
node
10
Resources
returns — pip install returns · libregistry