From 49b28cca621b12fd7f3c636241f96a53719f1632 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Thu, 13 Jun 2024 16:59:40 +0200 Subject: [PATCH] Fix consider-using-with warnings in core tests (#119606) --- tests/test_block_async_io.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/tests/test_block_async_io.py b/tests/test_block_async_io.py index d823f8c6912..20089cf15b9 100644 --- a/tests/test_block_async_io.py +++ b/tests/test_block_async_io.py @@ -212,8 +212,11 @@ async def test_protect_loop_importlib_import_module_in_integration( async def test_protect_loop_open(caplog: pytest.LogCaptureFixture) -> None: """Test open of a file in /proc is not reported.""" block_async_io.enable() - with contextlib.suppress(FileNotFoundError): - open("/proc/does_not_exist", encoding="utf8").close() + with ( + contextlib.suppress(FileNotFoundError), + open("/proc/does_not_exist", encoding="utf8"), + ): + pass assert "Detected blocking call to open with args" not in caplog.text @@ -221,8 +224,11 @@ async def test_protect_open(caplog: pytest.LogCaptureFixture) -> None: """Test opening a file in the event loop logs.""" with patch.object(block_async_io, "_IN_TESTS", False): block_async_io.enable() - with contextlib.suppress(FileNotFoundError): - open("/config/data_not_exist", encoding="utf8").close() + with ( + contextlib.suppress(FileNotFoundError), + open("/config/data_not_exist", encoding="utf8"), + ): + pass assert "Detected blocking call to open with args" in caplog.text @@ -250,8 +256,8 @@ async def test_protect_open_path(path: Any, caplog: pytest.LogCaptureFixture) -> """Test opening a file by path in the event loop logs.""" with patch.object(block_async_io, "_IN_TESTS", False): block_async_io.enable() - with contextlib.suppress(FileNotFoundError): - open(path, encoding="utf8").close() + with contextlib.suppress(FileNotFoundError), open(path, encoding="utf8"): + pass assert "Detected blocking call to open with args" in caplog.text @@ -331,7 +337,10 @@ async def test_open_calls_ignored_in_tests(caplog: pytest.LogCaptureFixture) -> """Test opening a file in tests is ignored.""" assert block_async_io._IN_TESTS block_async_io.enable() - with contextlib.suppress(FileNotFoundError): - open("/config/data_not_exist", encoding="utf8").close() + with ( + contextlib.suppress(FileNotFoundError), + open("/config/data_not_exist", encoding="utf8"), + ): + pass assert "Detected blocking call to open with args" not in caplog.text