mirror of
https://github.com/apache/superset.git
synced 2026-05-28 19:25:20 +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>
131 lines
4.6 KiB
Python
131 lines
4.6 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 datetime import datetime, timedelta
|
|
from typing import Any, Optional
|
|
from uuid import UUID, uuid3
|
|
|
|
from flask import current_app, Flask, has_app_context
|
|
from flask_caching import BaseCache
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
|
|
from superset import db
|
|
from superset.key_value.exceptions import KeyValueCreateFailedError
|
|
from superset.key_value.types import (
|
|
KeyValueCodec,
|
|
KeyValueResource,
|
|
PickleKeyValueCodec,
|
|
)
|
|
from superset.key_value.utils import get_uuid_namespace
|
|
from superset.utils.decorators import transaction
|
|
|
|
RESOURCE = KeyValueResource.METASTORE_CACHE
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class SupersetMetastoreCache(BaseCache):
|
|
def __init__(
|
|
self,
|
|
namespace: UUID,
|
|
codec: KeyValueCodec,
|
|
default_timeout: int = 300,
|
|
) -> None:
|
|
super().__init__(default_timeout)
|
|
self.namespace = namespace
|
|
self.codec = codec
|
|
|
|
@classmethod
|
|
def factory(
|
|
cls, app: Flask, config: dict[str, Any], args: list[Any], kwargs: dict[str, Any]
|
|
) -> BaseCache:
|
|
seed = config.get("CACHE_KEY_PREFIX", "")
|
|
kwargs["namespace"] = get_uuid_namespace(seed, app)
|
|
codec = config.get("CODEC") or PickleKeyValueCodec()
|
|
if (
|
|
has_app_context()
|
|
and not current_app.debug
|
|
and isinstance(codec, PickleKeyValueCodec)
|
|
):
|
|
logger.warning(
|
|
"Using PickleKeyValueCodec with SupersetMetastoreCache may be unsafe, "
|
|
"use at your own risk."
|
|
)
|
|
kwargs["codec"] = codec
|
|
return cls(*args, **kwargs)
|
|
|
|
def get_key(self, key: str) -> UUID:
|
|
return uuid3(self.namespace, key)
|
|
|
|
def _get_expiry(self, timeout: Optional[int]) -> Optional[datetime]:
|
|
timeout = self._normalize_timeout(timeout)
|
|
if timeout is not None and timeout > 0:
|
|
return datetime.now() + timedelta(seconds=timeout)
|
|
return None
|
|
|
|
def set(self, key: str, value: Any, timeout: Optional[int] = None) -> bool:
|
|
# pylint: disable=import-outside-toplevel
|
|
from superset.daos.key_value import KeyValueDAO # noqa: PLC0415
|
|
|
|
KeyValueDAO.upsert_entry(
|
|
resource=RESOURCE,
|
|
key=self.get_key(key),
|
|
value=value,
|
|
codec=self.codec,
|
|
expires_on=self._get_expiry(timeout),
|
|
)
|
|
db.session.commit() # pylint: disable=consider-using-transaction
|
|
return True
|
|
|
|
def add(self, key: str, value: Any, timeout: Optional[int] = None) -> bool:
|
|
# pylint: disable=import-outside-toplevel
|
|
from superset.daos.key_value import KeyValueDAO # noqa: PLC0415
|
|
|
|
try:
|
|
KeyValueDAO.delete_expired_entries(RESOURCE)
|
|
KeyValueDAO.create_entry(
|
|
resource=RESOURCE,
|
|
value=value,
|
|
codec=self.codec,
|
|
key=self.get_key(key),
|
|
expires_on=self._get_expiry(timeout),
|
|
)
|
|
db.session.commit() # pylint: disable=consider-using-transaction
|
|
return True
|
|
except (SQLAlchemyError, KeyValueCreateFailedError):
|
|
db.session.rollback() # pylint: disable=consider-using-transaction
|
|
return False
|
|
|
|
def get(self, key: str) -> Any:
|
|
# pylint: disable=import-outside-toplevel
|
|
from superset.daos.key_value import KeyValueDAO # noqa: PLC0415
|
|
|
|
return KeyValueDAO.get_value(RESOURCE, self.get_key(key), self.codec)
|
|
|
|
def has(self, key: str) -> bool:
|
|
entry = self.get(key)
|
|
if entry:
|
|
return True
|
|
return False
|
|
|
|
@transaction()
|
|
def delete(self, key: str) -> Any:
|
|
# pylint: disable=import-outside-toplevel
|
|
from superset.daos.key_value import KeyValueDAO # noqa: PLC0415
|
|
|
|
return KeyValueDAO.delete_entry(RESOURCE, self.get_key(key))
|