Browse Source

[REF-2229]Dedupe deprecation warnings (#2871)

* Dedupe deprecation warnings

* Update reflex/utils/console.py

Co-authored-by: Masen Furer <m_github@0x26.net>

* address PR comments

---------

Co-authored-by: Masen Furer <m_github@0x26.net>
Elijah Ahianyo 1 year ago
parent
commit
61c6728006
2 changed files with 16 additions and 7 deletions
  1. 1 0
      reflex/components/component.py
  2. 15 7
      reflex/utils/console.py

+ 1 - 0
reflex/components/component.py

@@ -621,6 +621,7 @@ class Component(BaseComponent, ABC):
                     reason=f"for consistency. Use `{prop}` instead.",
                     reason=f"for consistency. Use `{prop}` instead.",
                     deprecation_version="0.4.0",
                     deprecation_version="0.4.0",
                     removal_version="0.5.0",
                     removal_version="0.5.0",
+                    dedupe=False,
                 )
                 )
                 props[prop] = props.pop(under_prop)
                 props[prop] = props.pop(under_prop)
 
 

+ 15 - 7
reflex/utils/console.py

@@ -16,6 +16,9 @@ _console = Console()
 # The current log level.
 # The current log level.
 _LOG_LEVEL = LogLevel.INFO
 _LOG_LEVEL = LogLevel.INFO
 
 
+# Deprecated features who's warning has been printed.
+_EMITTED_DEPRECATION_WARNINGS = set()
+
 
 
 def set_log_level(log_level: LogLevel):
 def set_log_level(log_level: LogLevel):
     """Set the log level.
     """Set the log level.
@@ -111,6 +114,7 @@ def deprecate(
     reason: str,
     reason: str,
     deprecation_version: str,
     deprecation_version: str,
     removal_version: str,
     removal_version: str,
+    dedupe: bool = True,
     **kwargs,
     **kwargs,
 ):
 ):
     """Print a deprecation warning.
     """Print a deprecation warning.
@@ -119,15 +123,19 @@ def deprecate(
         feature_name: The feature to deprecate.
         feature_name: The feature to deprecate.
         reason: The reason for deprecation.
         reason: The reason for deprecation.
         deprecation_version: The version the feature was deprecated
         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.
         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):
 def error(msg: str, **kwargs):