Enable Ruff B017 (#115335)

This commit is contained in:
Sid 2024-04-15 22:25:09 +02:00 committed by GitHub
parent c7e6f3696f
commit 5f055a64bb
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 16 additions and 11 deletions

View file

@ -668,6 +668,7 @@ select = [
"B007", # Loop control variable {name} not used within loop body "B007", # Loop control variable {name} not used within loop body
"B014", # Exception handler with duplicate exception "B014", # Exception handler with duplicate exception
"B015", # Pointless comparison. Did you mean to assign a value? Otherwise, prepend assert or remove it. "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. "B018", # Found useless attribute access. Either assign it to a variable or remove it.
"B023", # Function definition does not bind loop variable {name} "B023", # Function definition does not bind loop variable {name}
"B026", # Star-arg unpacking after a keyword argument is strongly discouraged "B026", # Star-arg unpacking after a keyword argument is strongly discouraged

View file

@ -107,7 +107,7 @@ class FakeBleakClientRaisesOnConnect(BaseFakeBleakClient):
async def connect(self, *args, **kwargs): async def connect(self, *args, **kwargs):
"""Connect.""" """Connect."""
raise Exception("Test exception") raise ConnectionError("Test exception")
def _generate_ble_device_and_adv_data( 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] ble_device = hci0_device_advs["00:00:00:00:00:01"][0]
client = bleak.BleakClient(ble_device) client = bleak.BleakClient(ble_device)
with pytest.raises(Exception): with pytest.raises(ConnectionError) as exc_info:
assert await client.connect() is False await client.connect()
assert str(exc_info.value) == "Test exception"
assert allocate_slot_mock.call_count == 1 assert allocate_slot_mock.call_count == 1
assert release_slot_mock.call_count == 1 assert release_slot_mock.call_count == 1

View file

@ -15,9 +15,10 @@ async def test_await_or_reraise(hass: HomeAssistant) -> None:
async_noop = async_returns(None) async_noop = async_returns(None)
await await_or_reraise(async_noop()) await await_or_reraise(async_noop())
with pytest.raises(Exception): with pytest.raises(Exception) as exc_info:
async_ex = async_raises(Exception) async_ex = async_raises(Exception("Test exception"))
await await_or_reraise(async_ex()) await await_or_reraise(async_ex())
assert str(exc_info.value) == "Test exception"
with pytest.raises(HomeAssistantError): with pytest.raises(HomeAssistantError):
async_ex = async_raises(AqualinkServiceException) async_ex = async_raises(AqualinkServiceException)

View file

@ -2,6 +2,7 @@
from unittest.mock import AsyncMock, Mock from unittest.mock import AsyncMock, Mock
from awesomeversion.exceptions import AwesomeVersionStrategyException
from freezegun.api import FrozenDateTimeFactory from freezegun.api import FrozenDateTimeFactory
import pytest import pytest
@ -145,7 +146,7 @@ async def test_create_issue_invalid_version(
"translation_placeholders": {"abc": "123"}, "translation_placeholders": {"abc": "123"},
} }
with pytest.raises(Exception): with pytest.raises(AwesomeVersionStrategyException):
async_create_issue( async_create_issue(
hass, hass,
issue["domain"], issue["domain"],

View file

@ -370,9 +370,9 @@ async def test_remove_entry_installedapp_unknown_error(
) -> None: ) -> None:
"""Test raises exceptions removing the installed app.""" """Test raises exceptions removing the installed app."""
# Arrange # Arrange
smartthings_mock.delete_installed_app.side_effect = Exception smartthings_mock.delete_installed_app.side_effect = ValueError
# Act # Act
with pytest.raises(Exception): with pytest.raises(ValueError):
await smartthings.async_remove_entry(hass, config_entry) await smartthings.async_remove_entry(hass, config_entry)
# Assert # Assert
assert smartthings_mock.delete_installed_app.call_count == 1 assert smartthings_mock.delete_installed_app.call_count == 1
@ -403,9 +403,9 @@ async def test_remove_entry_app_unknown_error(
) -> None: ) -> None:
"""Test raises exceptions removing the app.""" """Test raises exceptions removing the app."""
# Arrange # Arrange
smartthings_mock.delete_app.side_effect = Exception smartthings_mock.delete_app.side_effect = ValueError
# Act # Act
with pytest.raises(Exception): with pytest.raises(ValueError):
await smartthings.async_remove_entry(hass, config_entry) await smartthings.async_remove_entry(hass, config_entry)
# Assert # Assert
assert smartthings_mock.delete_installed_app.call_count == 1 assert smartthings_mock.delete_installed_app.call_count == 1

View file

@ -346,8 +346,9 @@ async def test_component_base_exception_setup(hass: HomeAssistant) -> None:
mock_integration(hass, MockModule("comp", setup=exception_setup)) 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", {}) await setup.async_setup_component(hass, "comp", {})
assert str(exc_info.value) == "fail!"
assert "comp" not in hass.config.components assert "comp" not in hass.config.components