mirror of
https://github.com/apache/superset.git
synced 2026-05-24 01:05:21 +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>
209 lines
6.7 KiB
Python
209 lines
6.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 json
|
|
import os
|
|
from pathlib import Path
|
|
|
|
import pytest
|
|
import tomli_w
|
|
from click.testing import CliRunner
|
|
|
|
|
|
@pytest.fixture
|
|
def cli_runner():
|
|
"""Provide a Click CLI runner for testing commands."""
|
|
return CliRunner()
|
|
|
|
|
|
@pytest.fixture
|
|
def isolated_filesystem(tmp_path):
|
|
"""
|
|
Provide an isolated temporary directory and change to it.
|
|
This ensures tests don't interfere with each other.
|
|
"""
|
|
original_cwd = Path.cwd()
|
|
os.chdir(tmp_path)
|
|
yield tmp_path
|
|
os.chdir(original_cwd)
|
|
|
|
|
|
@pytest.fixture
|
|
def extension_params():
|
|
"""Default parameters for extension creation."""
|
|
return {
|
|
"publisher": "test-org",
|
|
"name": "test-extension",
|
|
"displayName": "Test Extension",
|
|
"version": "0.1.0",
|
|
"license": "Apache-2.0",
|
|
"include_frontend": True,
|
|
"include_backend": True,
|
|
}
|
|
|
|
|
|
@pytest.fixture
|
|
def cli_input_both():
|
|
"""CLI input for creating extension with both frontend and backend."""
|
|
return "Test Extension\n\ntest-org\n0.1.0\nApache-2.0\ny\ny\n"
|
|
|
|
|
|
@pytest.fixture
|
|
def cli_input_frontend_only():
|
|
"""CLI input for creating extension with frontend only."""
|
|
return "Test Extension\n\ntest-org\n0.1.0\nApache-2.0\ny\nn\n"
|
|
|
|
|
|
@pytest.fixture
|
|
def cli_input_backend_only():
|
|
"""CLI input for creating extension with backend only."""
|
|
return "Test Extension\n\ntest-org\n0.1.0\nApache-2.0\nn\ny\n"
|
|
|
|
|
|
@pytest.fixture
|
|
def cli_input_neither():
|
|
"""CLI input for creating extension with neither frontend nor backend."""
|
|
return "Test Extension\n\ntest-org\n0.1.0\nApache-2.0\nn\nn\n"
|
|
|
|
|
|
@pytest.fixture
|
|
def extension_setup_for_dev():
|
|
"""Set up extension structure for dev testing."""
|
|
|
|
def _setup(base_path: Path) -> None:
|
|
import json # noqa: PLC0415
|
|
|
|
# Create extension.json with new structure
|
|
extension_json = {
|
|
"publisher": "test-org",
|
|
"name": "test-extension",
|
|
"displayName": "Test Extension",
|
|
"version": "1.0.0",
|
|
"permissions": [],
|
|
}
|
|
(base_path / "extension.json").write_text(json.dumps(extension_json))
|
|
|
|
# Create frontend and backend directories
|
|
(base_path / "frontend").mkdir()
|
|
(base_path / "backend").mkdir()
|
|
|
|
return _setup
|
|
|
|
|
|
@pytest.fixture
|
|
def extension_setup_for_bundling():
|
|
"""Set up a complete extension structure ready for bundling."""
|
|
|
|
def _setup(base_path: Path) -> None:
|
|
import json # noqa: PLC0415
|
|
|
|
# Create dist directory with manifest and files
|
|
dist_dir = base_path / "dist"
|
|
dist_dir.mkdir(parents=True)
|
|
|
|
# Create manifest.json with composite ID
|
|
manifest = {
|
|
"id": "test-org.test-extension",
|
|
"publisher": "test-org",
|
|
"name": "test-extension",
|
|
"displayName": "Test Extension",
|
|
"version": "1.0.0",
|
|
"permissions": [],
|
|
}
|
|
(dist_dir / "manifest.json").write_text(json.dumps(manifest))
|
|
|
|
# Create some frontend files
|
|
frontend_dir = dist_dir / "frontend" / "dist"
|
|
frontend_dir.mkdir(parents=True)
|
|
(frontend_dir / "remoteEntry.abc123.js").write_text("// remote entry")
|
|
(frontend_dir / "main.js").write_text("// main js")
|
|
|
|
# Create some backend files - updated path structure
|
|
backend_dir = dist_dir / "backend" / "src" / "test_org" / "test_extension"
|
|
backend_dir.mkdir(parents=True)
|
|
(backend_dir / "__init__.py").write_text("# init")
|
|
|
|
return _setup
|
|
|
|
|
|
@pytest.fixture
|
|
def extension_with_versions():
|
|
"""Create an extension directory structure with configurable versions and licenses."""
|
|
|
|
def _create(
|
|
base_path: Path,
|
|
ext_version: str = "1.0.0",
|
|
frontend_version: str | None = None,
|
|
backend_version: str | None = None,
|
|
ext_license: str | None = "Apache-2.0",
|
|
frontend_license: str | None = None,
|
|
backend_license: str | None = None,
|
|
) -> None:
|
|
extension_json = {
|
|
"publisher": "test-org",
|
|
"name": "test-extension",
|
|
"displayName": "Test Extension",
|
|
"version": ext_version,
|
|
"permissions": [],
|
|
}
|
|
if ext_license is not None:
|
|
extension_json["license"] = ext_license
|
|
(base_path / "extension.json").write_text(json.dumps(extension_json))
|
|
|
|
if frontend_version is not None:
|
|
frontend_dir = base_path / "frontend"
|
|
frontend_dir.mkdir(exist_ok=True)
|
|
(frontend_dir / "src").mkdir(exist_ok=True)
|
|
(frontend_dir / "src" / "index.tsx").write_text("// entry")
|
|
pkg = {
|
|
"name": "@test-org/test-extension",
|
|
"version": frontend_version,
|
|
}
|
|
if frontend_license is not None:
|
|
pkg["license"] = frontend_license
|
|
elif ext_license is not None:
|
|
pkg["license"] = ext_license
|
|
(frontend_dir / "package.json").write_text(json.dumps(pkg, indent=2))
|
|
|
|
if backend_version is not None:
|
|
backend_dir = base_path / "backend"
|
|
backend_dir.mkdir(exist_ok=True)
|
|
src_dir = backend_dir / "src" / "test_org" / "test_extension"
|
|
src_dir.mkdir(parents=True, exist_ok=True)
|
|
(src_dir / "entrypoint.py").write_text("# entry")
|
|
project = {
|
|
"name": "test-org-test-extension",
|
|
"version": backend_version,
|
|
}
|
|
if backend_license is not None:
|
|
project["license"] = backend_license
|
|
elif ext_license is not None:
|
|
project["license"] = ext_license
|
|
pyproject = {
|
|
"project": project,
|
|
"tool": {
|
|
"apache_superset_extensions": {
|
|
"build": {"include": ["src/**/*.py"]}
|
|
}
|
|
},
|
|
}
|
|
(backend_dir / "pyproject.toml").write_text(tomli_w.dumps(pyproject))
|
|
|
|
return _create
|