Install & Compatibility
Where this runs
tested against v0.17.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
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.034s · 17.8MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.6s · import 0.032s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Ok
✓ from result import Ok
Err
✓ from result import Err
Result
✓ from result import Result
is_ok
✓ from result import is_ok
✗ some_result.ok_value
Accessing .ok_value or .err_value directly without first verifying the type (e.g., with `is_ok`, `is_err`, `isinstance`, or pattern matching) will raise an AttributeError if the type does not match.
is_err
✓ from result import is_err
✗ some_result.err_value
Accessing .ok_value or .err_value directly without first verifying the type (e.g., with `is_ok`, `is_err`, `isinstance`, or pattern matching) will raise an AttributeError if the type does not match.
This quickstart demonstrates defining a function that returns a `Result` type, indicating either a successful operation with `Ok(value)` or a failure with `Err(error)`. It shows how to safely unwrap the result using `isinstance` checks or the provided `is_ok`/`is_err` type guards. For Python 3.10 and newer, structural pattern matching (`match` statement) offers a more elegant way to handle `Result` types.
from result import Ok, Err, Result, is_ok
def divide(a: int, b: int) -> Result[float, str]:
if b == 0:
return Err("Cannot divide by zero")
return Ok(a / b)
# Example usage with isinstance
div_result = divide(10, 2)
if isinstance(div_result, Ok):
print(f"Division successful: {div_result.ok_value}")
else:
print(f"Division failed: {div_result.err_value}")
div_result_fail = divide(10, 0)
if is_ok(div_result_fail):
print(f"Division successful: {div_result_fail.ok_value}")
else:
print(f"Division failed: {div_result_fail.err_value}")
# Example usage with pattern matching (Python 3.10+)
# This is commented out to ensure compatibility with Python < 3.10 for execution,
# but is a recommended pattern for newer Python versions.
# def process_division(a: int, b: int):
# match divide(a, b):
# case Ok(value):
# print(f"{a} / {b} == {value}")
# case Err(error_message):
# print(f"Error: {error_message}")
# process_division(10, 5)
# process_division(10, 0)
Debug
Known issues
breakingThe `rustedpy/result` library is explicitly marked as 'NOT MAINTAINED' on its GitHub repository. This means there will be no further development, bug fixes, or security updates. Projects relying on this library may face compatibility issues with newer Python versions or unaddressed vulnerabilities.fixEvaluate migration to a maintained alternative like `dry-python/returns` (specifically its `Result` container) or `overflowy/safe-result` for ongoing support and features.
affects: 0.17.0 and potentially older versions
gotchaAttempting to access `.ok_value` on an `Err` instance or `.err_value` on an `Ok` instance will result in an `AttributeError`. The `Ok` and `Err` classes are slotted (`__slots__`), preventing arbitrary attribute assignment and making instances immutable.fixAlways use type checks (e.g., `isinstance(result, Ok)` or `is_ok(result)`) or Python 3.10+ pattern matching to correctly narrow the type before accessing the contained value. Results are immutable; do not attempt to modify their properties directly.
affects: All versions
gotchaMyPy may sometimes struggle with type inference for `Result` types in certain scenarios, leading to 'Cannot infer type argument' errors.fixExplicitly add type hints (e.g., `my_var: Result[MySuccessType, MyErrorType] = ...`) and utilize `isinstance` checks to help MyPy correctly narrow types. The library's documentation suggests `isinstance(res, Ok)` as a workaround.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'result'
The 'result' library is not installed in the current Python environment or is not available on the system's Python path.
result.UnwrapError: Called unwrap() on an Err value
The `unwrap()` method was invoked on a `Result` object that holds an `Err` value, which is designed to raise an exception when a successful outcome is implicitly assumed.
fixif my_result.is_ok():
value = my_result.unwrap()
else:
error = my_result.err()
# Handle the error appropriately AttributeError: 'Ok' object has no attribute 'value'
Developers are attempting to access the underlying value of an `Ok` (or `Err`) instance directly using a non-existent `value` attribute, instead of using the provided methods.
fixmy_result_ok = Ok(10)
value = my_result_ok.ok()
# Or safely: value = my_result_ok.unwrap()
my_result_err = Err("Failed")
error_value = my_result_err.err() TypeError: Can't instantiate abstract class Result with abstract methods map, and_then, map_err, or_else, unwrap, unwrap_or, expect, ok, err, is_ok, is_err
The `Result` class is an abstract base class and cannot be directly instantiated; users should create concrete `Ok` or `Err` instances instead.
fixfrom result import Ok, Err
success = Ok(123)
failure = Err("Something went wrong") UnwrapError: Cannot unwrap an Err value
You attempted to retrieve the successful value from an `Err` object using `unwrap()`, which is designed to raise an error if the result is not `Ok`.
fixAlways check `is_ok()` before calling `unwrap()`, or use `unwrap_or()`, `unwrap_or_else()`, `ok()`, or `err()` for safer value extraction.
```python
from result import Ok, Err
def divide(a, b):
return Ok(a / b) if b != 0 else Err("Cannot divide by zero")
my_result = divide(10, 0)
# Incorrect: print(my_result.unwrap()) # This would raise UnwrapError
# Correct:
if my_result.is_ok():
print(my_result.unwrap())
else:
print(f"Error: {my_result.unwrap_err()}")
# Or using unwrap_or:
print(divide(10, 0).unwrap_or("Default value if error"))
``` Upgrade
Version history
0.17.0latest on PyPI · released Jun 2, 2024
Audit
Dependencies
No dependency data recorded yet.