Skip to content

API Reference

Every symbol exported from pedros, grouped by area. For behavior details, edge cases, and runnable examples, follow the link into the Guide.

Decorators

timed

timed(func: Callable[P, R]) -> Callable[P, R]
timed(func: Callable[P, Awaitable[R]]) -> Callable[P, Awaitable[R]]
timed(*, log_level: str | None = 'DEBUG') -> Callable[[Callable[P, R]], Callable[P, R]]
timed(*, log_level: str | None = 'DEBUG') -> Callable[[Callable[P, Awaitable[R]]], Callable[P, Awaitable[R]]]
timed(func: Callable[P, Any] | None = None, *, log_level: str | None = 'DEBUG') -> Any

A decorator to measure and log the execution time of a function or method. It can be applied to both synchronous and asynchronous functions, and allows customization of the logging level.

Parameters:

Name Type Description Default
func Callable[P, Any] | None

The function to be decorated. If not provided, the decorator can be used with additional configuration through keyword arguments.

None
log_level str | None

The logging level to use for reporting execution time. Defaults to "DEBUG". If set to "NONE" (case insensitive), no logging will occur.

'DEBUG'

Returns:

Type Description
Any

A decorated function or an asynchronous coroutine that logs its execution time.

Guide

safe

safe(func: Callable[P, R]) -> Callable[P, R]
safe(func: Callable[P, Awaitable[R]]) -> Callable[P, Awaitable[R]]
safe(*, catch: type[Exception] | tuple[type[Exception], ...] = Exception, log_level: str | None = 'ERROR', re_raise: bool = True, on_error: Callable[[Exception], Any] | None = None, on_finally: Callable[[], Any] | None = None) -> Callable[[Callable[P, R]], Callable[P, R]]
safe(*, catch: type[Exception] | tuple[type[Exception], ...] = Exception, log_level: str | None = 'ERROR', re_raise: bool = True, on_error: Callable[[Exception], Any] | None = None, on_finally: Callable[[], Any] | None = None) -> Callable[[Callable[P, Awaitable[R]]], Callable[P, Awaitable[R]]]
safe(func: Callable[P, Any] | None = None, *, catch: type[Exception] | tuple[type[Exception], ...] = Exception, log_level: str | None = 'ERROR', re_raise: bool = True, on_error: Callable[[Exception], Any] | None = None, on_finally: Callable[[], Any] | None = None) -> Any

A decorator function for safely executing another function within a context where exceptions can be logged, handled, and optionally propagated or suppressed. This utility can also execute additional actions when an error occurs or after the final execution, whether an exception was raised or not.

Parameters:

Name Type Description Default
func Callable[P, Any] | None

The target function to be decorated. If not provided, the decorator is returned to be used with a target function. Defaults to None.

None
catch type[Exception] | tuple[type[Exception], ...]

The type of exception or a tuple of exception types to catch. Defaults to Exception.

Exception
log_level str | None

The logging level to use when an exception is caught. If set to "NONE", no logging is performed. Defaults to "ERROR".

'ERROR'
re_raise bool

Determines whether the caught exceptions should be re-raised after handling. If True, the exception is re-raised. If False, the exception is suppressed. Defaults to True.

True
on_error Callable[[Exception], Any] | None

An optional callable to be executed when an exception is caught. The callable should accept the exception instance as its parameter. Defaults to None.

None
on_finally Callable[[], Any] | None

An optional callable to be executed in a finally block after the function execution, regardless of whether an exception was raised or not. It does not accept any parameters. Defaults to None.

None

Returns:

Type Description
Any

If func is provided, it returns the decorated version of func. If func is None, it returns the decorator to be used with a target function.

Guide

trace

trace(func: Callable[P, R]) -> Callable[P, R]
trace(func: Callable[P, Awaitable[R]]) -> Callable[P, Awaitable[R]]
trace(*, log_level: str | None = 'DEBUG') -> Callable[[Callable[P, R]], Callable[P, R]]
trace(*, log_level: str | None = 'DEBUG') -> Callable[[Callable[P, Awaitable[R]]], Callable[P, Awaitable[R]]]
trace(func: Callable[P, Any] | None = None, *, log_level: str | None = 'DEBUG') -> Any

A decorator to log a function's arguments and return value, or the exception it raised. It never catches or suppresses exceptions, purely observational, and can be applied to both synchronous and asynchronous functions.

Parameters:

Name Type Description Default
func Callable[P, Any] | None

The function to be decorated. If not provided, the decorator can be used with additional configuration through keyword arguments.

None
log_level str | None

The logging level to use for call/return/error traces. Defaults to "DEBUG". If set to "NONE" (case insensitive), no logging will occur.

'DEBUG'

Returns:

Type Description
Any

A decorated function or an asynchronous coroutine that logs its calls.

Guide

monitor

monitor(func: Callable[P, R]) -> Callable[P, R]
monitor(func: Callable[P, Awaitable[R]]) -> Callable[P, Awaitable[R]]
monitor(func: Callable[P, Any]) -> Any

A decorator that combines @trace, @timed, and @safe with their default settings, with no configuration of its own. Logs the call and return value, times execution, and catches/logs/re-raises exceptions.

Equivalent to stacking all three with their defaults:

@trace
@timed
@safe
def f(): ...

Parameters:

Name Type Description Default
func Callable[P, Any]

The function to be decorated.

required

Returns:

Type Description
Any

The decorated function, wrapped with tracing, timing, and error handling.

Guide

Logging

setup_logging

setup_logging(level: int | str = logging.INFO, logger_name: str = _LIBRARY_LOGGER_NAME, *, add_handler: bool | None = None, propagate: bool | None = None) -> None

Configure logging for one logger without reconfiguring root logging.

This function attempts to use Rich's RichHandler for enhanced, colorful, and trace-friendly logging. If Rich is not installed, it silently falls back to Python's standard logging handler. See more about Rich (https://pypi.org/project/rich/).

Calling this explicitly always takes precedence over pedros's own auto-configuration (see :func:get_logger), regardless of call order: it re-derives handler/propagation state from the current root logging setup every time it runs.

Parameters:

Name Type Description Default
level int | str

Logging level to use as integer or string name. Defaults to logging.INFO.

INFO
logger_name str

Logger to configure. Defaults to the pedros package logger.

_LIBRARY_LOGGER_NAME
add_handler bool | None

Whether to attach a handler to logger_name. By default, a handler is added only when neither root nor the target logger has handlers.

None
propagate bool | None

Whether records should propagate to ancestor loggers. By default, propagation is enabled only when root logging is the selected handler path.

None

Returns:

Type Description
None

None

Guide

get_logger

get_logger(name: str | None = None) -> logging.Logger

Return a logger instance.

If no name is provided, the pedros package logger is returned, and it is auto-configured on first use if nothing has configured it yet. This is what powers pedros's own decorators and utilities without requiring any setup. Auto-configuration is deterministic (own handler, no propagation) regardless of call order relative to the host application's logging setup; call :func:setup_logging explicitly to integrate with root logging instead.

Parameters:

Name Type Description Default
name str | None

Name of the logger. If None, defaults to the pedros logger.

None

Returns:

Type Description
Logger

A configured logger instance.

Guide

normalize_log_level

normalize_log_level(log_level: int | str | None) -> int | None

Normalize a log level to a numeric logging value.

Parameters:

Name Type Description Default
log_level int | str | None

A numeric level, a textual level (e.g., "INFO"), "NONE", or None.

required

Returns:

Type Description
int | None

Numeric level, or None for no logging / when disabled.

Raises:

Type Description
ValueError

If the provided level is unknown.

Progress bars

progbar

progbar(iterable: Iterable[ItemType], *args: Any, backend: Backend | str = 'auto', **kwargs: Any) -> Iterable[ItemType]

Displays a progress bar for the provided iterable, using a chosen backend library.

This function iterates over the elements of the given iterable and optionally displays a progress bar using either the rich or tqdm libraries. The choice of backend can be explicitly specified, or automatically determined based on the libraries available in the environment. If no supported library is found or the backend is set to "none", no progress bar will be displayed and the original iterable will be returned.

Parameters:

Name Type Description Default
iterable Iterable[ItemType]

The iterable whose elements should be iterated over with an optional progress bar.

required
args Any

Positional arguments to pass to the progress bar library.

()
backend Backend | str

The progress bar backend to use. Supported values are: - "auto" (default): Automatically selects a backend based on available libraries. - "rich": Uses the rich.progress library, if available. - "tqdm": Uses the tqdm library, if available. - "none": Disables the progress bar and returns the original iterable.

'auto'
kwargs Any

Additional keyword arguments to customize the behavior of the progress bar. These are dependent on the backend used: - For tqdm, desc and other tqdm-specific parameters can be provided. - For rich.progress, description and other rich-specific arguments can be used.

{}

Returns:

Type Description
Iterable[ItemType]

The input iterable, optionally wrapped with a progress bar based on the selected backend.

Guide

Dependency checks

has_dep

has_dep(name: str, version: str | None = None) -> bool

Check if a specified dependency is available on the system.

This function verifies the availability of a Python module or package by checking if it can be located using the Python runtime's import system. When a version constraint is provided, the dependency must also satisfy the constraint using installed distribution metadata.

Parameters:

Name Type Description Default
name str

The name of the dependency to check.

required
version str | None

Optional exact version or comma-separated range constraint.

None

Returns:

Type Description
bool

True if the dependency is available, False otherwise.

Guide