michael-s-molina commented on code in PR #39171:
URL: https://github.com/apache/superset/pull/39171#discussion_r3560999616


##########
superset/extensions/context.py:
##########
@@ -16,75 +16,124 @@
 # under the License.
 
 """
-Extension Context Management - provides ambient context during extension 
loading.
+Extension Context Management - provides ambient context for extensions.
 
-This module provides a thread-local context system that allows decorators to
-automatically detect whether they are being called in host or extension code
-during extension loading.
+This module provides a context system using Python's contextvars that allows
+extensions to access their context (metadata and scoped resources) via 
get_context().
+
+The context is set during extension loading and when extension callbacks are 
invoked.
+Uses ContextVar for thread-safe and async-safe context management with 
automatic
+save/restore for nested contexts.
 """
 
 from __future__ import annotations
 
-import contextlib
-from threading import local
-from typing import Any, Generator
+from contextlib import contextmanager
+from contextvars import ContextVar
+from typing import Any, Iterator
 
 from superset_core.extensions.types import Manifest
 
-# Thread-local storage for extension context
-_extension_context: local = local()
-
 
-class ExtensionContext:
-    """Manages ambient extension context during loading."""
+class ExtensionStorage:
+    """Extension storage with all available tiers."""
 
-    def __init__(self, manifest: Manifest):
-        self.manifest = manifest
+    @property
+    def ephemeral(self) -> Any:
+        from superset.extensions.storage.ephemeral import EphemeralState
 
-    def __enter__(self) -> "ExtensionContext":
-        if getattr(_extension_context, "current", None) is not None:
-            current_extension = _extension_context.current.manifest.id
-            raise RuntimeError(
-                f"Cannot initialize extension {self.manifest.id} while 
extension "
-                f"{current_extension} is already being initialized. "
-                f"Nested extension initialization is not supported."
-            )
+        return EphemeralState
 
-        _extension_context.current = self
-        return self
+    @property
+    def persistent(self) -> Any:
+        from superset.extensions.storage.persistent import PersistentState
 
-    def __exit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
-        # Clear the current context
-        _extension_context.current = None
+        return PersistentState
 
 
-class ExtensionContextWrapper:
-    """Wrapper for extension context with extensible properties."""
+class ConcreteExtensionContext:
+    """Concrete implementation of ExtensionContext for the host."""
 
     def __init__(self, manifest: Manifest):
         self._manifest = manifest
+        self._storage = ExtensionStorage()
+
+    @property
+    def extension(self) -> Manifest:
+        """Extension metadata (new API)."""

Review Comment:
   Done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to