codeant-ai-for-open-source[bot] commented on code in PR #39171: URL: https://github.com/apache/superset/pull/39171#discussion_r3622494042
########## superset-core/src/superset_core/extensions/context.py: ########## @@ -0,0 +1,136 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +""" +Extension Context API for superset-core extensions. + +Provides access to the current extension's context, including metadata +and scoped resources like storage. Extensions call `get_context()` to +access their context during execution. + +The context is set by the host (Superset) during extension loading and +is only available within extension code. + +Usage: + from superset_core.extensions.context import get_context + + def setup(): + ctx = get_context() + + # Access extension metadata + print(f"Running {ctx.extension.displayName} v{ctx.extension.version}") + + # Access extension-scoped storage + ctx.storage.ephemeral.set("lastRun", time.time()) + data = ctx.storage.ephemeral.get("cachedData") +""" + +from __future__ import annotations + +from typing import Any, Protocol, TYPE_CHECKING + +if TYPE_CHECKING: + from superset_core.extensions.types import BaseExtension + + +class StorageAccessor(Protocol): + """Protocol for storage access with user-scoped and shared modes.""" + + def get(self, key: str) -> Any: + """Get a value from storage.""" + ... + + def set(self, key: str, value: Any, ttl: int = 3600) -> None: + """Set a value in storage with optional TTL.""" + ... + + def remove(self, key: str) -> None: + """Remove a value from storage.""" + ... + + @property + def shared(self) -> "StorageAccessor": + """Shared (cross-user) storage accessor.""" + ... + + +class ExtensionStorage(Protocol): + """Extension-scoped storage accessor for all available tiers.""" + + @property + def ephemeral(self) -> StorageAccessor: + """Server-side cache (Redis/Memcached) with TTL.""" + ... + + @property + def persistent(self) -> StorageAccessor: + """Database-backed persistent storage.""" + ... + + +class ExtensionContext(Protocol): + """ + Context object providing extension-specific resources. + + This context is only available during extension execution. + Calling `get_context()` outside of an extension will raise an error. + """ + + @property + def extension(self) -> "BaseExtension": + """Metadata about the current extension.""" + ... + + @property + def storage(self) -> ExtensionStorage: + """Extension-scoped storage across all available tiers.""" + ... + + +def get_context() -> ExtensionContext: + """ + Get the current extension's context. + + This function returns the context for the currently executing extension, + providing access to extension metadata and scoped resources like storage. + + Host implementations will replace this function during initialization + with a concrete implementation providing actual functionality. + + :returns: The current extension's context. + :raises RuntimeError: If called outside of an extension context. + + Example: + from superset_core.extensions.context import get_context + + ctx = get_context() + + # Access extension metadata + print(f"Extension: {ctx.extension.id}") + print(f"Version: {ctx.extension.version}") + + # Access extension-scoped storage + ctx.storage.ephemeral.set("tempData", data, ttl=3600) + value = ctx.storage.ephemeral.get("tempData") + + # Access shared (cross-user) storage + ctx.storage.ephemeral.shared.set("globalCounter", count) + """ + raise NotImplementedError( + "get_context() must be called within an extension context. " + "This function is replaced by the host during extension loading." + ) Review Comment: **Suggestion:** The function documentation says a `RuntimeError` is raised, but the implementation raises `NotImplementedError`; this mismatch can break callers/tests that rely on the documented exception type. Update the docs or raise the documented exception consistently. [docstring mismatch] <details> <summary><b>Severity Level:</b> Major ⚠️</summary> ```mdx - ⚠️ Docstring misleads callers about raised exception type. - ⚠️ Tests expecting RuntimeError from get_context may fail. - ⚠️ Extension code may catch wrong exception class. ``` </details> <details> <summary><b>Steps of Reproduction ✅ </b></summary> ```mdx 1. In `superset-core/src/superset_core/extensions/context.py`, locate the `get_context` function defined at lines 104-132; its docstring at line 115 states `:raises RuntimeError: If called outside of an extension context.`. 2. Observe the implementation at lines 133-136, where `get_context()` ends by raising `NotImplementedError` with a message explaining that the host replaces this function during extension loading. 3. Start a Python interpreter or unit test that imports `get_context` from `superset_core.extensions.context` without any Superset host initialization that would patch in a concrete implementation. 4. Call `get_context()` directly; the default implementation executes lines 133-136 and raises `NotImplementedError`, contradicting the documented `RuntimeError` in line 115, so any caller or test written to catch `RuntimeError` will mis-handle the error. ``` </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=58376ce1938a48898dcf0bfc5064b340&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) [](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=58376ce1938a48898dcf0bfc5064b340&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset) *(Use Cmd/Ctrl + Click for best experience)* <details> <summary><b>Prompt for AI Agent 🤖 </b></summary> ```mdx This is a comment left during a code review. **Path:** superset-core/src/superset_core/extensions/context.py **Line:** 115:136 **Comment:** *Docstring Mismatch: The function documentation says a `RuntimeError` is raised, but the implementation raises `NotImplementedError`; this mismatch can break callers/tests that rely on the documented exception type. Update the docs or raise the documented exception consistently. Validate the correctness of the flagged issue. If correct, How can I resolve this? If you propose a fix, implement it and please make it concise. Once fix is implemented, also check other comments on the same PR, and ask user if the user wants to fix the rest of the comments as well. if said yes, then fetch all the comments validate the correctness and implement a minimal fix ``` </details> <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F39171&comment_hash=aef640b51f0e0c8cfc799bcb027ae90dde98b67f5683235c543f487abc8b7f93&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F39171&comment_hash=aef640b51f0e0c8cfc799bcb027ae90dde98b67f5683235c543f487abc8b7f93&reaction=dislike'>👎</a> -- 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]
