Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
from .result import Result, Ok, Err
res1 = Ok('hello') # type: Result[str, int]
if isinstance(res1, Ok):
ok = res1 # type: Ok[str]
okValue = res1.ok() # type: str
mapped_to_float = res1.map_or(1.0, lambda s: len(s) * 1.5) # type: float
else:
err = res1 # type: Err[int]
errValue = err.err() # type: int
mapped_to_list = res1.map_err(lambda e: [e]).err() # type: Optional[List[int]]
# Test constructor functions
res1 = Ok()
res2 = Ok(42)
res3 = Err(1)
from .result import Result, Ok, Err
res1 = Ok('hello') # type: Result[str, int]
if isinstance(res1, Ok):
ok = res1 # type: Ok[str]
okValue = res1.ok() # type: str
mapped_to_float = res1.map_or(1.0, lambda s: len(s) * 1.5) # type: float
else:
err = res1 # type: Err[int]
errValue = err.err() # type: int
mapped_to_list = res1.map_err(lambda e: [e]).err() # type: Optional[List[int]]
# Test constructor functions
res1 = Ok()
res2 = Ok(42)
res3 = Err(1)
from typing import List, Optional
from .result import Result, Ok, Err
res1 = Ok('hello') # type: Result[str, int]
if isinstance(res1, Ok):
ok = res1 # type: Ok[str]
okValue = res1.ok() # type: str
mapped_to_float = res1.map_or(1.0, lambda s: len(s) * 1.5) # type: float
else:
err = res1 # type: Err[int]
errValue = err.err() # type: int
mapped_to_list = res1.map_err(lambda e: [e]).err() # type: Optional[List[int]]
# Test constructor functions
res1 = Ok()
res2 = Ok(42)
res3 = Err(1)
from typing import List, Optional
from .result import Result, Ok, Err
res1 = Ok('hello') # type: Result[str, int]
if isinstance(res1, Ok):
ok = res1 # type: Ok[str]
okValue = res1.ok() # type: str
mapped_to_float = res1.map_or(1.0, lambda s: len(s) * 1.5) # type: float
else:
err = res1 # type: Err[int]
errValue = err.err() # type: int
mapped_to_list = res1.map_err(lambda e: [e]).err() # type: Optional[List[int]]
# Test constructor functions
res1 = Ok()
res2 = Ok(42)
res3 = Err(1)
def __eq__(self, other: Any) -> bool:
return isinstance(other, Ok) and self.value == other.value
def map(self, op: Callable[[T], U]) -> 'Result[U, E]':
"""
The contained result is `Ok`, so return `Ok` with original value mapped to
a new value using the passed in function.
"""
return Ok(op(self._value))
def map_err(self, op: Callable[[E], F]) -> 'Result[T, F]':
"""
The contained result is `Err`, so return `Err` with original error mapped to
a new value using the passed in function.
"""
return Err(op(self._value))
# define Result as a generic type alias for use
# in type annotations
"""
A simple `Result` type inspired by Rust.
Not all methods (https://doc.rust-lang.org/std/result/enum.Result.html)
have been implemented, only the ones that make sense in the Python context.
"""
Result = Union[Ok[T], Err[E]]
"""
A type to use in `isinstance` checks.
This is purely for convenience sake, as you could also just write `isinstance(res, (Ok, Err))
"""
OkErr = (Ok, Err)
class UnwrapError(Exception):
pass