From 6cbad61572b25f1c1589c2c7f9433a02ba9a4d83 Mon Sep 17 00:00:00 2001 From: Franck Nijhof Date: Sun, 19 Feb 2023 05:05:44 +0100 Subject: [PATCH] Enable Ruff RUF006; Hard reference to asyncio.create_task return value (#88216) * Enable Ruff RUF006 * Fix test --------- Co-authored-by: Paulus Schoutsen --- pyproject.toml | 1 + tests/test_runner.py | 11 +++++++---- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index e0255bbe65b..d913ad0daaf 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -258,6 +258,7 @@ select = [ "SIM401", # Use get from dict with default instead of an if block "T20", # flake8-print "TRY004", # Prefer TypeError exception for invalid type + "RUF006", # Store a reference to the return value of asyncio.create_task "UP", # pyupgrade "W", # pycodestyle ] diff --git a/tests/test_runner.py b/tests/test_runner.py index 11a55031493..c5222cd026e 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -122,18 +122,21 @@ def test_run_does_not_block_forever_with_shielded_task(hass, tmpdir, caplog): async def test_unhandled_exception_traceback(hass, caplog): """Test an unhandled exception gets a traceback in debug mode.""" + raised = asyncio.Event() + async def _unhandled_exception(): + raised.set() raise Exception("This is unhandled") try: hass.loop.set_debug(True) - asyncio.create_task(_unhandled_exception()) + task = asyncio.create_task(_unhandled_exception()) + await raised.wait() + # Delete it without checking result to trigger unhandled exception + del task finally: hass.loop.set_debug(False) - await asyncio.sleep(0) - await asyncio.sleep(0) - assert "Task exception was never retrieved" in caplog.text assert "This is unhandled" in caplog.text assert "_unhandled_exception" in caplog.text