From 61c6728006fc5bdd3fca278bcdce2e1bc6841764 Mon Sep 17 00:00:00 2001 From: Elijah Ahianyo Date: Tue, 26 Mar 2024 16:33:55 +0000 Subject: [PATCH] [REF-2229]Dedupe deprecation warnings (#2871) * Dedupe deprecation warnings * Update reflex/utils/console.py Co-authored-by: Masen Furer * address PR comments --------- Co-authored-by: Masen Furer --- reflex/components/component.py | 1 + reflex/utils/console.py | 22 +++++++++++++++------- 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/reflex/components/component.py b/reflex/components/component.py index 41c7494cc..6b25a4f3a 100644 --- a/reflex/components/component.py +++ b/reflex/components/component.py @@ -621,6 +621,7 @@ class Component(BaseComponent, ABC): reason=f"for consistency. Use `{prop}` instead.", deprecation_version="0.4.0", removal_version="0.5.0", + dedupe=False, ) props[prop] = props.pop(under_prop) diff --git a/reflex/utils/console.py b/reflex/utils/console.py index 1580da706..ef37a2aa5 100644 --- a/reflex/utils/console.py +++ b/reflex/utils/console.py @@ -16,6 +16,9 @@ _console = Console() # The current log level. _LOG_LEVEL = LogLevel.INFO +# Deprecated features who's warning has been printed. +_EMITTED_DEPRECATION_WARNINGS = set() + def set_log_level(log_level: LogLevel): """Set the log level. @@ -111,6 +114,7 @@ def deprecate( reason: str, deprecation_version: str, removal_version: str, + dedupe: bool = True, **kwargs, ): """Print a deprecation warning. @@ -119,15 +123,19 @@ def deprecate( feature_name: The feature to deprecate. reason: The reason for deprecation. deprecation_version: The version the feature was deprecated - removal_version: The version the deprecated feature will be removed. + removal_version: The version the deprecated feature will be removed + dedupe: If True, suppress multiple console logs of deprecation message. kwargs: Keyword arguments to pass to the print function. """ - msg = ( - f"{feature_name} has been deprecated in version {deprecation_version} {reason.rstrip('.')}. It will be completely " - f"removed in {removal_version}" - ) - if _LOG_LEVEL <= LogLevel.WARNING: - print(f"[yellow]DeprecationWarning: {msg}[/yellow]", **kwargs) + if feature_name not in _EMITTED_DEPRECATION_WARNINGS: + msg = ( + f"{feature_name} has been deprecated in version {deprecation_version} {reason.rstrip('.')}. It will be completely " + f"removed in {removal_version}" + ) + if _LOG_LEVEL <= LogLevel.WARNING: + print(f"[yellow]DeprecationWarning: {msg}[/yellow]", **kwargs) + if dedupe: + _EMITTED_DEPRECATION_WARNINGS.add(feature_name) def error(msg: str, **kwargs):