mirror of
https://github.com/apache/superset.git
synced 2026-05-07 17:04:58 +00:00
chore(pre-commit): Add pyupgrade and pycln hooks (#24197)
This commit is contained in:
@@ -23,7 +23,7 @@ from graphlib import TopologicalSorter
|
||||
from inspect import getsource
|
||||
from pathlib import Path
|
||||
from types import ModuleType
|
||||
from typing import Any, Dict, List, Set, Type
|
||||
from typing import Any
|
||||
|
||||
import click
|
||||
from flask import current_app
|
||||
@@ -48,12 +48,10 @@ def import_migration_script(filepath: Path) -> ModuleType:
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(module) # type: ignore
|
||||
return module
|
||||
raise Exception(
|
||||
"No module spec found in location: `{path}`".format(path=str(filepath))
|
||||
)
|
||||
raise Exception(f"No module spec found in location: `{str(filepath)}`")
|
||||
|
||||
|
||||
def extract_modified_tables(module: ModuleType) -> Set[str]:
|
||||
def extract_modified_tables(module: ModuleType) -> set[str]:
|
||||
"""
|
||||
Extract the tables being modified by a migration script.
|
||||
|
||||
@@ -62,7 +60,7 @@ def extract_modified_tables(module: ModuleType) -> Set[str]:
|
||||
actually traversing the AST.
|
||||
"""
|
||||
|
||||
tables: Set[str] = set()
|
||||
tables: set[str] = set()
|
||||
for function in {"upgrade", "downgrade"}:
|
||||
source = getsource(getattr(module, function))
|
||||
tables.update(re.findall(r'alter_table\(\s*"(\w+?)"\s*\)', source, re.DOTALL))
|
||||
@@ -72,11 +70,11 @@ def extract_modified_tables(module: ModuleType) -> Set[str]:
|
||||
return tables
|
||||
|
||||
|
||||
def find_models(module: ModuleType) -> List[Type[Model]]:
|
||||
def find_models(module: ModuleType) -> list[type[Model]]:
|
||||
"""
|
||||
Find all models in a migration script.
|
||||
"""
|
||||
models: List[Type[Model]] = []
|
||||
models: list[type[Model]] = []
|
||||
tables = extract_modified_tables(module)
|
||||
|
||||
# add models defined explicitly in the migration script
|
||||
@@ -123,7 +121,7 @@ def find_models(module: ModuleType) -> List[Type[Model]]:
|
||||
sorter: TopologicalSorter[Any] = TopologicalSorter()
|
||||
for model in models:
|
||||
inspector = inspect(model)
|
||||
dependent_tables: List[str] = []
|
||||
dependent_tables: list[str] = []
|
||||
for column in inspector.columns.values():
|
||||
for foreign_key in column.foreign_keys:
|
||||
if foreign_key.column.table.name != model.__tablename__:
|
||||
@@ -174,7 +172,7 @@ def main(
|
||||
|
||||
print("\nIdentifying models used in the migration:")
|
||||
models = find_models(module)
|
||||
model_rows: Dict[Type[Model], int] = {}
|
||||
model_rows: dict[type[Model], int] = {}
|
||||
for model in models:
|
||||
rows = session.query(model).count()
|
||||
print(f"- {model.__name__} ({rows} rows in table {model.__tablename__})")
|
||||
@@ -182,7 +180,7 @@ def main(
|
||||
session.close()
|
||||
|
||||
print("Benchmarking migration")
|
||||
results: Dict[str, float] = {}
|
||||
results: dict[str, float] = {}
|
||||
start = time.time()
|
||||
upgrade(revision=revision)
|
||||
duration = time.time() - start
|
||||
@@ -190,14 +188,14 @@ def main(
|
||||
print(f"Migration on current DB took: {duration:.2f} seconds")
|
||||
|
||||
min_entities = 10
|
||||
new_models: Dict[Type[Model], List[Model]] = defaultdict(list)
|
||||
new_models: dict[type[Model], list[Model]] = defaultdict(list)
|
||||
while min_entities <= limit:
|
||||
downgrade(revision=down_revision)
|
||||
print(f"Running with at least {min_entities} entities of each model")
|
||||
for model in models:
|
||||
missing = min_entities - model_rows[model]
|
||||
if missing > 0:
|
||||
entities: List[Model] = []
|
||||
entities: list[Model] = []
|
||||
print(f"- Adding {missing} entities to the {model.__name__} model")
|
||||
bar = ChargingBar("Processing", max=missing)
|
||||
try:
|
||||
|
||||
@@ -33,13 +33,13 @@ Example:
|
||||
./cancel_github_workflows.py 1024 --include-last
|
||||
"""
|
||||
import os
|
||||
from typing import Any, Dict, Iterable, Iterator, List, Optional, Union
|
||||
from collections.abc import Iterable, Iterator
|
||||
from typing import Any, Literal, Optional, Union
|
||||
|
||||
import click
|
||||
import requests
|
||||
from click.exceptions import ClickException
|
||||
from dateutil import parser
|
||||
from typing_extensions import Literal
|
||||
|
||||
github_token = os.environ.get("GITHUB_TOKEN")
|
||||
github_repo = os.environ.get("GITHUB_REPOSITORY", "apache/superset")
|
||||
@@ -47,7 +47,7 @@ github_repo = os.environ.get("GITHUB_REPOSITORY", "apache/superset")
|
||||
|
||||
def request(
|
||||
method: Literal["GET", "POST", "DELETE", "PUT"], endpoint: str, **kwargs: Any
|
||||
) -> Dict[str, Any]:
|
||||
) -> dict[str, Any]:
|
||||
resp = requests.request(
|
||||
method,
|
||||
f"https://api.github.com/{endpoint.lstrip('/')}",
|
||||
@@ -61,8 +61,8 @@ def request(
|
||||
|
||||
def list_runs(
|
||||
repo: str,
|
||||
params: Optional[Dict[str, str]] = None,
|
||||
) -> Iterator[Dict[str, Any]]:
|
||||
params: Optional[dict[str, str]] = None,
|
||||
) -> Iterator[dict[str, Any]]:
|
||||
"""List all github workflow runs.
|
||||
Returns:
|
||||
An iterator that will iterate through all pages of matching runs."""
|
||||
@@ -77,16 +77,15 @@ def list_runs(
|
||||
params={**params, "per_page": 100, "page": page},
|
||||
)
|
||||
total_count = result["total_count"]
|
||||
for item in result["workflow_runs"]:
|
||||
yield item
|
||||
yield from result["workflow_runs"]
|
||||
page += 1
|
||||
|
||||
|
||||
def cancel_run(repo: str, run_id: Union[str, int]) -> Dict[str, Any]:
|
||||
def cancel_run(repo: str, run_id: Union[str, int]) -> dict[str, Any]:
|
||||
return request("POST", f"/repos/{repo}/actions/runs/{run_id}/cancel")
|
||||
|
||||
|
||||
def get_pull_request(repo: str, pull_number: Union[str, int]) -> Dict[str, Any]:
|
||||
def get_pull_request(repo: str, pull_number: Union[str, int]) -> dict[str, Any]:
|
||||
return request("GET", f"/repos/{repo}/pulls/{pull_number}")
|
||||
|
||||
|
||||
@@ -96,7 +95,7 @@ def get_runs(
|
||||
user: Optional[str] = None,
|
||||
statuses: Iterable[str] = ("queued", "in_progress"),
|
||||
events: Iterable[str] = ("pull_request", "push"),
|
||||
) -> List[Dict[str, Any]]:
|
||||
) -> list[dict[str, Any]]:
|
||||
"""Get workflow runs associated with the given branch"""
|
||||
return [
|
||||
item
|
||||
@@ -108,7 +107,7 @@ def get_runs(
|
||||
]
|
||||
|
||||
|
||||
def print_commit(commit: Dict[str, Any], branch: str) -> None:
|
||||
def print_commit(commit: dict[str, Any], branch: str) -> None:
|
||||
"""Print out commit message for verification"""
|
||||
indented_message = " \n".join(commit["message"].split("\n"))
|
||||
date_str = (
|
||||
@@ -155,7 +154,7 @@ Date: {date_str}
|
||||
def cancel_github_workflows(
|
||||
branch_or_pull: Optional[str],
|
||||
repo: str,
|
||||
event: List[str],
|
||||
event: list[str],
|
||||
include_last: bool,
|
||||
include_running: bool,
|
||||
) -> None:
|
||||
|
||||
@@ -24,7 +24,7 @@ def cleanup_permissions() -> None:
|
||||
pvms = security_manager.get_session.query(
|
||||
security_manager.permissionview_model
|
||||
).all()
|
||||
print("# of permission view menus is: {}".format(len(pvms)))
|
||||
print(f"# of permission view menus is: {len(pvms)}")
|
||||
pvms_dict = defaultdict(list)
|
||||
for pvm in pvms:
|
||||
pvms_dict[(pvm.permission, pvm.view_menu)].append(pvm)
|
||||
@@ -43,7 +43,7 @@ def cleanup_permissions() -> None:
|
||||
pvms = security_manager.get_session.query(
|
||||
security_manager.permissionview_model
|
||||
).all()
|
||||
print("Stage 1: # of permission view menus is: {}".format(len(pvms)))
|
||||
print(f"Stage 1: # of permission view menus is: {len(pvms)}")
|
||||
|
||||
# 2. Clean up None permissions or view menus
|
||||
pvms = security_manager.get_session.query(
|
||||
@@ -57,7 +57,7 @@ def cleanup_permissions() -> None:
|
||||
pvms = security_manager.get_session.query(
|
||||
security_manager.permissionview_model
|
||||
).all()
|
||||
print("Stage 2: # of permission view menus is: {}".format(len(pvms)))
|
||||
print(f"Stage 2: # of permission view menus is: {len(pvms)}")
|
||||
|
||||
# 3. Delete empty permission view menus from roles
|
||||
roles = security_manager.get_session.query(security_manager.role_model).all()
|
||||
|
||||
Reference in New Issue
Block a user