stouputils.decorators.silent module#

silent(
func: Callable[..., T],
*,
mute_stderr: bool = False,
replay_on_error: bool = False,
error_log_level: int | None = None,
watch_loggers: Sequence[str] | None = None,
) Callable[..., T][source]#
silent(
func: None = None,
*,
mute_stderr: bool = False,
replay_on_error: bool = False,
error_log_level: int | None = None,
watch_loggers: Sequence[str] | None = None,
) Callable[[Callable[..., T]], Callable[..., T]]

Decorator that makes a function silent (disable stdout, and stderr if specified).

Alternative to Muffle.

Parameters:
  • func (Callable[..., T] | None) – Function to make silent

  • mute_stderr (bool) – Whether to mute stderr or not

  • replay_on_error (bool) – Capture output and replay it if the call errors (see Muffle)

  • error_log_level (int | None) – Also treat log records at/above this level as an error (e.g. logging.ERROR)

  • watch_loggers (Sequence[str] | None) – Names of the loggers to watch for error_log_level (default: root logger only)

Examples

>>> @silent
... def test():
...     print("Hello, world!")
>>> test()
>>> @silent(mute_stderr=True)
... def test2():
...     print("Hello, world!")
>>> test2()
>>> silent(print)("Hello, world!")
>>> # Only shows the output if the wrapped call fails
>>> @silent(replay_on_error=True)
... def test3():
...     print("Context that explains the failure below")
...     raise ValueError("boom")
>>> try:
...     test3()
... except ValueError:
...     pass
Context that explains the failure below