Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a warning category and stacklevel to rerun warnings.warn calls #2985

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions rerun_py/rerun_sdk/rerun/log/log_decorator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import functools
import logging
import traceback
import warnings
from typing import Any, Callable, TypeVar, cast
Expand All @@ -13,8 +12,11 @@

_TFunc = TypeVar("_TFunc", bound=Callable[..., Any])

# Default formatting for the `warnings` package is... non optimal.
warnings.formatwarning = lambda msg, *args, **kwargs: f"WARNING:rerun:{msg}\n"

class RerunWarning(Warning):
"""A custom warning class that we use to identify warnings that are emitted by the Rerun SDK itself."""

pass


def log_decorator(func: _TFunc) -> _TFunc:
Expand All @@ -35,7 +37,9 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
if not bindings.is_enabled(recording):
# NOTE: use `warnings` which handles runtime deduplication.
warnings.warn(
f"Rerun is disabled - {func.__name__}() call ignored. You must call rerun.init before using log APIs."
f"Rerun is disabled - {func.__name__}() call ignored. You must call rerun.init before using log APIs.",
category=RerunWarning,
stacklevel=2,
)
return

Expand All @@ -48,6 +52,6 @@ def wrapper(*args: Any, **kwargs: Any) -> Any:
except Exception as e:
warning = "".join(traceback.format_exception(e.__class__, e, e.__traceback__))
log_text_entry_internal("rerun", warning, level=LogLevel.WARN, recording=recording)
logging.warning(f"Ignoring rerun log call: {warning}")
warnings.warn(f"Ignoring rerun log call: {warning}", category=RerunWarning, stacklevel=2)

return cast(_TFunc, wrapper)