Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 17.7k
Enable ruff B008 (function-call-in-default-argument) and fix violations#66979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
175294113323d381b1a4bd011b835b855ef62c2be04cc68575858903bec9beaFile filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
jscheffl marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| # | ||
| # 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 | ||
| from unittest import mock | ||
| import pytest | ||
| pytest.importorskip("google.cloud.aiplatform.vertex_ray.util.resources") | ||
| from google.cloud.aiplatform.vertex_ray.util.resources import Resources | ||
| from airflow.providers.google.cloud.operators.vertex_ai.ray import CreateRayClusterOperator | ||
| TEST_GCP_CONN_ID = "test-gcp-conn-id" | ||
| TEST_LOCATION = "us-central1" | ||
| TEST_PROJECT_ID = "test-project-id" | ||
| TEST_PYTHON_VERSION = "3.10" | ||
| TEST_RAY_VERSION = "2.33" | ||
| TEST_CLUSTER_NAME = "test-cluster-name" | ||
| VERTEX_AI_RAY_OP_PATH = "airflow.providers.google.cloud.operators.vertex_ai.ray.{}" | ||
| class TestCreateRayClusterOperator: | ||
| @mock.patch(VERTEX_AI_RAY_OP_PATH.format("RayHook")) | ||
| def test_create_ray_cluster_with_explicit_head_node_type(self, mock_hook_cls): | ||
| explicit_head = Resources() | ||
| op = CreateRayClusterOperator( | ||
| task_id="test-task", | ||
| project_id=TEST_PROJECT_ID, | ||
| location=TEST_LOCATION, | ||
| python_version=TEST_PYTHON_VERSION, | ||
| ray_version=TEST_RAY_VERSION, | ||
| head_node_type=explicit_head, | ||
| gcp_conn_id=TEST_GCP_CONN_ID, | ||
| ) | ||
| assert op.head_node_type is explicit_head | ||
| @mock.patch(VERTEX_AI_RAY_OP_PATH.format("RayHook")) | ||
| def test_create_ray_cluster_default_head_node_type_is_fresh_resources(self, mock_hook_cls): | ||
| op1 = CreateRayClusterOperator( | ||
| task_id="test-task-1", | ||
| project_id=TEST_PROJECT_ID, | ||
| location=TEST_LOCATION, | ||
| python_version=TEST_PYTHON_VERSION, | ||
| ray_version=TEST_RAY_VERSION, | ||
| gcp_conn_id=TEST_GCP_CONN_ID, | ||
| ) | ||
| op2 = CreateRayClusterOperator( | ||
| task_id="test-task-2", | ||
| project_id=TEST_PROJECT_ID, | ||
| location=TEST_LOCATION, | ||
| python_version=TEST_PYTHON_VERSION, | ||
| ray_version=TEST_RAY_VERSION, | ||
| gcp_conn_id=TEST_GCP_CONN_ID, | ||
| ) | ||
| assert isinstance(op1.head_node_type, Resources) | ||
| assert isinstance(op2.head_node_type, Resources) | ||
| assert op1.head_node_type is not op2.head_node_type | ||
| @mock.patch(VERTEX_AI_RAY_OP_PATH.format("VertexAIRayClusterLink")) | ||
| @mock.patch(VERTEX_AI_RAY_OP_PATH.format("RayHook")) | ||
| def test_execute_without_head_node_type_passes_default_resources(self, mock_hook_cls, mock_link): | ||
| mock_hook = mock_hook_cls.return_value | ||
| mock_hook.create_ray_cluster.return_value = ( | ||
| f"projects/{TEST_PROJECT_ID}/locations/{TEST_LOCATION}/persistentResources/{TEST_CLUSTER_NAME}" | ||
| ) | ||
| mock_hook.extract_cluster_id.return_value = TEST_CLUSTER_NAME | ||
| op = CreateRayClusterOperator( | ||
| task_id="test-task", | ||
| project_id=TEST_PROJECT_ID, | ||
| location=TEST_LOCATION, | ||
| python_version=TEST_PYTHON_VERSION, | ||
| ray_version=TEST_RAY_VERSION, | ||
| gcp_conn_id=TEST_GCP_CONN_ID, | ||
| ) | ||
| ti_mock = mock.MagicMock() | ||
| context = {"ti": ti_mock, "task": mock.MagicMock()} | ||
| op.execute(context=context) | ||
| call_kwargs = mock_hook.create_ray_cluster.call_args.kwargs | ||
| assert isinstance(call_kwargs["head_node_type"], Resources) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -655,6 +655,7 @@ extend-select = [ | ||
| "B004", # Checks for use of hasattr(x, "__call__") and replaces it with callable(x) | ||
| "B006", # Checks for uses of mutable objects as function argument defaults. | ||
| "B007", # Checks for unused variables in the loop | ||
| "B008", # Do not perform function call in argument defaults (use extend-immutable-calls for FastAPI DI) | ||
shahar1 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| "B012", # Checks for `break`, `continue`, and `return` statements in `finally` blocks | ||
| "B017", # Checks for pytest.raises context managers that catch Exception or BaseException. | ||
| "B019", # Use of functools.lru_cache or functools.cache on methods can lead to memory leaks | ||
| @@ -703,6 +704,18 @@ unfixable = [ | ||
| "PT022", | ||
| ] | ||
| [tool.ruff.lint.flake8-bugbear] | ||
jscheffl marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| # FastAPI dependency injection uses function calls in argument defaults intentionally. | ||
| # SHA256 is a stateless algorithm descriptor (cryptography library). | ||
| extend-immutable-calls = [ | ||
| "fastapi.Body", | ||
| "fastapi.Depends", | ||
| "fastapi.Query", | ||
| "fastapi.Path", | ||
| "fastapi.Security", | ||
| "cryptography.hazmat.primitives.hashes.SHA256", | ||
| ] | ||
| [tool.ruff.format] | ||
| docstring-code-format = true | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -116,7 +116,7 @@ def __init__( | ||
| self, | ||
| *, | ||
| poke_interval: timedelta | float = 60, | ||
| timeout: timedelta | float = conf.getfloat("sensors", "default_timeout"), | ||
| timeout: timedelta | float | None = None, | ||
| soft_fail: bool = False, | ||
| mode: str = "poke", | ||
| exponential_backoff: bool = False, | ||
| @@ -128,6 +128,8 @@ def __init__( | ||
| super().__init__(**kwargs) | ||
| self.poke_interval = self._coerce_poke_interval(poke_interval).total_seconds() | ||
| self.soft_fail = soft_fail | ||
| if timeout is None: | ||
| timeout = conf.getfloat("sensors", "default_timeout") | ||
shahar1 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| self.timeout: int | float = self._coerce_timeout(timeout).total_seconds() | ||
| self.mode = mode | ||
| self.exponential_backoff = exponential_backoff | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -125,15 +125,15 @@ class Resources: | ||
| def __init__( | ||
| self, | ||
| cpus=conf.getint("operators", "default_cpus"), | ||
| ram=conf.getint("operators", "default_ram"), | ||
| disk=conf.getint("operators", "default_disk"), | ||
| gpus=conf.getint("operators", "default_gpus"), | ||
| cpus=None, | ||
| ram=None, | ||
| disk=None, | ||
| gpus=None, | ||
| ): | ||
| self.cpus = CpuResource(cpus) | ||
| self.ram = RamResource(ram) | ||
| self.disk = DiskResource(disk) | ||
| self.gpus = GpuResource(gpus) | ||
| self.cpus = CpuResource(cpus if cpus is not None else conf.getint("operators", "default_cpus")) | ||
| self.ram = RamResource(ram if ram is not None else conf.getint("operators", "default_ram")) | ||
| self.disk = DiskResource(disk if disk is not None else conf.getint("operators", "default_disk")) | ||
| self.gpus = GpuResource(gpus if gpus is not None else conf.getint("operators", "default_gpus")) | ||
shahar1 marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| def __eq__(self, other: object) -> bool: | ||
| if not isinstance(other, self.__class__): | ||
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.