executor#

Collect PrecommitError instances from several executed functions.

class Executor(raise_exception: bool = True)[source]#

Bases: AbstractContextManager

Execute functions and collect any PrecommitError exceptions.

The Executor is a context manager that can be used to sequentially execute functions and collect any PrecommitError exceptions they raise. The collected exceptions are merged and re-raised as a new PrecommitError when the context manager exits.

To avoid raising the exceptions, set the raise_exception argument to False. The collected exceptions will then be printed to the console instead.

>>> def function1() -> None:
...     raise PrecommitError("Error message 1")
>>> def function2() -> None:
...     raise PrecommitError("Error message 2")
>>> def function3() -> None: ...
>>>
>>> with Executor(raise_exception=False) as execute:
...     execute(function1)
...     execute(function2)
...     execute(function3)
Error message 1
--------------------
Error message 2
__call__(function: ~typing.Callable[[~P], ~compwa_policy.utilities.executor.T], *args: ~typing.~P, **kwargs: ~typing.~P) T | None[source]#

Execute a function and collect any PrecommitError exceptions.

property error_messages: tuple[str, ...][source]#

View the collected error messages.

Note

Set COMPWA_POLICY_DEBUG=1 to enable profiling the execution times.

merge_messages() str[source]#
print_execution_times() None[source]#