From ce95d914431f23005c21e6dab1c8691bda3461bc Mon Sep 17 00:00:00 2001 From: Justin Chu Date: Wed, 23 Oct 2024 23:23:28 +0000 Subject: [PATCH] Warn once --- onnxscript/_internal/deprecation.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/onnxscript/_internal/deprecation.py b/onnxscript/_internal/deprecation.py index 301565c8d..7bf18482a 100644 --- a/onnxscript/_internal/deprecation.py +++ b/onnxscript/_internal/deprecation.py @@ -12,6 +12,12 @@ T = TypeVar("T") +@functools.lru_cache(maxsize=1024) +def _warn_once(message: str): + """Issue a FutureWarning only once per message.""" + warnings.warn(message, category=FutureWarning, stacklevel=3) + + def deprecated(since: str, removed_in: str, instructions: str) -> Callable[[T], T]: """Marks functions as deprecated. @@ -30,12 +36,10 @@ def deprecated(since: str, removed_in: str, instructions: str) -> Callable[[T], def decorator(function): @functools.wraps(function) def wrapper(*args, **kwargs): - warnings.warn( + _warn_once( f"'{function.__module__}.{function.__qualname__}' " f"is deprecated in version {since} and will be " f"removed in {removed_in}. Please {instructions}.", - category=FutureWarning, - stacklevel=2, ) return function(*args, **kwargs)