montasaurus/result: A simple Rust-like Result type for Python 3
a year ago
- #Python
- #Error Handling
- #Type Safety
- A Python 3 Result type inspired by Rust, fully type annotated, for handling Ok(value) or Err(error) outcomes.
- Forked from an unmaintained library, this version is experimental and primarily used for personal projects.
- Result[T, E] is a generic type alias for typing.Union[Ok[T], Err[E]], simplifying error handling without custom exceptions.
- Provides methods like is_ok, is_err, unwrap, expect, and map for managing Result types, similar to Rust's approach.
- Supports Python 3.10+ match statements for elegant pattern matching between Ok and Err cases.
- Includes utility functions like as_result decorator for converting exceptions into Result types and do notation for chaining operations.
- Features like unwrap_or, map_or, and map_err offer flexible ways to handle and transform values and errors.
- Designed with memory efficiency in mind, using __slots__ to prevent arbitrary attribute assignments.
- Compatible with async operations through do_async for awaiting results within do notation.
- Addresses common MyPy issues with type inference and provides workarounds for better type safety.