API Reference

outcome.capture(sync_fn, *args, **kwargs)

Run sync_fn(*args, **kwargs) and capture the result.

Returns:Either a Value or Error as appropriate.
await outcome.acapture(async_fn, *args, **kwargs)

Run await async_fn(*args, **kwargs) and capture the result.

Returns:Either a Value or Error as appropriate.
class outcome.Outcome

An abstract class representing the result of a Python computation.

This class has two concrete subclasses: Value representing a value, and Error representing an exception.

In addition to the methods described below, comparison operators on Value and Error objects (==, <, etc.) check that the other object is also a Value or Error object respectively, and then compare the contained objects.

Outcome objects are hashable if the contained objects are hashable.

abstractmethod await asend(agen)

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)

Send or throw the contained value or exception into the given generator object.

Parameters:gen – A generator object supporting .send() and .throw() methods.
unwrap()

Return or raise the contained value or exception.

These two lines of code are equivalent:

x = fn(*args)
x = Result.capture(fn, *args).unwrap()
class outcome.Value(value)

Concrete Outcome subclass representing a regular value.

await asend(agen)

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)

Send or throw the contained value or exception into the given generator object.

Parameters:gen – A generator object supporting .send() and .throw() methods.
unwrap()

Return or raise the contained value or exception.

These two lines of code are equivalent:

x = fn(*args)
x = Result.capture(fn, *args).unwrap()
class outcome.Error(error)

Concrete Outcome subclass representing a raised exception.

await asend(agen)

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(it)

Send or throw the contained value or exception into the given generator object.

Parameters:gen – A generator object supporting .send() and .throw() methods.
unwrap()

Return or raise the contained value or exception.

These two lines of code are equivalent:

x = fn(*args)
x = Result.capture(fn, *args).unwrap()
class outcome.AlreadyUsedError

An Outcome can only be unwrapped once.