Skip to content

Commit

Permalink
Replace pylint broad-exception-raised rule with ruff (#123021)
Browse files Browse the repository at this point in the history
  • Loading branch information
epenet authored Aug 2, 2024
1 parent 5446dd9 commit ad26db7
Show file tree
Hide file tree
Showing 19 changed files with 26 additions and 35 deletions.
3 changes: 1 addition & 2 deletions homeassistant/components/fritz/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,7 @@ async def async_scan_devices(self, now: datetime | None = None) -> None:
self.fritz_hosts.get_mesh_topology
)
):
# pylint: disable-next=broad-exception-raised
raise Exception("Mesh supported but empty topology reported")
raise Exception("Mesh supported but empty topology reported") # noqa: TRY002
except FritzActionError:
self.mesh_role = MeshRoles.SLAVE
# Avoid duplicating device trackers
Expand Down
3 changes: 1 addition & 2 deletions homeassistant/components/starline/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,7 @@ async def _async_authenticate_user(self, error=None):
self._captcha_image = data["captchaImg"]
return self._async_form_auth_captcha(error)

# pylint: disable=broad-exception-raised
raise Exception(data)
raise Exception(data) # noqa: TRY002
except Exception as err: # noqa: BLE001
_LOGGER.error("Error auth user: %s", err)
return self._async_form_auth_user(ERROR_AUTH_USER)
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ disable = [
"no-else-return", # RET505
"broad-except", # BLE001
"protected-access", # SLF001
"broad-exception-raised", # TRY002
# "no-self-use", # PLR6301 # Optional plugin, not enabled

# Handled by mypy
Expand Down Expand Up @@ -823,7 +824,6 @@ ignore = [
"PYI024", # Use typing.NamedTuple instead of collections.namedtuple
"RET503",
"RET501",
"TRY002",
"TRY301"
]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def get_suggested(schema, key):
return None
return k.description["suggested_value"]
# Wanted key absent from schema
raise Exception
raise KeyError(f"Key `{key}` is missing from schema")


@pytest.mark.parametrize("platform", ["sensor"])
Expand Down
6 changes: 2 additions & 4 deletions tests/components/bluetooth/test_passive_update_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,8 +583,7 @@ def _async_generate_mock_data(
nonlocal run_count
run_count += 1
if run_count == 2:
# pylint: disable-next=broad-exception-raised
raise Exception("Test exception")
raise Exception("Test exception") # noqa: TRY002
return GENERIC_PASSIVE_BLUETOOTH_DATA_UPDATE

coordinator = PassiveBluetoothProcessorCoordinator(
Expand Down Expand Up @@ -1418,8 +1417,7 @@ def _mock_update_method(
nonlocal run_count
run_count += 1
if run_count == 2:
# pylint: disable-next=broad-exception-raised
raise Exception("Test exception")
raise Exception("Test exception") # noqa: TRY002
return {"test": "data"}

@callback
Expand Down
2 changes: 1 addition & 1 deletion tests/components/notify/test_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ async def test_platform_setup_with_error(

async def async_get_service(hass, config, discovery_info=None):
"""Return None for an invalid notify service."""
raise Exception("Setup error") # pylint: disable=broad-exception-raised
raise Exception("Setup error") # noqa: TRY002

mock_notify_platform(
hass, tmp_path, "testnotify", async_get_service=async_get_service
Expand Down
2 changes: 1 addition & 1 deletion tests/components/profiler/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def __init__(self, fail):

def __repr__(self):
if self.fail:
raise Exception("failed") # pylint: disable=broad-exception-raised
raise Exception("failed") # noqa: TRY002
return "<DumpLogDummy success>"

obj1 = DumpLogDummy(False)
Expand Down
2 changes: 1 addition & 1 deletion tests/components/roon/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class RoonApiMockException(RoonApiMock):
@property
def token(self):
"""Throw exception."""
raise Exception # pylint: disable=broad-exception-raised
raise Exception # noqa: TRY002


class RoonDiscoveryMock:
Expand Down
2 changes: 1 addition & 1 deletion tests/components/stt/test_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async def async_get_engine(
discovery_info: DiscoveryInfoType | None = None,
) -> Provider:
"""Raise exception during platform setup."""
raise Exception("Setup error") # pylint: disable=broad-exception-raised
raise Exception("Setup error") # noqa: TRY002

mock_stt_platform(hass, tmp_path, "bad_stt", async_get_engine=async_get_engine)

Expand Down
2 changes: 1 addition & 1 deletion tests/components/system_health/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ async def test_info_endpoint_register_callback_exc(
"""Test that the info endpoint requires auth."""

async def mock_info(hass):
raise Exception("TEST ERROR") # pylint: disable=broad-exception-raised
raise Exception("TEST ERROR") # noqa: TRY002

async_register_info(hass, "lovelace", mock_info)
assert await async_setup_component(hass, "system_health", {})
Expand Down
2 changes: 1 addition & 1 deletion tests/components/system_log/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async def get_error_log(hass_ws_client):

def _generate_and_log_exception(exception, log):
try:
raise Exception(exception) # pylint: disable=broad-exception-raised
raise Exception(exception) # noqa: TRY002
except Exception:
_LOGGER.exception(log)

Expand Down
4 changes: 2 additions & 2 deletions tests/components/tts/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ def get_tts_audio(
) -> tts.TtsAudioType:
"""Load TTS dat."""
# This should not be called, data should be fetched from cache
raise Exception("Boom!") # pylint: disable=broad-exception-raised
raise Exception("Boom!") # noqa: TRY002


class MockEntityBoom(MockTTSEntity):
Expand All @@ -1027,7 +1027,7 @@ def get_tts_audio(
) -> tts.TtsAudioType:
"""Load TTS dat."""
# This should not be called, data should be fetched from cache
raise Exception("Boom!") # pylint: disable=broad-exception-raised
raise Exception("Boom!") # noqa: TRY002


@pytest.mark.parametrize("mock_provider", [MockProviderBoom(DEFAULT_LANG)])
Expand Down
2 changes: 1 addition & 1 deletion tests/components/tts/test_legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ async def async_get_engine(
discovery_info: DiscoveryInfoType | None = None,
) -> Provider:
"""Raise exception during platform setup."""
raise Exception("Setup error") # pylint: disable=broad-exception-raised
raise Exception("Setup error") # noqa: TRY002

mock_integration(hass, MockModule(domain="bad_tts"))
mock_platform(hass, "bad_tts.tts", BadPlatform(mock_provider))
Expand Down
6 changes: 2 additions & 4 deletions tests/helpers/test_dispatcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,7 @@ async def test_callback_exception_gets_logged(
@callback
def bad_handler(*args):
"""Record calls."""
# pylint: disable-next=broad-exception-raised
raise Exception("This is a bad message callback")
raise Exception("This is a bad message callback") # noqa: TRY002

# wrap in partial to test message logging.
async_dispatcher_connect(hass, "test", partial(bad_handler))
Expand All @@ -209,8 +208,7 @@ async def test_coro_exception_gets_logged(

async def bad_async_handler(*args):
"""Record calls."""
# pylint: disable-next=broad-exception-raised
raise Exception("This is a bad message in a coro")
raise Exception("This is a bad message in a coro") # noqa: TRY002

# wrap in partial to test message logging.
async_dispatcher_connect(hass, "test", bad_async_handler)
Expand Down
2 changes: 1 addition & 1 deletion tests/test_config_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ async def test_remove_entry_raises(

async def mock_unload_entry(hass, entry):
"""Mock unload entry function."""
raise Exception("BROKEN") # pylint: disable=broad-exception-raised
raise Exception("BROKEN") # noqa: TRY002

mock_integration(hass, MockModule("comp", async_unload_entry=mock_unload_entry))

Expand Down
4 changes: 2 additions & 2 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,11 @@ def can_call_async_get_hass() -> bool:
try:
if ha.async_get_hass() is hass:
return True
raise Exception # pylint: disable=broad-exception-raised
raise Exception # noqa: TRY002
except HomeAssistantError:
return False

raise Exception # pylint: disable=broad-exception-raised
raise Exception # noqa: TRY002

# Test scheduling a coroutine which calls async_get_hass via hass.async_create_task
async def _async_create_task() -> None:
Expand Down
5 changes: 2 additions & 3 deletions tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ async def async_raise(*_):
try:
await asyncio.sleep(2)
except asyncio.CancelledError:
raise Exception # pylint: disable=broad-exception-raised
raise Exception # noqa: TRY002

async def async_shielded(*_):
try:
Expand Down Expand Up @@ -142,8 +142,7 @@ async def test_unhandled_exception_traceback(

async def _unhandled_exception():
raised.set()
# pylint: disable-next=broad-exception-raised
raise Exception("This is unhandled")
raise Exception("This is unhandled") # noqa: TRY002

try:
hass.loop.set_debug(True)
Expand Down
7 changes: 3 additions & 4 deletions tests/test_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ async def test_component_exception_setup(hass: HomeAssistant) -> None:

def exception_setup(hass, config):
"""Raise exception."""
raise Exception("fail!") # pylint: disable=broad-exception-raised
raise Exception("fail!") # noqa: TRY002

mock_integration(hass, MockModule("comp", setup=exception_setup))

Expand All @@ -352,7 +352,7 @@ async def test_component_base_exception_setup(hass: HomeAssistant) -> None:

def exception_setup(hass, config):
"""Raise exception."""
raise BaseException("fail!") # pylint: disable=broad-exception-raised
raise BaseException("fail!") # noqa: TRY002

mock_integration(hass, MockModule("comp", setup=exception_setup))

Expand All @@ -372,8 +372,7 @@ def config_check_setup(hass, config):
"""Test that config is passed in."""
if config.get("comp_a", {}).get("valid", False):
return True
# pylint: disable-next=broad-exception-raised
raise Exception(f"Config not passed in: {config}")
raise Exception(f"Config not passed in: {config}") # noqa: TRY002

platform = MockPlatform()

Expand Down
3 changes: 1 addition & 2 deletions tests/util/test_logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ async def test_async_create_catching_coro(
"""Test exception logging of wrapped coroutine."""

async def job():
# pylint: disable-next=broad-exception-raised
raise Exception("This is a bad coroutine")
raise Exception("This is a bad coroutine") # noqa: TRY002

hass.async_create_task(logging_util.async_create_catching_coro(job()))
await hass.async_block_till_done()
Expand Down

0 comments on commit ad26db7

Please sign in to comment.