Skip to content

Commit

Permalink
Refactor: Simplify code in scripts (#33295)
Browse files Browse the repository at this point in the history
  • Loading branch information
eumiro authored Aug 13, 2023
1 parent de780e0 commit 50a6385
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 35 deletions.
2 changes: 1 addition & 1 deletion kubernetes_tests/test_kubernetes_pod_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -927,7 +927,7 @@ def test_pod_template_file(
" creation_timestamp: null",
" deletion_grace_period_seconds: null",
]
actual = [x.getMessage() for x in caplog.records if x.msg == "Starting pod:\n%s"][0].splitlines()
actual = next(x.getMessage() for x in caplog.records if x.msg == "Starting pod:\n%s").splitlines()
assert actual[: len(expected_lines)] == expected_lines

actual_pod = self.api_client.sanitize_for_serialization(k.pod)
Expand Down
5 changes: 1 addition & 4 deletions scripts/ci/pre_commit/pre_commit_check_pre_commit_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,7 @@ def render_template(

def update_static_checks_array(hooks: dict[str, list[str]], image_hooks: list[str]):
rows = []
hook_ids = list(hooks.keys())
hook_ids.sort()
for hook_id in hook_ids:
hook_description = hooks[hook_id]
for hook_id, hook_description in sorted(hooks.items()):
formatted_hook_description = (
hook_description[0] if len(hook_description) == 1 else "* " + "\n* ".join(hook_description)
)
Expand Down
2 changes: 1 addition & 1 deletion scripts/ci/pre_commit/pre_commit_json_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def load_file(file_path: str):
if file_path.lower().endswith(".json"):
with open(file_path) as input_file:
return json.load(input_file)
elif file_path.lower().endswith(".yaml") or file_path.lower().endswith(".yml"):
elif file_path.lower().endswith((".yaml", ".yml")):
with open(file_path) as input_file:
return yaml.safe_load(input_file)
raise _ValidatorError("Unknown file format. Supported extension: '.yaml', '.json'")
Expand Down
22 changes: 4 additions & 18 deletions scripts/ci/pre_commit/pre_commit_update_common_sql_api_stubs.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,25 +69,11 @@ def summarize_changes(results: list[str]) -> tuple[int, int]:
"""
removals, additions = 0, 0
for line in results:
if (
line.startswith("+")
or line.startswith("[green]+")
and not (
# Skip additions of comments in counting removals
line.startswith("+#")
or line.startswith("[green]+#")
)
):
if line.startswith(("+", "[green]+")) and not line.startswith(("+#", "[green]+#")):
# Skip additions of comments in counting removals
additions += 1
if (
line.startswith("-")
or line.startswith("[red]-")
and not (
# Skip removals of comments in counting removals
line.startswith("-#")
or line.startswith("[red]-#")
)
):
if line.startswith(("-", "[red]+")) and not line.startswith(("-#", "[red]+#")):
# Skip removals of comments in counting removals
removals += 1
return removals, additions

Expand Down
5 changes: 2 additions & 3 deletions scripts/ci/pre_commit/pre_commit_update_example_dags_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,10 @@ def get_provider_and_version(url_path: str) -> tuple[str, str]:
provider_info = yaml.safe_load(f)
version = provider_info["versions"][0]
provider = "-".join(candidate_folders)
while provider.endswith("-"):
provider = provider[:-1]
provider = provider.rstrip("-")
return provider, version
except FileNotFoundError:
candidate_folders = candidate_folders[:-1]
candidate_folders.pop()
console.print(
f"[red]Bad example path: {url_path}. Missing "
f"provider.yaml in any of the 'airflow/providers/{url_path}' folders. [/]"
Expand Down
4 changes: 2 additions & 2 deletions scripts/in_container/run_migration_reference.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,6 @@ def ensure_filenames_are_sorted(revisions):
revisions = list(reversed(list(get_revisions())))
ensure_airflow_version(revisions=revisions)
revisions = list(reversed(list(get_revisions())))
ensure_filenames_are_sorted(revisions)
ensure_filenames_are_sorted(revisions=revisions)
revisions = list(get_revisions())
update_docs(revisions)
update_docs(revisions=revisions)
5 changes: 2 additions & 3 deletions scripts/in_container/update_quarantined_test_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class TestHistory(NamedTuple):
":x:": False,
}

reverse_status_map: dict[bool, str] = {status_map[key]: key for key in status_map.keys()}
reverse_status_map: dict[bool, str] = {val: key for key, val in status_map.items()}


def get_url(result: TestResult) -> str:
Expand Down Expand Up @@ -160,8 +160,7 @@ def get_history_status(history: TestHistory):
def get_table(history_map: dict[str, TestHistory]) -> str:
headers = ["Test", "Last run", f"Last {num_runs} runs", "Status", "Comment"]
the_table: list[list[str]] = []
for ordered_key in sorted(history_map.keys()):
history = history_map[ordered_key]
for _, history in sorted(history_map.items()):
the_table.append(
[
history.url,
Expand Down
5 changes: 2 additions & 3 deletions scripts/in_container/verify_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def onerror(_):

for path, prefix in walkable_paths_and_prefixes.items():
for modinfo in pkgutil.walk_packages(path=[path], prefix=prefix, onerror=onerror):
if not any(modinfo.name.startswith(provider_prefix) for provider_prefix in provider_prefixes):
if not modinfo.name.startswith(tuple(provider_prefixes)):
if print_skips:
console.print(f"Skipping module: {modinfo.name}")
continue
Expand Down Expand Up @@ -332,8 +332,7 @@ def get_details_about_classes(
:param wrong_entities: wrong entities found for that type
:param full_package_name: full package name
"""
all_entities = list(entities)
all_entities.sort()
all_entities = sorted(entities)
TOTALS[entity_type] += len(all_entities)
return EntityTypeSummary(
entities=all_entities,
Expand Down

0 comments on commit 50a6385

Please sign in to comment.