Skip to content

Commit

Permalink
[Refactor] Fix ruff rule B018: useless-expression
Browse files Browse the repository at this point in the history
Signed-off-by: win5923 <[email protected]>
  • Loading branch information
win5923 committed Jan 22, 2025
1 parent a918028 commit 19c83d3
Show file tree
Hide file tree
Showing 33 changed files with 56 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def py_func_pass_python_actor_handle():

@ray.remote
def py_func_python_raise_exception():
1 / 0
raise ZeroDivisionError


@ray.remote
Expand Down
1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ ignore = [
# The above are rules ignored originally in flake8
# The following are rules ignored in ruff
"F841",
"B018",
"B023",
"B024",
"B026",
Expand Down
8 changes: 4 additions & 4 deletions python/ray/dag/tests/experimental/test_accelerated_dag.py
Original file line number Diff line number Diff line change
Expand Up @@ -2538,14 +2538,14 @@ async def main():
match=(expected_error_message),
):
_ = await async_compiled_dag.execute_async(1)
(ref1, ref2)
(ref1, ref2) # noqa: B018

loop = get_or_create_event_loop()
loop.run_until_complete(main())
# to show variables are being used and avoid destruction since
# CompiledDagRef __del__ will release buffers and
# increment _max_finished_execution_index
(ref1, ref2)
(ref1, ref2) # noqa: B018


def test_result_buffer_exceeds_capacity(ray_start_regular):
Expand Down Expand Up @@ -2586,12 +2586,12 @@ async def main():
match=(expected_error_message),
):
_ = await async_compiled_dag.execute_async(4)
(ref1, ref3)
(ref1, ref3) # noqa: B018

loop = get_or_create_event_loop()
loop.run_until_complete(main())
# same reason as comment for test_inflight_requests_exceed_capacity
(ref1, ref3)
(ref1, ref3) # noqa: B018


def test_event_profiling(ray_start_regular, monkeypatch):
Expand Down
6 changes: 2 additions & 4 deletions python/ray/data/tests/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ def test_user_exception(
ctx.log_internal_stack_trace_to_stdout = log_internal_stack_trace_to_stdout

def f(row):
1 / 0
return row
raise ZeroDivisionError

with pytest.raises(UserCodeException) as exc_info:
ray.data.range(1).map(f).take_all()
Expand Down Expand Up @@ -80,8 +79,7 @@ def test_full_traceback_logged_with_ray_debugger(
monkeypatch.setenv("RAY_DEBUG_POST_MORTEM", 1)

def f(row):
1 / 0
return row
raise ZeroDivisionError

with pytest.raises(Exception) as exc_info:
ray.data.range(1).map(f).take_all()
Expand Down
1 change: 0 additions & 1 deletion python/ray/data/tests/test_execution_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ def get_read_tasks(self, parallelism: int) -> List[ReadTask]:
large_object = np.zeros((128, 1024, 1024), dtype=np.uint8) # 128 MiB

def read_fn():
large_object
yield pd.DataFrame({"column": [0]})

return [ReadTask(read_fn, BlockMetadata(1, None, None, None, None))]
Expand Down
2 changes: 1 addition & 1 deletion python/ray/serve/_private/benchmarks/proxy_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ async def fetch_http(session, data):

async def fetch_grpc(stub, data):
result = await stub.grpc_call(serve_pb2.RawData(nums=data))
result.output
_ = result.output


@ray.remote
Expand Down
2 changes: 1 addition & 1 deletion python/ray/serve/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ def test_status_constructor_error(serve_instance):
@serve.deployment
class A:
def __init__(self):
1 / 0
raise ZeroDivisionError

serve._run(A.bind(), _blocking=False)

Expand Down
2 changes: 1 addition & 1 deletion python/ray/serve/tests/test_config_files/fail.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
@serve.deployment
class A:
def __init__(self):
1 / 0
raise ZeroDivisionError


node = A.bind()
2 changes: 1 addition & 1 deletion python/ray/serve/tests/test_config_files/fail_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class A:
def __init__(self):
time.sleep(5)
1 / 0
raise ZeroDivisionError


node = A.bind()
2 changes: 1 addition & 1 deletion python/ray/serve/tests/test_config_files/import_error.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from ray import serve

1 / 0
raise ZeroDivisionError


@serve.deployment(ray_actor_options={"num_cpus": 0.1})
Expand Down
2 changes: 1 addition & 1 deletion python/ray/serve/tests/test_fastapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ def test_fastapi_init_lifespan_should_not_shutdown(serve_instance):

@app.on_event("shutdown")
async def shutdown():
1 / 0
raise ZeroDivisionError

@serve.deployment
@serve.ingress(app)
Expand Down
2 changes: 1 addition & 1 deletion python/ray/serve/tests/test_http_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def redirect_twice(self, request: Request):
def test_default_error_handling(serve_instance):
@serve.deployment
def f():
1 / 0
raise ZeroDivisionError

serve.run(f.bind())
r = requests.get("http://localhost:8000/f")
Expand Down
2 changes: 1 addition & 1 deletion python/ray/serve/tests/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -937,7 +937,7 @@ def test_stream_to_logger():

# Calling non-existing attribute on the StreamToLogger should still raise error.
with pytest.raises(AttributeError):
stream_to_logger.i_dont_exist
_ = stream_to_logger.i_dont_exist


@pytest.mark.skipif(sys.platform == "win32", reason="Fail to create temp dir.")
Expand Down
4 changes: 2 additions & 2 deletions python/ray/serve/tests/test_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ def test_proxy_metrics_fields_not_found(serve_start_shutdown):

# Should generate 404 responses
broken_url = "http://127.0.0.1:8000/fake_route"
requests.get(broken_url).text
requests.get(broken_url)
print("Sent requests to broken URL.")

# Ping gRPC proxy for not existing application.
Expand Down Expand Up @@ -540,7 +540,7 @@ def f(*args):

# Deployment should generate divide-by-zero errors
correct_url = "http://127.0.0.1:8000/real_route"
requests.get(correct_url).text
requests.get(correct_url)
print("Sent requests to correct URL.")

# Ping gPRC proxy for broken app
Expand Down
12 changes: 6 additions & 6 deletions python/ray/serve/tests/unit/test_batching.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,14 @@ async def test_batch_size_one_long_timeout(use_class):
@serve.batch(max_batch_size=1, batch_wait_timeout_s=1000)
async def long_timeout(requests):
if "raise" in requests:
1 / 0
raise ZeroDivisionError
return requests

class LongTimeout:
@serve.batch(max_batch_size=1, batch_wait_timeout_s=1000)
async def long_timeout(self, requests):
if "raise" in requests:
1 / 0
raise ZeroDivisionError
return requests

cls = LongTimeout()
Expand All @@ -158,15 +158,15 @@ async def test_batch_size_multiple_zero_timeout(use_class):
async def zero_timeout(requests):
await block_execution_event.wait()
if "raise" in requests:
1 / 0
raise ZeroDivisionError
return requests

class ZeroTimeout:
@serve.batch(max_batch_size=2, batch_wait_timeout_s=0)
async def zero_timeout(self, requests):
await block_execution_event.wait()
if "raise" in requests:
1 / 0
raise ZeroDivisionError
return requests

cls = ZeroTimeout()
Expand Down Expand Up @@ -262,14 +262,14 @@ async def test_batch_size_multiple_long_timeout(use_class):
@serve.batch(max_batch_size=3, batch_wait_timeout_s=1000)
async def long_timeout(requests):
if "raise" in requests:
1 / 0
raise ZeroDivisionError
return requests

class LongTimeout:
@serve.batch(max_batch_size=3, batch_wait_timeout_s=1000)
async def long_timeout(self, requests):
if "raise" in requests:
1 / 0
raise ZeroDivisionError
return requests

cls = LongTimeout()
Expand Down
4 changes: 2 additions & 2 deletions python/ray/serve/tests/unit/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -609,14 +609,14 @@ def test_grpc_options():
grpc_servicer_functions = ["fake.service.that.does.not.exist"]
with pytest.raises(ModuleNotFoundError) as exception:
grpc_options = gRPCOptions(grpc_servicer_functions=grpc_servicer_functions)
grpc_options.grpc_servicer_func_callable
_ = grpc_options.grpc_servicer_func_callable
assert "can't be imported!" in str(exception)

# Not callable should raise ValueError.
grpc_servicer_functions = ["ray.serve._private.constants.DEFAULT_HTTP_PORT"]
with pytest.raises(ValueError) as exception:
grpc_options = gRPCOptions(grpc_servicer_functions=grpc_servicer_functions)
grpc_options.grpc_servicer_func_callable
_ = grpc_options.grpc_servicer_func_callable
assert "is not a callable function!" in str(exception)


Expand Down
2 changes: 1 addition & 1 deletion python/ray/serve/tests/unit/test_deployment_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ def test_placement_groups(self):
),
)

ray.util.placement_group
_ = ray.util.placement_group
scheduler.on_deployment_created(d_id1, SpreadDeploymentSchedulingPolicy())
scheduler.on_deployment_created(d_id2, SpreadDeploymentSchedulingPolicy())

Expand Down
10 changes: 5 additions & 5 deletions python/ray/serve/tests/unit/test_grpc_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ def test_dummy_servicer_can_take_any_methods():
When dummy_servicer is called with any custom defined methods, it won't raise error.
"""
dummy_servicer = DummyServicer()
dummy_servicer.foo
dummy_servicer.bar
dummy_servicer.baz
dummy_servicer.my_method
dummy_servicer.Predict
_ = dummy_servicer.foo
_ = dummy_servicer.bar
_ = dummy_servicer.baz
_ = dummy_servicer.my_method
_ = dummy_servicer.Predict


def test_grpc_server():
Expand Down
2 changes: 1 addition & 1 deletion python/ray/tests/accelerators/test_accelerators.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_accelerators():
AttributeError,
match="module 'ray.util.accelerators' has no attribute 'NVIDIA_INVALID'",
):
accelerators.NVIDIA_INVALID
_ = accelerators.NVIDIA_INVALID
with pytest.warns(RayDeprecationWarning):
assert accelerators.NVIDIA_TESLA_A100 == "A100"

Expand Down
6 changes: 3 additions & 3 deletions python/ray/tests/accelerators/test_amd_gpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def test_visible_amd_gpu_ids(mock_get_num_accelerators, monkeypatch, shutdown_on
# we call get_accelerator_manager_for_resource
del get_accelerator_manager_for_resource._resource_name_to_accelerator_manager
ray.init()
mock_get_num_accelerators.called
_ = mock_get_num_accelerators.called
assert ray.available_resources()["GPU"] == 3


Expand All @@ -28,7 +28,7 @@ def test_visible_amd_gpu_ids(mock_get_num_accelerators, monkeypatch, shutdown_on
)
def test_visible_amd_gpu_type(mock_get_amd_device_ids, shutdown_only):
ray.init()
mock_get_amd_device_ids.called
_ = mock_get_amd_device_ids.called
assert (
AMDGPUAcceleratorManager.get_current_node_accelerator_type()
== "AMD-Instinct-MI300X-OAM"
Expand All @@ -41,7 +41,7 @@ def test_visible_amd_gpu_type(mock_get_amd_device_ids, shutdown_only):
)
def test_visible_amd_gpu_type_bad_device_id(mock_get_num_accelerators, shutdown_only):
ray.init()
mock_get_num_accelerators.called
_ = mock_get_num_accelerators.called
assert AMDGPUAcceleratorManager.get_current_node_accelerator_type() is None


Expand Down
4 changes: 2 additions & 2 deletions python/ray/tests/accelerators/test_hpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_auto_detected_more_than_visible(
# Test more hpus are detected than visible.
monkeypatch.setenv("HABANA_VISIBLE_MODULES", "0,1,2")
ray.init()
mock_get_num_accelerators.called
_ = mock_get_num_accelerators.called
assert ray.available_resources()["HPU"] == 3


Expand All @@ -37,7 +37,7 @@ def test_auto_detected_more_than_visible(
def test_auto_detect_resources(mock_get_num_accelerators, shutdown_only):
# Test that ray node resources are filled with auto detected count.
ray.init()
mock_get_num_accelerators.called
_ = mock_get_num_accelerators.called
assert ray.available_resources()["HPU"] == 2


Expand Down
4 changes: 2 additions & 2 deletions python/ray/tests/accelerators/test_neuron.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_auto_detected_more_than_visible(
# Test more neuron_cores are detected than visible.
monkeypatch.setenv("NEURON_RT_VISIBLE_CORES", "0,1,2")
ray.init()
mock_get_num_accelerators.called
_ = mock_get_num_accelerators.called
assert ray.available_resources()["neuron_cores"] == 3


Expand All @@ -36,7 +36,7 @@ def test_auto_detected_more_than_visible(
def test_auto_detect_resources(mock_get_num_accelerators, shutdown_only):
# Test that ray node resources are filled with auto detected count.
ray.init()
mock_get_num_accelerators.called
_ = mock_get_num_accelerators.called
assert ray.available_resources()["neuron_cores"] == 2


Expand Down
4 changes: 2 additions & 2 deletions python/ray/tests/test_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def task():

@ray.remote
def task_throws():
1 / 0
raise ZeroDivisionError

with pytest.raises(ray.exceptions.RayTaskError):
await task_throws.remote().as_future()
Expand All @@ -148,7 +148,7 @@ def big_object(self):
return "a" * (str_len)

def throw_error(self):
1 / 0
raise ZeroDivisionError

actor = Actor.remote()

Expand Down
2 changes: 1 addition & 1 deletion python/ray/tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ def __init__(self):
self.val = ray.put(0)

def method(self):
f
f # noqa : B018

f = Foo()
ray.put(f)
Expand Down
4 changes: 2 additions & 2 deletions python/ray/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ def test_ray_status(shutdown_only, monkeypatch, enable_v2):

def output_ready():
result = runner.invoke(scripts.status)
result.stdout
_ = result.stdout
if not result.exception and "memory" in result.output:
return True
raise RuntimeError(
Expand Down Expand Up @@ -967,7 +967,7 @@ def test_ray_status_multinode(ray_start_cluster, enable_v2):

def output_ready():
result = runner.invoke(scripts.status)
result.stdout
_ = result.stdout
if not result.exception and "memory" in result.output:
return True
raise RuntimeError(
Expand Down
2 changes: 1 addition & 1 deletion python/ray/tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ def child_func(self):
assert ray.get(handle.child_func.remote()) == 42
with pytest.raises(AttributeError):
# We should raise attribute error when accessing a non-existent func
SomeClass.nonexistent_func
_ = SomeClass.nonexistent_func


def test_serialize_client_actor_handle(call_ray_start_shared):
Expand Down
Loading

0 comments on commit 19c83d3

Please sign in to comment.