codeant-ai-for-open-source[bot] commented on code in PR #39171: URL: https://github.com/apache/superset/pull/39171#discussion_r3624430154
########## superset-core/src/superset_core/extensions/context.py: ########## @@ -0,0 +1,117 @@ +# 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(), EphemeralSetOptions(ttl=3600)) + data = ctx.storage.ephemeral.get("cachedData") +""" + +from __future__ import annotations + +from typing import Protocol, TYPE_CHECKING + +if TYPE_CHECKING: + from superset_core.extensions.storage.ephemeral import EphemeralStateAccessor + from superset_core.extensions.storage.persistent import PersistentStateAccessor + from superset_core.extensions.types import BaseExtension + + +class ExtensionStorage(Protocol): + """Extension-scoped storage accessor for all available tiers.""" + + @property + def ephemeral(self) -> "EphemeralStateAccessor": + """Server-side cache (Redis/Memcached) with TTL.""" + ... + + @property + def persistent(self) -> "PersistentStateAccessor": + """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) Review Comment: **Suggestion:** The usage example passes `ttl` as a direct keyword argument, but the storage API uses an options object for write configuration; copying this example will raise a runtime argument error in extension code. Update the example to use the correct `set` call shape. [docstring mismatch] <details> <summary><b>Severity Level:</b> Major ⚠️</summary> ```mdx - ⚠️ Backend extensions using docstring example get TypeError. - ⚠️ Tier 2 ephemeral storage writes fail for copied pattern. - ⚠️ Developer onboarding hindered by misleading storage documentation. ``` </details> <details> <summary><b>Steps of Reproduction ✅ </b></summary> ```mdx 1. A backend extension author imports `get_context()` from `superset_core.extensions.context` and follows the example in the docstring of `get_context()` at `superset-core/src/superset_core/extensions/context.py:85-113`, specifically the line `ctx.storage.ephemeral.set("tempData", data, ttl=3600)` at line 108. 2. In their extension backend module (for example `superset/extensions/my_extension/backend.py`), they write: `ctx = get_context()` `ctx.storage.ephemeral.set("tempData", data, ttl=3600)` mirroring the docstring example instead of the earlier correct example using `EphemeralSetOptions` at `superset-core/src/superset_core/extensions/context.py:38-39`. 3. At runtime, the host implementation wires `ctx.storage.ephemeral` to an `EphemeralStateAccessor` instance defined in `superset-core/src/superset_core/extensions/storage/ephemeral.py:68`, whose `set()` method expects a third argument of type `EphemeralSetOptions` (options object) and does not declare a `ttl` keyword parameter. 4. When the extension code invokes `ctx.storage.ephemeral.set("tempData", data, ttl=3600)` the Python runtime attempts to pass `ttl` as a keyword argument to `EphemeralStateAccessor.set()`, which lacks such a parameter, causing a `TypeError` (unexpected keyword argument) and failing the extension’s storage write. ``` </details> [](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=e347bb9d335e4fd6ab019b02a0fa11c8&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=e347bb9d335e4fd6ab019b02a0fa11c8&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:** 108:108 **Comment:** *Docstring Mismatch: The usage example passes `ttl` as a direct keyword argument, but the storage API uses an options object for write configuration; copying this example will raise a runtime argument error in extension code. Update the example to use the correct `set` call shape. 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=d5f6a4b5bdc5f1a3e28a8aeaec8aa74299a7a5114d9ce80607eee009b0230484&reaction=like'>👍</a> | <a href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F39171&comment_hash=d5f6a4b5bdc5f1a3e28a8aeaec8aa74299a7a5114d9ce80607eee009b0230484&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]
