mirror of
https://github.com/apache/superset.git
synced 2026-05-30 12:49:17 +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>
121 lines
4.3 KiB
Python
121 lines
4.3 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 logging
|
|
from functools import partial
|
|
from typing import Optional
|
|
|
|
from superset.commands.base import BaseCommand
|
|
from superset.commands.theme.exceptions import (
|
|
SystemThemeInUseError,
|
|
SystemThemeProtectedError,
|
|
ThemeDeleteFailedError,
|
|
ThemeNotFoundError,
|
|
)
|
|
from superset.daos.theme import ThemeDAO
|
|
from superset.extensions import db
|
|
from superset.models.core import Theme
|
|
from superset.utils.decorators import on_error, transaction
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class DeleteThemeCommand(BaseCommand):
|
|
def __init__(self, model_ids: list[int]):
|
|
self._model_ids = model_ids
|
|
self._models: Optional[list[Theme]] = None
|
|
self._dashboard_usage: Optional[dict[int, list[str]]] = None
|
|
|
|
@transaction(on_error=partial(on_error, reraise=ThemeDeleteFailedError))
|
|
def run(self) -> None:
|
|
self.validate()
|
|
assert self._models
|
|
|
|
# Dissociate dashboards from themes before deleting
|
|
self._dissociate_dashboards()
|
|
|
|
ThemeDAO.delete(self._models)
|
|
|
|
def validate(self) -> None:
|
|
# Validate/populate model exists
|
|
self._models = ThemeDAO.find_by_ids(self._model_ids)
|
|
if not self._models or len(self._models) != len(self._model_ids):
|
|
raise ThemeNotFoundError()
|
|
|
|
# Check if any of the themes are system themes
|
|
for theme in self._models:
|
|
if theme.is_system:
|
|
raise SystemThemeProtectedError()
|
|
# Check if theme is in use as system default or dark
|
|
if theme.is_system_default or theme.is_system_dark:
|
|
raise SystemThemeInUseError()
|
|
|
|
# Check for dashboard usage
|
|
self._dashboard_usage = self._get_dashboard_usage()
|
|
|
|
def _dissociate_dashboards(self) -> None:
|
|
"""Dissociate dashboards from themes before deletion."""
|
|
from superset.models.dashboard import Dashboard # noqa: PLC0415
|
|
|
|
theme_ids = [theme.id for theme in self._models or []]
|
|
if not theme_ids:
|
|
return
|
|
|
|
# Get count of affected dashboards for logging
|
|
affected_count = (
|
|
db.session.query(Dashboard)
|
|
.filter(Dashboard.theme_id.in_(theme_ids))
|
|
.count()
|
|
)
|
|
|
|
if affected_count > 0:
|
|
logger.info(
|
|
"Dissociating %d dashboards from %d themes before deletion",
|
|
affected_count,
|
|
len(theme_ids),
|
|
)
|
|
|
|
# Set theme_id to NULL for all dashboards using these themes
|
|
db.session.query(Dashboard).filter(
|
|
Dashboard.theme_id.in_(theme_ids)
|
|
).update({Dashboard.theme_id: None}, synchronize_session=False)
|
|
|
|
def _get_dashboard_usage(self) -> dict[int, list[str]]:
|
|
"""Get dashboard names that use these themes."""
|
|
from superset.models.dashboard import Dashboard # noqa: PLC0415
|
|
|
|
theme_ids = [theme.id for theme in self._models or []]
|
|
if not theme_ids:
|
|
return {}
|
|
|
|
dashboards_using_themes = (
|
|
db.session.query(Dashboard.theme_id, Dashboard.dashboard_title)
|
|
.filter(Dashboard.theme_id.in_(theme_ids))
|
|
.all()
|
|
)
|
|
|
|
usage: dict[int, list[str]] = {}
|
|
for theme_id, dashboard_title in dashboards_using_themes:
|
|
if theme_id not in usage:
|
|
usage[theme_id] = []
|
|
usage[theme_id].append(dashboard_title)
|
|
|
|
return usage
|
|
|
|
def get_dashboard_usage(self) -> dict[int, list[str]]:
|
|
"""Public method to get dashboard usage info."""
|
|
return self._dashboard_usage or {}
|