""" Result type for handling success/error cases """ from typing import TypeVar, Generic, Optional, Callable from dataclasses import dataclass T = TypeVar('T') E = TypeVar('E') @dataclass class Success(Generic[T]): """Successful result with value""" value: T def is_success(self) -> bool: return True def is_error(self) -> bool: return False def map(self, func: Callable[[T], 'Success']) -> 'Success[T]': """Apply function to value""" return func(self.value) @dataclass class Error(Generic[E]): """Error result with error value""" error: E def is_success(self) -> bool: return False def is_error(self) -> bool: return True def map(self, func: Callable) -> 'Error[E]': """Return self on error""" return self Result = Success[T] | Error[E]