mirror of
https://github.com/esphome/esphome.git
synced 2026-06-24 09:57:43 +00:00
[core] Enable ruff BLE (flake8-blind-except) lint family (#16659)
This commit is contained in:
@@ -1800,7 +1800,7 @@ def command_analyze_memory(args: ArgsProtocol, config: ConfigType) -> int:
|
|||||||
ram_report = ram_analyzer.generate_report()
|
ram_report = ram_analyzer.generate_report()
|
||||||
print()
|
print()
|
||||||
print(ram_report)
|
print(ram_report)
|
||||||
except Exception as e: # pylint: disable=broad-except
|
except Exception as e: # noqa: BLE001 # pylint: disable=broad-except
|
||||||
_LOGGER.warning("RAM strings analysis failed: %s", e)
|
_LOGGER.warning("RAM strings analysis failed: %s", e)
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
|||||||
@@ -45,7 +45,7 @@ class AsyncThreadRunner(threading.Thread, Generic[_T]):
|
|||||||
async def _runner(self) -> None:
|
async def _runner(self) -> None:
|
||||||
try:
|
try:
|
||||||
self.result = await self._coro_factory()
|
self.result = await self._coro_factory()
|
||||||
except Exception as exc: # pylint: disable=broad-except
|
except Exception as exc: # noqa: BLE001 # pylint: disable=broad-except
|
||||||
# Capture all exceptions so ``event`` is always set — otherwise a
|
# Capture all exceptions so ``event`` is always set — otherwise a
|
||||||
# crash would hang the waiter forever.
|
# crash would hang the waiter forever.
|
||||||
self.exception = exc
|
self.exception = exc
|
||||||
|
|||||||
@@ -43,7 +43,7 @@ def save_compiled_config(config: ConfigType) -> None:
|
|||||||
try:
|
try:
|
||||||
rendered = yaml_util.dump(config, show_secrets=True)
|
rendered = yaml_util.dump(config, show_secrets=True)
|
||||||
write_file(compiled_config_path(CORE.config_filename), rendered, private=True)
|
write_file(compiled_config_path(CORE.config_filename), rendered, private=True)
|
||||||
except Exception as err: # pylint: disable=broad-except
|
except Exception as err: # noqa: BLE001 # pylint: disable=broad-except
|
||||||
_LOGGER.debug("Skipping compiled config cache write: %s", err)
|
_LOGGER.debug("Skipping compiled config cache write: %s", err)
|
||||||
|
|
||||||
|
|
||||||
@@ -62,7 +62,7 @@ def load_compiled_config(conf_path: Path) -> ConfigType | None:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
config = yaml_util.load_yaml(cache_path, clear_secrets=False)
|
config = yaml_util.load_yaml(cache_path, clear_secrets=False)
|
||||||
except Exception: # pylint: disable=broad-except
|
except Exception: # noqa: BLE001 # pylint: disable=broad-except
|
||||||
return None
|
return None
|
||||||
|
|
||||||
storage = StorageJSON.load(ext_storage_path(conf_path.name))
|
storage = StorageJSON.load(ext_storage_path(conf_path.name))
|
||||||
|
|||||||
@@ -2683,7 +2683,7 @@ def _decode_pc(config, addr):
|
|||||||
command = [str(addr2line_path), "-pfiaC", "-e", str(firmware_elf_path), addr]
|
command = [str(addr2line_path), "-pfiaC", "-e", str(firmware_elf_path), addr]
|
||||||
try:
|
try:
|
||||||
translation = subprocess.check_output(command, close_fds=False).decode().strip()
|
translation = subprocess.check_output(command, close_fds=False).decode().strip()
|
||||||
except Exception: # pylint: disable=broad-except
|
except Exception: # noqa: BLE001 # pylint: disable=broad-except
|
||||||
_LOGGER.debug("Caught exception for command %s", command, exc_info=1)
|
_LOGGER.debug("Caught exception for command %s", command, exc_info=1)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|||||||
@@ -472,7 +472,7 @@ def _decode_pc(config, addr):
|
|||||||
command = [idedata.addr2line_path, "-pfiaC", "-e", idedata.firmware_elf_path, addr]
|
command = [idedata.addr2line_path, "-pfiaC", "-e", idedata.firmware_elf_path, addr]
|
||||||
try:
|
try:
|
||||||
translation = subprocess.check_output(command, close_fds=False).decode().strip()
|
translation = subprocess.check_output(command, close_fds=False).decode().strip()
|
||||||
except Exception: # pylint: disable=broad-except
|
except Exception: # noqa: BLE001 # pylint: disable=broad-except
|
||||||
_LOGGER.debug("Caught exception for command %s", command, exc_info=1)
|
_LOGGER.debug("Caught exception for command %s", command, exc_info=1)
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|||||||
@@ -535,7 +535,7 @@ def _addr2line(addr2line: str, elf: Path, addr: str) -> str:
|
|||||||
check=True,
|
check=True,
|
||||||
)
|
)
|
||||||
return result.stdout.strip().splitlines()[0]
|
return result.stdout.strip().splitlines()[0]
|
||||||
except Exception as err: # pylint: disable=broad-except
|
except Exception as err: # noqa: BLE001 # pylint: disable=broad-except
|
||||||
_LOGGER.error("Running command failed: %s", err)
|
_LOGGER.error("Running command failed: %s", err)
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|||||||
@@ -1379,7 +1379,7 @@ class LoginHandler(BaseHandler):
|
|||||||
loop = asyncio.get_running_loop()
|
loop = asyncio.get_running_loop()
|
||||||
try:
|
try:
|
||||||
req = await loop.run_in_executor(None, self._make_supervisor_auth_request)
|
req = await loop.run_in_executor(None, self._make_supervisor_auth_request)
|
||||||
except Exception as err: # pylint: disable=broad-except
|
except Exception as err: # noqa: BLE001 # pylint: disable=broad-except
|
||||||
_LOGGER.warning("Error during Hass.io auth request: %s", err)
|
_LOGGER.warning("Error during Hass.io auth request: %s", err)
|
||||||
self.set_status(500)
|
self.set_status(500)
|
||||||
self.render_login_page(error="Internal server error")
|
self.render_login_page(error="Internal server error")
|
||||||
|
|||||||
@@ -120,7 +120,7 @@ def run_extra_script(
|
|||||||
"__name__": "__pio_extra_script__",
|
"__name__": "__pio_extra_script__",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
except Exception as e: # pylint: disable=broad-exception-caught
|
except Exception as e: # noqa: BLE001 # pylint: disable=broad-exception-caught
|
||||||
_LOGGER.warning("PIO extra-script %s raised %s; skipping", script_path, e)
|
_LOGGER.warning("PIO extra-script %s raised %s; skipping", script_path, e)
|
||||||
return ExtraScriptResult()
|
return ExtraScriptResult()
|
||||||
finally:
|
finally:
|
||||||
|
|||||||
@@ -783,7 +783,7 @@ def download_from_mirrors(
|
|||||||
f.seek(0)
|
f.seek(0)
|
||||||
return url
|
return url
|
||||||
|
|
||||||
except Exception as e: # pylint: disable=broad-exception-caught
|
except Exception as e: # noqa: BLE001 # pylint: disable=broad-exception-caught
|
||||||
_LOGGER.debug("Failed to download %s: %s", url, str(e))
|
_LOGGER.debug("Failed to download %s: %s", url, str(e))
|
||||||
last_exception = e
|
last_exception = e
|
||||||
|
|
||||||
|
|||||||
@@ -94,7 +94,7 @@ def patch_file_downloader() -> None:
|
|||||||
self._http_response.close()
|
self._http_response.close()
|
||||||
if hasattr(self, "_http_session"):
|
if hasattr(self, "_http_session"):
|
||||||
self._http_session.close()
|
self._http_session.close()
|
||||||
except Exception:
|
except Exception: # noqa: BLE001
|
||||||
pass
|
pass
|
||||||
# pylint: enable=protected-access,broad-except
|
# pylint: enable=protected-access,broad-except
|
||||||
time.sleep(delay)
|
time.sleep(delay)
|
||||||
|
|||||||
@@ -267,7 +267,7 @@ class StorageJSON:
|
|||||||
def load(path: Path) -> StorageJSON | None:
|
def load(path: Path) -> StorageJSON | None:
|
||||||
try:
|
try:
|
||||||
return StorageJSON._load_impl(path)
|
return StorageJSON._load_impl(path)
|
||||||
except Exception: # pylint: disable=broad-except
|
except Exception: # noqa: BLE001 # pylint: disable=broad-except
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def apply_to_core(self) -> None:
|
def apply_to_core(self) -> None:
|
||||||
@@ -342,7 +342,7 @@ class EsphomeStorageJSON:
|
|||||||
return datetime.strptime( # noqa: DTZ007
|
return datetime.strptime( # noqa: DTZ007
|
||||||
self.last_update_check_str, "%Y-%m-%dT%H:%M:%S"
|
self.last_update_check_str, "%Y-%m-%dT%H:%M:%S"
|
||||||
)
|
)
|
||||||
except Exception: # pylint: disable=broad-except
|
except Exception: # noqa: BLE001 # pylint: disable=broad-except
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@last_update_check.setter
|
@last_update_check.setter
|
||||||
@@ -371,7 +371,7 @@ class EsphomeStorageJSON:
|
|||||||
def load(path: str) -> EsphomeStorageJSON | None:
|
def load(path: str) -> EsphomeStorageJSON | None:
|
||||||
try:
|
try:
|
||||||
return EsphomeStorageJSON._load_impl(path)
|
return EsphomeStorageJSON._load_impl(path)
|
||||||
except Exception: # pylint: disable=broad-except
|
except Exception: # noqa: BLE001 # pylint: disable=broad-except
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|||||||
@@ -271,7 +271,7 @@ def run_external_command(
|
|||||||
raise
|
raise
|
||||||
except SystemExit as err:
|
except SystemExit as err:
|
||||||
return err.args[0]
|
return err.args[0]
|
||||||
except Exception as err: # pylint: disable=broad-except
|
except Exception as err: # noqa: BLE001 # pylint: disable=broad-except
|
||||||
_LOGGER.error("Running command failed: %s", err)
|
_LOGGER.error("Running command failed: %s", err)
|
||||||
_LOGGER.error("Please try running %s locally.", full_cmd)
|
_LOGGER.error("Please try running %s locally.", full_cmd)
|
||||||
return 1
|
return 1
|
||||||
@@ -318,7 +318,7 @@ def run_external_process(*cmd: str, **kwargs: Any) -> int | str:
|
|||||||
return proc.stdout if capture_stdout else proc.returncode
|
return proc.stdout if capture_stdout else proc.returncode
|
||||||
except KeyboardInterrupt: # pylint: disable=try-except-raise
|
except KeyboardInterrupt: # pylint: disable=try-except-raise
|
||||||
raise
|
raise
|
||||||
except Exception as err: # pylint: disable=broad-except
|
except Exception as err: # noqa: BLE001 # pylint: disable=broad-except
|
||||||
_LOGGER.error("Running command failed: %s", err)
|
_LOGGER.error("Running command failed: %s", err)
|
||||||
_LOGGER.error("Please try running %s locally.", full_cmd)
|
_LOGGER.error("Please try running %s locally.", full_cmd)
|
||||||
return 1
|
return 1
|
||||||
|
|||||||
@@ -134,13 +134,13 @@ def read_config(args):
|
|||||||
try:
|
try:
|
||||||
config = loader(file_name)
|
config = loader(file_name)
|
||||||
res = validate_config(config, command_line_substitutions)
|
res = validate_config(config, command_line_substitutions)
|
||||||
except Exception as err: # pylint: disable=broad-except
|
except Exception as err: # noqa: BLE001 # pylint: disable=broad-except
|
||||||
vs.add_yaml_error(str(err))
|
vs.add_yaml_error(str(err))
|
||||||
else:
|
else:
|
||||||
for err in res.errors:
|
for err in res.errors:
|
||||||
try:
|
try:
|
||||||
range_ = _get_invalid_range(res, err)
|
range_ = _get_invalid_range(res, err)
|
||||||
vs.add_validation_error(range_, _format_vol_invalid(err, res))
|
vs.add_validation_error(range_, _format_vol_invalid(err, res))
|
||||||
except Exception: # pylint: disable=broad-except
|
except Exception: # noqa: BLE001 # pylint: disable=broad-except
|
||||||
continue
|
continue
|
||||||
print(vs.dump())
|
print(vs.dump())
|
||||||
|
|||||||
@@ -342,7 +342,7 @@ async def async_discover_mdns_devices(
|
|||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
aiozc = AsyncEsphomeZeroconf()
|
aiozc = AsyncEsphomeZeroconf()
|
||||||
except Exception as err: # pylint: disable=broad-except
|
except Exception as err: # noqa: BLE001 # pylint: disable=broad-except
|
||||||
# Zeroconf init can raise OSError, NonUniqueNameException, etc.
|
# Zeroconf init can raise OSError, NonUniqueNameException, etc.
|
||||||
# Any failure here just means we can't discover — log and move on.
|
# Any failure here just means we can't discover — log and move on.
|
||||||
_LOGGER.warning("mDNS discovery failed to initialize: %s", err)
|
_LOGGER.warning("mDNS discovery failed to initialize: %s", err)
|
||||||
|
|||||||
@@ -112,6 +112,7 @@ exclude = ['generated']
|
|||||||
[tool.ruff.lint]
|
[tool.ruff.lint]
|
||||||
select = [
|
select = [
|
||||||
"B", # flake8-bugbear
|
"B", # flake8-bugbear
|
||||||
|
"BLE", # flake8-blind-except
|
||||||
"C4", # flake8-comprehensions
|
"C4", # flake8-comprehensions
|
||||||
"DTZ", # flake8-datetimez
|
"DTZ", # flake8-datetimez
|
||||||
"E", # pycodestyle
|
"E", # pycodestyle
|
||||||
|
|||||||
@@ -128,7 +128,7 @@ def uses_local_file_references(component_dir: Path) -> bool:
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
content = common_yaml.read_text()
|
content = common_yaml.read_text()
|
||||||
except Exception: # pylint: disable=broad-exception-caught
|
except Exception: # noqa: BLE001 # pylint: disable=broad-exception-caught
|
||||||
return False
|
return False
|
||||||
|
|
||||||
# Pattern to match $component_dir or ${component_dir} references
|
# Pattern to match $component_dir or ${component_dir} references
|
||||||
@@ -164,7 +164,7 @@ def is_platform_component(component_dir: Path) -> bool:
|
|||||||
try:
|
try:
|
||||||
content = comp_init.read_text()
|
content = comp_init.read_text()
|
||||||
return "IS_PLATFORM_COMPONENT = True" in content
|
return "IS_PLATFORM_COMPONENT = True" in content
|
||||||
except Exception: # pylint: disable=broad-exception-caught
|
except Exception: # noqa: BLE001 # pylint: disable=broad-exception-caught
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
@@ -222,7 +222,7 @@ def analyze_yaml_file(yaml_file: Path) -> dict[str, Any]:
|
|||||||
try:
|
try:
|
||||||
data = yaml_util.load_yaml(yaml_file)
|
data = yaml_util.load_yaml(yaml_file)
|
||||||
result["loaded"] = True
|
result["loaded"] = True
|
||||||
except Exception: # pylint: disable=broad-exception-caught
|
except Exception: # noqa: BLE001 # pylint: disable=broad-exception-caught
|
||||||
return result
|
return result
|
||||||
|
|
||||||
# Check for Extend/Remove objects
|
# Check for Extend/Remove objects
|
||||||
|
|||||||
@@ -392,7 +392,7 @@ def compile_and_get_binary(
|
|||||||
if exit_code != 0:
|
if exit_code != 0:
|
||||||
print(f"Error compiling {label} for {', '.join(components)}")
|
print(f"Error compiling {label} for {', '.join(components)}")
|
||||||
return exit_code, None
|
return exit_code, None
|
||||||
except Exception as e:
|
except Exception as e: # noqa: BLE001
|
||||||
print(f"Error compiling {label} for {', '.join(components)}: {e}")
|
print(f"Error compiling {label} for {', '.join(components)}: {e}")
|
||||||
return EXIT_COMPILE_ERROR, None
|
return EXIT_COMPILE_ERROR, None
|
||||||
|
|
||||||
|
|||||||
@@ -312,7 +312,7 @@ def _is_clang_tidy_full_scan() -> bool:
|
|||||||
)
|
)
|
||||||
# Exit 0 means hash changed (full scan needed)
|
# Exit 0 means hash changed (full scan needed)
|
||||||
return result.returncode == 0
|
return result.returncode == 0
|
||||||
except Exception:
|
except Exception: # noqa: BLE001
|
||||||
# If hash check fails, run full scan to be safe
|
# If hash check fails, run full scan to be safe
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|||||||
@@ -437,7 +437,7 @@ def main() -> None:
|
|||||||
tests_dir=args.tests_dir,
|
tests_dir=args.tests_dir,
|
||||||
output_file=args.output,
|
output_file=args.output,
|
||||||
)
|
)
|
||||||
except Exception as e:
|
except Exception as e: # noqa: BLE001
|
||||||
print(f"Error merging configs: {e}", file=sys.stderr)
|
print(f"Error merging configs: {e}", file=sys.stderr)
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ async def connect_disconnect(client_id: int, iteration: int) -> tuple[int, bool,
|
|||||||
await asyncio.wait_for(cli.connect(login=True), timeout=10)
|
await asyncio.wait_for(cli.connect(login=True), timeout=10)
|
||||||
await cli.disconnect()
|
await cli.disconnect()
|
||||||
return iteration, True, ""
|
return iteration, True, ""
|
||||||
except Exception as e:
|
except Exception as e: # noqa: BLE001
|
||||||
return (
|
return (
|
||||||
iteration,
|
iteration,
|
||||||
False,
|
False,
|
||||||
|
|||||||
@@ -63,7 +63,7 @@ def test_component_group(
|
|||||||
try:
|
try:
|
||||||
result = subprocess.run(cmd, check=False)
|
result = subprocess.run(cmd, check=False)
|
||||||
return result.returncode == 0
|
return result.returncode == 0
|
||||||
except Exception as e:
|
except Exception as e: # noqa: BLE001
|
||||||
print(f"Error running test: {e}")
|
print(f"Error running test: {e}")
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ async def syslog_udp_listener() -> AsyncGenerator[tuple[int, SyslogReceiver]]:
|
|||||||
receiver.on_message(msg)
|
receiver.on_message(msg)
|
||||||
except BlockingIOError:
|
except BlockingIOError:
|
||||||
await asyncio.sleep(0.01)
|
await asyncio.sleep(0.01)
|
||||||
except Exception:
|
except Exception: # noqa: BLE001
|
||||||
break
|
break
|
||||||
|
|
||||||
task = asyncio.create_task(receive_messages())
|
task = asyncio.create_task(receive_messages())
|
||||||
|
|||||||
@@ -80,7 +80,7 @@ async def udp_listener(port: int = 0) -> AsyncGenerator[tuple[int, UDPReceiver]]
|
|||||||
receiver.on_message(data)
|
receiver.on_message(data)
|
||||||
except BlockingIOError:
|
except BlockingIOError:
|
||||||
await asyncio.sleep(0.01)
|
await asyncio.sleep(0.01)
|
||||||
except Exception:
|
except Exception: # noqa: BLE001
|
||||||
break
|
break
|
||||||
|
|
||||||
task = asyncio.create_task(receive_messages())
|
task = asyncio.create_task(receive_messages())
|
||||||
|
|||||||
Reference in New Issue
Block a user