Files
superset2/superset/semantic_layers/labels.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

108 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.
"""
Label helpers for SEMANTIC_LAYERS feature flag.
When the SEMANTIC_LAYERS feature flag is enabled the UI broadens
"dataset""datasource" and "database""data connection" so
that semantic views and semantic layers feel like first-class
citizens alongside traditional datasets and database connections.
Mirror of superset-frontend/src/features/semanticLayers/label.ts.
"""
from __future__ import annotations
from flask_babel import lazy_gettext as _
def _sl(legacy: str, semantic: str) -> str:
"""Return *semantic* when SEMANTIC_LAYERS is enabled, else *legacy*."""
# Imported lazily to avoid a circular import at module load time
# (superset.semantic_layers.labels is imported by superset.initialization,
# which is itself imported during superset package initialization).
from superset import ( # noqa: PLC0415
feature_flag_manager, # pylint: disable=import-outside-toplevel
)
return (
semantic
if feature_flag_manager.is_feature_enabled("SEMANTIC_LAYERS")
else legacy
)
# ---------------------------------------------------------------------------
# "dataset" family
# ---------------------------------------------------------------------------
def dataset_label() -> str:
"""Capitalized singular: "Dataset" / "Datasource" """
return _sl(_("Dataset"), _("Datasource"))
def dataset_label_lower() -> str:
"""Lower-case singular: "dataset" / "datasource" """
return _sl(_("dataset"), _("datasource"))
def datasets_label() -> str:
"""Capitalized plural: "Datasets" / "Datasources" """
return _sl(_("Datasets"), _("Datasources"))
def datasets_label_lower() -> str:
"""Lower-case plural: "datasets" / "datasources" """
return _sl(_("datasets"), _("datasources"))
# ---------------------------------------------------------------------------
# "database" family
# ---------------------------------------------------------------------------
def database_label() -> str:
"""Capitalized singular: "Database" / "Data connection" """
return _sl(_("Database"), _("Data connection"))
def database_label_lower() -> str:
"""Lower-case singular: "database" / "data connection" """
return _sl(_("database"), _("data connection"))
def databases_label() -> str:
"""Capitalized plural: "Databases" / "Data connections" """
return _sl(_("Databases"), _("Data connections"))
def databases_label_lower() -> str:
"""Lower-case plural: "databases" / "data connections" """
return _sl(_("databases"), _("data connections"))
# ---------------------------------------------------------------------------
# Menu label (includes the word "Connections")
# ---------------------------------------------------------------------------
def database_connections_menu_label() -> str:
"""Menu entry label: "Database Connections" / "Data Connections" """
return _sl(_("Database Connections"), _("Data Connections"))