warning_error_context#

tpcp.misc.warning_error_context(name: str, context: dict[str, Any] | None = None, /, *, context_provider: Callable[[], Mapping[str, Any]] | None = None, record_only: bool = False) WarningErrorContext[source]#

Add structured context information to warnings and exceptions raised in the context.

If provided, context is copied when the context is entered. context_provider is evaluated whenever context is rendered, allowing diagnostics to include state that changes while the context is active. It can be used without a fixed context dictionary. The provider must return a mapping and must not repeat fixed context keys.

A failing provider is represented in the rendered context instead of masking the warning or exception that caused context rendering.

Python applies warning filters before this context manager attaches its metadata. Consequently, different context values do not make otherwise identical warnings count as distinct occurrences. If every contextual occurrence must be shown or recorded, use an appropriate warning filter such as warnings.simplefilter("always").

When tpcp.parallel.delayed captures an active context for execution in a worker process, its fixed metadata and record_only setting are restored for that task. A propagated context_provider is evaluated once when the worker task enters the restored context, so all events in that task use the same snapshot. Context providers created inside the worker retain their normal dynamic behavior. When tpcp.parallel.Parallel is paired with tpcp.parallel.delayed, records from successful tasks are merged back into the original context. Records from a task that raises are not recovered from a separate worker. With n_jobs=1, execution remains inline: providers retain their normal dynamic behavior, and the active parent context directly records exceptions that escape the task.

The context manager yields a WarningErrorContext whose records list remains available after the context exits. It contains warnings that reached Python’s warning dispatcher and exceptions that escaped the context. Original warning and exception objects are retained so callers can inspect their concrete types and custom attributes.

The returned object can also be activated with WarningErrorContext.start and deactivated with WarningErrorContext.stop, which avoids indenting a long script. Manual use cannot automatically record or annotate an exception: stop() does not receive exception information, and an exception that skips the call leaves the context active. Use the with form whenever exception-safe cleanup or contextualized exceptions are required.

Setting record_only=True on an outer context suppresses warning forwarding and print_with_context output throughout its nested contexts while still recording those events. It does not suppress exceptions or ordinary print calls. Warning filters still run before recording, including in record-only mode.

On Python 3.9 and 3.10, exception context is stored in __notes__ but is not displayed by Python’s standard traceback renderer. Traceback renderers that support exception notes, such as Rich, display this context on those versions.

Examples using tpcp.misc.warning_error_context#

Warning and Error Context

Warning and Error Context