mirror of
https://github.com/apache/superset.git
synced 2026-05-28 11:15:24 +00:00
Follow-up to #40231 (merged), where a reviewer flagged a function-body `from datetime import datetime, timedelta` instead of a top-of-file import. Adds a `ruff-import-placement` pre-commit hook running `ruff check --select PLC0415 --preview --no-fix`. Per @rusackas's pushback on the first cut of this PR — which spammed 2,657 `# noqa: PLC0415` annotations across ~410 files without fixing anything — this revision is a much smaller surface area: 1. **Per-file-ignores** for whole directories where function-body imports are a deliberate pattern, not an oversight: - `superset/cli/**` and `scripts/**`: subcommand-deferred imports keep heavy modules out of the CLI startup path. - `superset/tasks/**`: Celery task bodies defer imports of the modules they orchestrate. - `superset/migrations/versions/**`: Alembic migrations interact with model state at runtime, not at module load. - `superset/mcp_service/**`: MCP tools lazy-load resources on invocation so the server can register many tools without paying their import cost at startup. - `superset/db_engine_specs/**`: engine specs defer driver imports so optional DB drivers don't have to be installed. - `superset/initialization/__init__.py`, `superset/extensions/__init__.py`, `superset/app.py`: the app-factory and extension wiring are intentionally full of circular-import workarounds. - `tests/**`: test files routinely defer imports for fixture isolation; the rule still applies to production code. 2. **Per-line `# noqa: PLC0415`** on the 259 remaining genuine circular-import sites (security/manager.py, sql/execution/executor.py, semantic_layers/labels.py, tags/core.py, core_api_injection.py, etc.). These are foundational modules where moving the imports up would actually break things. Net result: ~410 files / 2,657 grandfathered → ~73 files / 259 actual noqa annotations. The rule still catches every new function-body import outside the explicitly-allowed directories. Also: silences a pre-existing C901 on `mcp_service/sql_lab/tool/execute_sql.py` that fires under newer local ruff but not CI's pinned ruff 0.9.7 — blocks the local pre-commit run otherwise. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
234 lines
7.5 KiB
Python
234 lines
7.5 KiB
Python
# 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.
|
|
import mimetypes
|
|
from io import BytesIO
|
|
from typing import Any
|
|
|
|
from flask import send_file
|
|
from flask.wrappers import Response
|
|
from flask_appbuilder.api import BaseApi, expose, protect, safe
|
|
|
|
from superset.extensions.utils import (
|
|
build_extension_data,
|
|
get_extensions,
|
|
)
|
|
|
|
|
|
class ExtensionsRestApi(BaseApi):
|
|
allow_browser_login = True
|
|
resource_name = "extensions"
|
|
|
|
def response(self, status_code: int, **kwargs: Any) -> Response:
|
|
"""Helper method to create JSON responses."""
|
|
from flask import jsonify # noqa: PLC0415
|
|
|
|
return jsonify(kwargs), status_code
|
|
|
|
def response_404(self) -> Response:
|
|
"""Helper method to create 404 responses."""
|
|
from flask import jsonify # noqa: PLC0415
|
|
|
|
return jsonify({"message": "Not found"}), 404
|
|
|
|
@expose("/_info", methods=("GET",))
|
|
@protect()
|
|
@safe
|
|
def info(self, **kwargs: Any) -> Response:
|
|
"""Get API info including permissions.
|
|
---
|
|
get:
|
|
summary: Get API info
|
|
responses:
|
|
200:
|
|
description: API info
|
|
content:
|
|
application/json:
|
|
schema:
|
|
type: object
|
|
properties:
|
|
permissions:
|
|
type: array
|
|
items:
|
|
type: string
|
|
"""
|
|
return self.response(200, permissions=["can_read"])
|
|
|
|
# TODO: Support the q parameter
|
|
@protect()
|
|
@safe
|
|
@expose("/", methods=("GET",))
|
|
def get_list(self, **kwargs: Any) -> Response:
|
|
"""List all enabled extensions.
|
|
---
|
|
get_list:
|
|
summary: List all enabled extensions.
|
|
responses:
|
|
200:
|
|
description: List of all enabled extensions
|
|
content:
|
|
application/json:
|
|
schema:
|
|
type: object
|
|
properties:
|
|
result:
|
|
type: array
|
|
items:
|
|
type: object
|
|
properties:
|
|
remoteEntry:
|
|
type: string
|
|
remoteEntry:
|
|
type: string
|
|
400:
|
|
$ref: '#/components/responses/400'
|
|
401:
|
|
$ref: '#/components/responses/401'
|
|
422:
|
|
$ref: '#/components/responses/422'
|
|
500:
|
|
$ref: '#/components/responses/500'
|
|
"""
|
|
result = []
|
|
extensions = get_extensions()
|
|
for extension in extensions.values():
|
|
extension_data = build_extension_data(extension)
|
|
result.append(extension_data)
|
|
|
|
response = {
|
|
"result": result,
|
|
"count": len(result),
|
|
}
|
|
|
|
return self.response(200, **response)
|
|
|
|
@protect()
|
|
@safe
|
|
@expose("/<publisher>/<name>", methods=("GET",))
|
|
def get(self, publisher: str, name: str, **kwargs: Any) -> Response:
|
|
"""Get an extension by its publisher and name.
|
|
---
|
|
get:
|
|
summary: Get an extension by its publisher and name.
|
|
parameters:
|
|
- in: path
|
|
schema:
|
|
type: string
|
|
name: publisher
|
|
- in: path
|
|
schema:
|
|
type: string
|
|
name: name
|
|
responses:
|
|
200:
|
|
description: Extension details
|
|
content:
|
|
application/json:
|
|
schema:
|
|
type: object
|
|
properties:
|
|
result:
|
|
type: array
|
|
items:
|
|
type: object
|
|
properties:
|
|
remoteEntry:
|
|
type: string
|
|
remoteEntry:
|
|
type: string
|
|
400:
|
|
$ref: '#/components/responses/400'
|
|
401:
|
|
$ref: '#/components/responses/401'
|
|
422:
|
|
$ref: '#/components/responses/422'
|
|
500:
|
|
$ref: '#/components/responses/500'
|
|
"""
|
|
# Reconstruct composite ID from publisher and name
|
|
composite_id = f"{publisher}.{name}"
|
|
extensions = get_extensions()
|
|
extension = extensions.get(composite_id)
|
|
if not extension:
|
|
return self.response_404()
|
|
extension_data = build_extension_data(extension)
|
|
return self.response(200, result=extension_data)
|
|
|
|
@protect()
|
|
@safe
|
|
@expose("/<publisher>/<name>/<file>", methods=("GET",))
|
|
def content(self, publisher: str, name: str, file: str) -> Response:
|
|
"""Get a frontend chunk of an extension.
|
|
---
|
|
get:
|
|
summary: Get a frontend chunk of an extension.
|
|
parameters:
|
|
- in: path
|
|
schema:
|
|
type: string
|
|
name: publisher
|
|
description: publisher of the extension
|
|
- in: path
|
|
schema:
|
|
type: string
|
|
name: name
|
|
description: technical name of the extension
|
|
- in: path
|
|
schema:
|
|
type: string
|
|
name: file
|
|
description: name of the requested chunk
|
|
responses:
|
|
200:
|
|
description: Extension import result
|
|
content:
|
|
application/json:
|
|
schema:
|
|
type: object
|
|
properties:
|
|
message:
|
|
type: string
|
|
400:
|
|
$ref: '#/components/responses/400'
|
|
401:
|
|
$ref: '#/components/responses/401'
|
|
422:
|
|
$ref: '#/components/responses/422'
|
|
500:
|
|
$ref: '#/components/responses/500'
|
|
"""
|
|
# Reconstruct composite ID from publisher and name
|
|
composite_id = f"{publisher}.{name}"
|
|
extensions = get_extensions()
|
|
extension = extensions.get(composite_id)
|
|
if not extension:
|
|
return self.response_404()
|
|
|
|
chunk = extension.frontend.get(file)
|
|
if not chunk:
|
|
return self.response_404()
|
|
|
|
mimetype, _ = mimetypes.guess_type(file)
|
|
if not mimetype:
|
|
mimetype = "application/octet-stream"
|
|
|
|
response = send_file(BytesIO(chunk), mimetype=mimetype)
|
|
# Chunk filenames include a content hash, so they are immutable.
|
|
response.cache_control.max_age = 31536000
|
|
response.cache_control.public = True
|
|
response.cache_control.immutable = True
|
|
return response
|