Files
superset2/superset/utils/database.py
Claude Code dfd3f7b316 ci(lint): enforce no function-body imports (PLC0415) with targeted ignores
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>
2026-05-20 13:55:14 -07:00

107 lines
3.7 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.
from __future__ import annotations
import logging
from typing import Any, TYPE_CHECKING
from flask import current_app as app
from sqlalchemy.sql import compiler
from superset.constants import EXAMPLES_DB_UUID
if TYPE_CHECKING:
from superset.connectors.sqla.models import Database
logging.getLogger("MARKDOWN").setLevel(logging.INFO)
logger = logging.getLogger(__name__)
# TODO: duplicate code with DatabaseDao, below function should be moved or use dao
def get_or_create_db(
database_name: str, sqlalchemy_uri: str, always_create: bool | None = True
) -> Database:
# pylint: disable=import-outside-toplevel
from superset import db # noqa: PLC0415
from superset.models import core as models # noqa: PLC0415
database = (
db.session.query(models.Database).filter_by(database_name=database_name).first()
)
# databases with a fixed UUID
uuids = {
"examples": EXAMPLES_DB_UUID,
}
if not database and always_create:
logger.info("Creating database reference for %s", database_name)
database = models.Database(
database_name=database_name, uuid=uuids.get(database_name)
)
db.session.add(database)
database.set_sqlalchemy_uri(sqlalchemy_uri)
# todo: it's a bad idea to do an update in a get/create function
if database and database.sqlalchemy_uri_decrypted != sqlalchemy_uri:
database.set_sqlalchemy_uri(sqlalchemy_uri)
db.session.flush()
return database
def get_example_database() -> Database:
# pylint: disable=import-outside-toplevel
return get_or_create_db("examples", app.config["SQLALCHEMY_EXAMPLES_URI"])
def get_main_database() -> Database:
# pylint: disable=import-outside-toplevel
db_uri = app.config["SQLALCHEMY_DATABASE_URI"]
return get_or_create_db("main", db_uri)
# TODO - the below method used by tests so should move there but should move together
# with above function... think of how to refactor it
def remove_database(database: Database) -> None:
# pylint: disable=import-outside-toplevel
from superset import db # noqa: PLC0415
db.session.delete(database)
db.session.flush()
def apply_mariadb_ddl_fix() -> None:
"""
Fix MariaDB "NO CYCLE" syntax issue - MariaDB uses "NOCYCLE" (no space).
This fix will be included in SQLAlchemy v2.1.0.
See: https://github.com/sqlalchemy/sqlalchemy/blob/rel_2_1_0b1/lib/sqlalchemy/dialects/mysql/_mariadb_shim.py
"""
original_visit_create_sequence = compiler.DDLCompiler.visit_create_sequence
def patched_visit_create_sequence(self: Any, create: Any, **kw: Any) -> str:
text = original_visit_create_sequence(self, create, **kw)
dialect_name = getattr(self.dialect, "name", "") or ""
if "mariadb" in dialect_name.lower():
return text.replace("NO CYCLE", "NOCYCLE")
return text
compiler.DDLCompiler.visit_create_sequence = patched_visit_create_sequence