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 returnsVerified import paths — ran on the pinned version, not inferred.
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`.
Ensure your Python environment is at version `3.10` or higher to use recent `returns` releases.
Refactor code to not rely on these specific type attributes. Type hints should now be inferred correctly without needing to inspect these fields directly.
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)`.
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.
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.
Install the library using pip: `pip install returns`
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)
```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 ```
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
```