API Reference¶
- outcome.capture(sync_fn: Callable[ArgsT, ResultT], *args: ArgsT.args, **kwargs: ArgsT.kwargs) Value[ResultT] | Error¶
Run
sync_fn(*args, **kwargs)and capture the result.
- await outcome.acapture(async_fn: Callable[ArgsT, Awaitable[ResultT]], *args: ArgsT.args, **kwargs: ArgsT.kwargs) Value[ResultT] | Error¶
Run
await async_fn(*args, **kwargs)and capture the result.
- class outcome.Outcome¶
An abstract class representing the result of a Python computation.
This class has two concrete subclasses:
Valuerepresenting a value, andErrorrepresenting an exception.In addition to the methods described below, comparison operators on
ValueandErrorobjects (==,<, etc.) check that the other object is also aValueorErrorobject respectively, and then compare the contained objects.Outcomeobjects are hashable if the contained objects are hashable.- abstractmethod await asend(agen: AsyncGenerator[ResultT, ValueT]) ResultT¶
Send or throw the contained value or exception into the given async generator object.
- Parameters:
agen – An async generator object supporting
.asend()and.athrow()methods.
- abstractmethod send(gen: Generator[ResultT, ValueT, object]) ResultT¶
Send or throw the contained value or exception into the given generator object.
- Parameters:
gen – A generator object supporting
.send()and.throw()methods.
- abstractmethod unwrap() ValueT¶
Return or raise the contained value or exception.
These two lines of code are equivalent:
x = fn(*args) x = outcome.capture(fn, *args).unwrap()
- outcome.Maybe = Value[ResultT] | Error¶
A convenience alias to a union of both results. This allows type checkers to perform
exhaustiveness checking when isinstance() is used with either class:
outcome: Maybe[int] = capture(some_function, 1, 2, 3)
if isinstance(outcome, Value):
# Type checkers know it's a Value[int] here.
else:
# It must be an Error.
- class outcome.Value(value: ValueT)¶
Concrete
Outcomesubclass representing a regular value.- await asend(agen: AsyncGenerator[ResultT, ValueT]) ResultT¶
Send or throw the contained value or exception into the given async generator object.
- Parameters:
agen – An async generator object supporting
.asend()and.athrow()methods.
- send(gen: Generator[ResultT, ValueT, object]) ResultT¶
Send or throw the contained value or exception into the given generator object.
- Parameters:
gen – A generator object supporting
.send()and.throw()methods.
- unwrap() ValueT¶
Return or raise the contained value or exception.
These two lines of code are equivalent:
x = fn(*args) x = outcome.capture(fn, *args).unwrap()
- class outcome.Error(error: BaseException)¶
Concrete
Outcomesubclass representing a raised exception.- await asend(agen: AsyncGenerator[ResultT, NoReturn]) ResultT¶
Send or throw the contained value or exception into the given async generator object.
- Parameters:
agen – An async generator object supporting
.asend()and.athrow()methods.
- send(gen: Generator[ResultT, NoReturn, object]) ResultT¶
Send or throw the contained value or exception into the given generator object.
- Parameters:
gen – A generator object supporting
.send()and.throw()methods.
- unwrap() NoReturn¶
Return or raise the contained value or exception.
These two lines of code are equivalent:
x = fn(*args) x = outcome.capture(fn, *args).unwrap()
- class outcome.AlreadyUsedError¶
An Outcome can only be unwrapped once.