From 5345756b7148a7e0cdbe115f8d32c1e516af9301 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= <6774676+eumiro@users.noreply.github.com> Date: Fri, 25 Aug 2023 21:51:21 +0200 Subject: [PATCH] Refactor: Use inplace .sort() --- airflow/api_connexion/endpoints/task_endpoint.py | 2 +- airflow/dag_processing/manager.py | 4 ++-- airflow/timetables/events.py | 2 +- dev/validate_version_added_fields_in_config.py | 5 ++--- scripts/ci/pre_commit/pre_commit_sort_in_the_wild.py | 2 +- scripts/tools/generate-integrations-json.py | 2 +- tests/jobs/test_scheduler_job.py | 2 +- 7 files changed, 9 insertions(+), 10 deletions(-) diff --git a/airflow/api_connexion/endpoints/task_endpoint.py b/airflow/api_connexion/endpoints/task_endpoint.py index 23c2b32487b31..f4d48fe02d2fb 100644 --- a/airflow/api_connexion/endpoints/task_endpoint.py +++ b/airflow/api_connexion/endpoints/task_endpoint.py @@ -61,7 +61,7 @@ def get_tasks(*, dag_id: str, order_by: str = "task_id") -> APIResponse: tasks = dag.tasks try: - tasks = sorted(tasks, key=attrgetter(order_by.lstrip("-")), reverse=(order_by[0:1] == "-")) + tasks.sort(key=attrgetter(order_by.lstrip("-")), reverse=(order_by[0:1] == "-")) except AttributeError as err: raise BadRequest(detail=str(err)) task_collection = TaskCollection(tasks=tasks, total_entries=len(tasks)) diff --git a/airflow/dag_processing/manager.py b/airflow/dag_processing/manager.py index 5a92af893bd85..269ba769cbd67 100644 --- a/airflow/dag_processing/manager.py +++ b/airflow/dag_processing/manager.py @@ -865,7 +865,7 @@ def _log_file_processing_stats(self, known_file_paths): rows.append((file_path, processor_pid, runtime, num_dags, num_errors, last_runtime, last_run)) # Sort by longest last runtime. (Can't sort None values in python3) - rows = sorted(rows, key=lambda x: x[3] or 0.0) + rows.sort(key=lambda x: x[3] or 0.0) formatted_rows = [] for file_path, pid, runtime, num_dags, num_errors, last_runtime, last_run in rows: @@ -1167,7 +1167,7 @@ def prepare_file_path_queue(self): if is_mtime_mode: file_paths = sorted(files_with_mtime, key=files_with_mtime.get, reverse=True) elif list_mode == "alphabetical": - file_paths = sorted(file_paths) + file_paths.sort() elif list_mode == "random_seeded_by_host": # Shuffle the list seeded by hostname so multiple schedulers can work on different # set of files. Since we set the seed, the sort order will remain same per host diff --git a/airflow/timetables/events.py b/airflow/timetables/events.py index a59f4fc5b2077..ce8fa9527f787 100644 --- a/airflow/timetables/events.py +++ b/airflow/timetables/events.py @@ -52,7 +52,7 @@ def __init__( self.event_dates = list(event_dates) # Must be reversible and indexable if not presorted: # For long lists this could take a while, so only want to do it once - self.event_dates = sorted(self.event_dates) + self.event_dates.sort() self.restrict_to_events = restrict_to_events if description is None: self.description = ( diff --git a/dev/validate_version_added_fields_in_config.py b/dev/validate_version_added_fields_in_config.py index d68ce36a0de3a..7ee0fb16b2dcb 100755 --- a/dev/validate_version_added_fields_in_config.py +++ b/dev/validate_version_added_fields_in_config.py @@ -114,9 +114,8 @@ def read_local_config_options() -> set[tuple[str, str, str]]: computed_option_new_section.update(options) # 1. Prepare versions to checks -airflow_version = fetch_pypi_versions() -airflow_version = sorted(airflow_version, key=semver.VersionInfo.parse) -to_check_versions: list[str] = [d for d in airflow_version if d.startswith("2.")] +to_check_versions: list[str] = [d for d in fetch_pypi_versions() if d.startswith("2.")] +to_check_versions.sort(key=semver.VersionInfo.parse) # 2. Compute expected options set with version added fields expected_computed_options: set[tuple[str, str, str]] = set() diff --git a/scripts/ci/pre_commit/pre_commit_sort_in_the_wild.py b/scripts/ci/pre_commit/pre_commit_sort_in_the_wild.py index a04d7d18d01d1..cfe2047236be9 100755 --- a/scripts/ci/pre_commit/pre_commit_sort_in_the_wild.py +++ b/scripts/ci/pre_commit/pre_commit_sort_in_the_wild.py @@ -68,5 +68,5 @@ def stable_sort(x): line = "1." + line.split(".", maxsplit=1)[1] print(f"{old_line.strip()} => {line.strip()}") companies.append(line) - companies = sorted(companies, key=stable_sort) + companies.sort(key=stable_sort) inthewild_path.write_text("".join(header) + "\n" + "".join(companies)) diff --git a/scripts/tools/generate-integrations-json.py b/scripts/tools/generate-integrations-json.py index 94725da1df1a0..e66c28d9c0c72 100755 --- a/scripts/tools/generate-integrations-json.py +++ b/scripts/tools/generate-integrations-json.py @@ -67,7 +67,7 @@ result["logo"] = logo result_integrations.append(result) -result_integrations = sorted(result_integrations, key=lambda x: x["name"].lower()) +result_integrations.sort(key=lambda x: x["name"].lower()) with open(os.path.join(AIRFLOW_SITE_DIR, "landing-pages/site/static/integrations.json"), "w") as f: f.write( json.dumps( diff --git a/tests/jobs/test_scheduler_job.py b/tests/jobs/test_scheduler_job.py index d1612a84f3cab..4d65626dda30a 100644 --- a/tests/jobs/test_scheduler_job.py +++ b/tests/jobs/test_scheduler_job.py @@ -601,7 +601,7 @@ def test_find_executable_task_instances_pool(self, dag_maker): dr2.get_task_instance(task_id_1, session=session), dr2.get_task_instance(task_id_2, session=session), ] - tis = sorted(tis, key=lambda ti: ti.key) + tis.sort(key=lambda ti: ti.key) for ti in tis: ti.state = State.SCHEDULED session.merge(ti)