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,
contextis copied when the context is entered.context_provideris 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.delayedcaptures an active context for execution in a worker process, its fixed metadata andrecord_onlysetting are restored for that task. A propagatedcontext_provideris 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. Whentpcp.parallel.Parallelis paired withtpcp.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. Withn_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
WarningErrorContextwhoserecordslist 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.startand deactivated withWarningErrorContext.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 thewithform whenever exception-safe cleanup or contextualized exceptions are required.Setting
record_only=Trueon an outer context suppresses warning forwarding andprint_with_contextoutput throughout its nested contexts while still recording those events. It does not suppress exceptions or ordinaryprintcalls. 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.