From 5f055a64bbd895e7c595e0e64bec9856c40ebae6 Mon Sep 17 00:00:00 2001 From: Sid <27780930+autinerd@users.noreply.github.com> Date: Mon, 15 Apr 2024 22:25:09 +0200 Subject: [PATCH] Enable Ruff B017 (#115335) --- pyproject.toml | 1 + tests/components/bluetooth/test_wrappers.py | 7 ++++--- tests/components/iaqualink/test_utils.py | 5 +++-- tests/components/repairs/test_init.py | 3 ++- tests/components/smartthings/test_init.py | 8 ++++---- tests/test_setup.py | 3 ++- 6 files changed, 16 insertions(+), 11 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 8701d67c930..3db19fe6851 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -668,6 +668,7 @@ select = [ "B007", # Loop control variable {name} not used within loop body "B014", # Exception handler with duplicate exception "B015", # Pointless comparison. Did you mean to assign a value? Otherwise, prepend assert or remove it. + "B017", # pytest.raises(BaseException) should be considered evil "B018", # Found useless attribute access. Either assign it to a variable or remove it. "B023", # Function definition does not bind loop variable {name} "B026", # Star-arg unpacking after a keyword argument is strongly discouraged diff --git a/tests/components/bluetooth/test_wrappers.py b/tests/components/bluetooth/test_wrappers.py index c14fb8a58c1..2acc2b0ddfc 100644 --- a/tests/components/bluetooth/test_wrappers.py +++ b/tests/components/bluetooth/test_wrappers.py @@ -107,7 +107,7 @@ class FakeBleakClientRaisesOnConnect(BaseFakeBleakClient): async def connect(self, *args, **kwargs): """Connect.""" - raise Exception("Test exception") + raise ConnectionError("Test exception") def _generate_ble_device_and_adv_data( @@ -304,8 +304,9 @@ async def test_release_slot_on_connect_exception( ): ble_device = hci0_device_advs["00:00:00:00:00:01"][0] client = bleak.BleakClient(ble_device) - with pytest.raises(Exception): - assert await client.connect() is False + with pytest.raises(ConnectionError) as exc_info: + await client.connect() + assert str(exc_info.value) == "Test exception" assert allocate_slot_mock.call_count == 1 assert release_slot_mock.call_count == 1 diff --git a/tests/components/iaqualink/test_utils.py b/tests/components/iaqualink/test_utils.py index c803fb48b09..b9aba93523c 100644 --- a/tests/components/iaqualink/test_utils.py +++ b/tests/components/iaqualink/test_utils.py @@ -15,9 +15,10 @@ async def test_await_or_reraise(hass: HomeAssistant) -> None: async_noop = async_returns(None) await await_or_reraise(async_noop()) - with pytest.raises(Exception): - async_ex = async_raises(Exception) + with pytest.raises(Exception) as exc_info: + async_ex = async_raises(Exception("Test exception")) await await_or_reraise(async_ex()) + assert str(exc_info.value) == "Test exception" with pytest.raises(HomeAssistantError): async_ex = async_raises(AqualinkServiceException) diff --git a/tests/components/repairs/test_init.py b/tests/components/repairs/test_init.py index ec34409eb74..75088f6c370 100644 --- a/tests/components/repairs/test_init.py +++ b/tests/components/repairs/test_init.py @@ -2,6 +2,7 @@ from unittest.mock import AsyncMock, Mock +from awesomeversion.exceptions import AwesomeVersionStrategyException from freezegun.api import FrozenDateTimeFactory import pytest @@ -145,7 +146,7 @@ async def test_create_issue_invalid_version( "translation_placeholders": {"abc": "123"}, } - with pytest.raises(Exception): + with pytest.raises(AwesomeVersionStrategyException): async_create_issue( hass, issue["domain"], diff --git a/tests/components/smartthings/test_init.py b/tests/components/smartthings/test_init.py index 6ff640e012a..ae8a288e3a5 100644 --- a/tests/components/smartthings/test_init.py +++ b/tests/components/smartthings/test_init.py @@ -370,9 +370,9 @@ async def test_remove_entry_installedapp_unknown_error( ) -> None: """Test raises exceptions removing the installed app.""" # Arrange - smartthings_mock.delete_installed_app.side_effect = Exception + smartthings_mock.delete_installed_app.side_effect = ValueError # Act - with pytest.raises(Exception): + with pytest.raises(ValueError): await smartthings.async_remove_entry(hass, config_entry) # Assert assert smartthings_mock.delete_installed_app.call_count == 1 @@ -403,9 +403,9 @@ async def test_remove_entry_app_unknown_error( ) -> None: """Test raises exceptions removing the app.""" # Arrange - smartthings_mock.delete_app.side_effect = Exception + smartthings_mock.delete_app.side_effect = ValueError # Act - with pytest.raises(Exception): + with pytest.raises(ValueError): await smartthings.async_remove_entry(hass, config_entry) # Assert assert smartthings_mock.delete_installed_app.call_count == 1 diff --git a/tests/test_setup.py b/tests/test_setup.py index e3d9a322862..65472643adb 100644 --- a/tests/test_setup.py +++ b/tests/test_setup.py @@ -346,8 +346,9 @@ async def test_component_base_exception_setup(hass: HomeAssistant) -> None: mock_integration(hass, MockModule("comp", setup=exception_setup)) - with pytest.raises(BaseException): + with pytest.raises(BaseException) as exc_info: await setup.async_setup_component(hass, "comp", {}) + assert str(exc_info.value) == "fail!" assert "comp" not in hass.config.components