Skip to content

Commit

Permalink
Do not create lists we don't need (#33519)
Browse files Browse the repository at this point in the history
  • Loading branch information
eumiro committed Aug 24, 2023
1 parent 9dac487 commit 4154cc0
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 36 deletions.
4 changes: 2 additions & 2 deletions airflow/cli/commands/webserver_command.py
Expand Up @@ -144,8 +144,8 @@ def ready_prefix_on_cmdline(proc):
pass
return False

ready_workers = [proc for proc in workers if ready_prefix_on_cmdline(proc)]
return len(ready_workers)
nb_ready_workers = sum(1 for proc in workers if ready_prefix_on_cmdline(proc))
return nb_ready_workers

def _get_num_workers_running(self) -> int:
"""Return number of running Gunicorn workers processes."""
Expand Down
10 changes: 6 additions & 4 deletions airflow/providers/databricks/hooks/databricks_sql.py
Expand Up @@ -91,10 +91,12 @@ def _get_sql_endpoint_by_name(self, endpoint_name) -> dict[str, Any]:
result = self._do_api_call(LIST_SQL_ENDPOINTS_ENDPOINT)
if "endpoints" not in result:
raise AirflowException("Can't list Databricks SQL endpoints")
lst = [endpoint for endpoint in result["endpoints"] if endpoint["name"] == endpoint_name]
if not lst:
raise AirflowException(f"Can't f Databricks SQL endpoint with name '{endpoint_name}'")
return lst[0]
try:
endpoint = next(endpoint for endpoint in result["endpoints"] if endpoint["name"] == endpoint_name)
except StopIteration:
raise AirflowException(f"Can't find Databricks SQL endpoint with name '{endpoint_name}'")
else:
return endpoint

def get_conn(self) -> Connection:
"""Returns a Databricks SQL connection object."""
Expand Down
2 changes: 1 addition & 1 deletion airflow/providers/google/cloud/operators/datafusion.py
Expand Up @@ -44,7 +44,7 @@ class DataFusionPipelineLinkHelper:
@staticmethod
def get_project_id(instance):
instance = instance["name"]
project_id = [x for x in instance.split("/") if x.startswith("airflow")][0]
project_id = next(x for x in instance.split("/") if x.startswith("airflow"))
return project_id


Expand Down
36 changes: 16 additions & 20 deletions dev/breeze/src/airflow_breeze/commands/production_image_commands.py
Expand Up @@ -435,26 +435,22 @@ def check_docker_context_files(install_packages_from_context: bool):
:param install_packages_from_context: whether we want to install from docker-context-files
"""
context_file = DOCKER_CONTEXT_DIR.glob("**/*")
number_of_context_files = len(
[context for context in context_file if context.is_file() and context.name != ".README.md"]
)
if number_of_context_files == 0:
if install_packages_from_context:
get_console().print("[warning]\nERROR! You want to install packages from docker-context-files")
get_console().print("[warning]\n but there are no packages to install in this folder.")
sys.exit(1)
else:
if not install_packages_from_context:
get_console().print(
"[warning]\n ERROR! There are some extra files in docker-context-files except README.md"
)
get_console().print("[warning]\nAnd you did not choose --install-packages-from-context flag")
get_console().print(
"[warning]\nThis might result in unnecessary cache invalidation and long build times"
)
get_console().print("[warning]Please restart the command with --cleanup-context switch\n")
sys.exit(1)
context_file = DOCKER_CONTEXT_DIR.rglob("*")
any_context_files = any(context.is_file() and context.name != ".README.md" for context in context_file)
if not any_context_files and install_packages_from_context:
get_console().print("[warning]\nERROR! You want to install packages from docker-context-files")
get_console().print("[warning]\n but there are no packages to install in this folder.")
sys.exit(1)
elif any_context_files and not install_packages_from_context:
get_console().print(
"[warning]\n ERROR! There are some extra files in docker-context-files except README.md"
)
get_console().print("[warning]\nAnd you did not choose --install-packages-from-context flag")
get_console().print(
"[warning]\nThis might result in unnecessary cache invalidation and long build times"
)
get_console().print("[warning]Please restart the command with --cleanup-context switch\n")
sys.exit(1)


def run_build_production_image(
Expand Down
12 changes: 6 additions & 6 deletions scripts/ci/pre_commit/pre_commit_unittest_testcase.py
Expand Up @@ -29,18 +29,18 @@ def check_test_file(file: str) -> int:
classes = [c for c in node.body if isinstance(c, ast.ClassDef)]
for c in classes:
# Some classes are returned as an ast.Attribute, some as an ast.Name object. Not quite sure why
parent_classes = [base.attr for base in c.bases if isinstance(base, ast.Attribute)]
parent_classes.extend([base.id for base in c.bases if isinstance(base, ast.Name)])

if "TestCase" in parent_classes:
if any(
(isinstance(base, ast.Attribute) and base.attr == "TestCase")
or (isinstance(base, ast.Name) and base.id == "TestCase")
for base in c.bases
):
found += 1
print(f"The class {c.name} inherits from TestCase, please use pytest instead")

return found


def main(*args: str) -> int:
return sum([check_test_file(file) for file in args[1:]])
return sum(check_test_file(file) for file in args[1:])


if __name__ == "__main__":
Expand Down
4 changes: 2 additions & 2 deletions tests/core/test_configuration.py
Expand Up @@ -1565,7 +1565,7 @@ def test_get_sections_including_defaults(self):
all_sections_including_defaults = airflow_cfg.get_sections_including_defaults()
assert "core" in all_sections_including_defaults
assert "test-section" in all_sections_including_defaults
assert len([section for section in all_sections_including_defaults if section == "core"]) == 1
assert sum(1 for section in all_sections_including_defaults if section == "core") == 1

def test_get_options_including_defaults(self):
airflow_cfg = AirflowConfigParser()
Expand All @@ -1589,7 +1589,7 @@ def test_get_options_including_defaults(self):
assert "dags_folder" in all_core_options_including_defaults
assert "test-value" == airflow_cfg.get("core", "new-test-key")
assert "test-runner" == airflow_cfg.get("core", "task_runner")
assert len([option for option in all_core_options_including_defaults if option == "task_runner"]) == 1
assert sum(1 for option in all_core_options_including_defaults if option == "task_runner") == 1


def test_sensitive_values():
Expand Down
2 changes: 1 addition & 1 deletion tests/decorators/test_setup_teardown.py
Expand Up @@ -255,7 +255,7 @@ def mytask2():
mytask2()

assert len(dag.task_group.children) == 6
assert [x for x in dag.tasks if not x.downstream_list] # no deps have been set
assert sum(1 for x in dag.tasks if not x.downstream_list) == 6
assert dag.task_group.children["setuptask"].is_setup
assert dag.task_group.children["teardowntask"].is_teardown
assert dag.task_group.children["setuptask2"].is_setup
Expand Down

0 comments on commit 4154cc0

Please sign in to comment.