Skip to content

Commit 2c13c70

Browse files
authored
Update ruff to 0.12.0 (home-assistant#147106)
1 parent 73d0d87 commit 2c13c70

File tree

96 files changed

+291
-427
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+291
-427
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/astral-sh/ruff-pre-commit
3-
rev: v0.11.12
3+
rev: v0.12.0
44
hooks:
55
- id: ruff-check
66
args:

homeassistant/__main__.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ def validate_python() -> None:
3838

3939
def ensure_config_path(config_dir: str) -> None:
4040
"""Validate the configuration directory."""
41-
# pylint: disable-next=import-outside-toplevel
42-
from . import config as config_util
41+
from . import config as config_util # noqa: PLC0415
4342

4443
lib_dir = os.path.join(config_dir, "deps")
4544

@@ -80,8 +79,7 @@ def ensure_config_path(config_dir: str) -> None:
8079

8180
def get_arguments() -> argparse.Namespace:
8281
"""Get parsed passed in arguments."""
83-
# pylint: disable-next=import-outside-toplevel
84-
from . import config as config_util
82+
from . import config as config_util # noqa: PLC0415
8583

8684
parser = argparse.ArgumentParser(
8785
description="Home Assistant: Observe, Control, Automate.",
@@ -177,8 +175,7 @@ def main() -> int:
177175
validate_os()
178176

179177
if args.script is not None:
180-
# pylint: disable-next=import-outside-toplevel
181-
from . import scripts
178+
from . import scripts # noqa: PLC0415
182179

183180
return scripts.run(args.script)
184181

@@ -188,8 +185,7 @@ def main() -> int:
188185

189186
ensure_config_path(config_dir)
190187

191-
# pylint: disable-next=import-outside-toplevel
192-
from . import config, runner
188+
from . import config, runner # noqa: PLC0415
193189

194190
safe_mode = config.safe_mode_enabled(config_dir)
195191

homeassistant/auth/mfa_modules/notify.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,28 +52,28 @@
5252

5353
def _generate_secret() -> str:
5454
"""Generate a secret."""
55-
import pyotp # pylint: disable=import-outside-toplevel
55+
import pyotp # noqa: PLC0415
5656

5757
return str(pyotp.random_base32())
5858

5959

6060
def _generate_random() -> int:
6161
"""Generate a 32 digit number."""
62-
import pyotp # pylint: disable=import-outside-toplevel
62+
import pyotp # noqa: PLC0415
6363

6464
return int(pyotp.random_base32(length=32, chars=list("1234567890")))
6565

6666

6767
def _generate_otp(secret: str, count: int) -> str:
6868
"""Generate one time password."""
69-
import pyotp # pylint: disable=import-outside-toplevel
69+
import pyotp # noqa: PLC0415
7070

7171
return str(pyotp.HOTP(secret).at(count))
7272

7373

7474
def _verify_otp(secret: str, otp: str, count: int) -> bool:
7575
"""Verify one time password."""
76-
import pyotp # pylint: disable=import-outside-toplevel
76+
import pyotp # noqa: PLC0415
7777

7878
return bool(pyotp.HOTP(secret).verify(otp, count))
7979

homeassistant/auth/mfa_modules/totp.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
def _generate_qr_code(data: str) -> str:
3939
"""Generate a base64 PNG string represent QR Code image of data."""
40-
import pyqrcode # pylint: disable=import-outside-toplevel
40+
import pyqrcode # noqa: PLC0415
4141

4242
qr_code = pyqrcode.create(data)
4343

@@ -59,7 +59,7 @@ def _generate_qr_code(data: str) -> str:
5959

6060
def _generate_secret_and_qr_code(username: str) -> tuple[str, str, str]:
6161
"""Generate a secret, url, and QR code."""
62-
import pyotp # pylint: disable=import-outside-toplevel
62+
import pyotp # noqa: PLC0415
6363

6464
ota_secret = pyotp.random_base32()
6565
url = pyotp.totp.TOTP(ota_secret).provisioning_uri(
@@ -107,7 +107,7 @@ async def _async_save(self) -> None:
107107

108108
def _add_ota_secret(self, user_id: str, secret: str | None = None) -> str:
109109
"""Create a ota_secret for user."""
110-
import pyotp # pylint: disable=import-outside-toplevel
110+
import pyotp # noqa: PLC0415
111111

112112
ota_secret: str = secret or pyotp.random_base32()
113113

@@ -163,7 +163,7 @@ async def async_validate(self, user_id: str, user_input: dict[str, Any]) -> bool
163163

164164
def _validate_2fa(self, user_id: str, code: str) -> bool:
165165
"""Validate two factor authentication code."""
166-
import pyotp # pylint: disable=import-outside-toplevel
166+
import pyotp # noqa: PLC0415
167167

168168
if (ota_secret := self._users.get(user_id)) is None: # type: ignore[union-attr]
169169
# even we cannot find user, we still do verify
@@ -196,7 +196,7 @@ async def async_step_init(
196196
Return self.async_show_form(step_id='init') if user_input is None.
197197
Return self.async_create_entry(data={'result': result}) if finish.
198198
"""
199-
import pyotp # pylint: disable=import-outside-toplevel
199+
import pyotp # noqa: PLC0415
200200

201201
errors: dict[str, str] = {}
202202

homeassistant/bootstrap.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ async def create_hass() -> core.HomeAssistant:
394394

395395
def open_hass_ui(hass: core.HomeAssistant) -> None:
396396
"""Open the UI."""
397-
import webbrowser # pylint: disable=import-outside-toplevel
397+
import webbrowser # noqa: PLC0415
398398

399399
if hass.config.api is None or "frontend" not in hass.config.components:
400400
_LOGGER.warning("Cannot launch the UI because frontend not loaded")
@@ -561,8 +561,7 @@ async def async_enable_logging(
561561

562562
if not log_no_color:
563563
try:
564-
# pylint: disable-next=import-outside-toplevel
565-
from colorlog import ColoredFormatter
564+
from colorlog import ColoredFormatter # noqa: PLC0415
566565

567566
# basicConfig must be called after importing colorlog in order to
568567
# ensure that the handlers it sets up wraps the correct streams.
@@ -606,7 +605,7 @@ async def async_enable_logging(
606605
)
607606
threading.excepthook = lambda args: logging.getLogger().exception(
608607
"Uncaught thread exception",
609-
exc_info=( # type: ignore[arg-type]
608+
exc_info=( # type: ignore[arg-type] # noqa: LOG014
610609
args.exc_type,
611610
args.exc_value,
612611
args.exc_traceback,
@@ -1060,5 +1059,5 @@ async def _async_setup_multi_components(
10601059
_LOGGER.error(
10611060
"Error setting up integration %s - received exception",
10621061
domain,
1063-
exc_info=(type(result), result, result.__traceback__),
1062+
exc_info=(type(result), result, result.__traceback__), # noqa: LOG014
10641063
)

homeassistant/components/airly/config_flow.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ async def async_step_user(
3939
)
4040
self._abort_if_unique_id_configured()
4141
try:
42-
location_point_valid = await test_location(
42+
location_point_valid = await check_location(
4343
websession,
4444
user_input["api_key"],
4545
user_input["latitude"],
4646
user_input["longitude"],
4747
)
4848
if not location_point_valid:
49-
location_nearest_valid = await test_location(
49+
location_nearest_valid = await check_location(
5050
websession,
5151
user_input["api_key"],
5252
user_input["latitude"],
@@ -88,7 +88,7 @@ async def async_step_user(
8888
)
8989

9090

91-
async def test_location(
91+
async def check_location(
9292
client: ClientSession,
9393
api_key: str,
9494
latitude: float,

homeassistant/components/automation/helpers.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
def _blueprint_in_use(hass: HomeAssistant, blueprint_path: str) -> bool:
1414
"""Return True if any automation references the blueprint."""
15-
from . import automations_with_blueprint # pylint: disable=import-outside-toplevel
15+
from . import automations_with_blueprint # noqa: PLC0415
1616

1717
return len(automations_with_blueprint(hass, blueprint_path)) > 0
1818

@@ -28,8 +28,7 @@ async def _reload_blueprint_automations(
2828
@callback
2929
def async_get_blueprints(hass: HomeAssistant) -> blueprint.DomainBlueprints:
3030
"""Get automation blueprints."""
31-
# pylint: disable-next=import-outside-toplevel
32-
from .config import AUTOMATION_BLUEPRINT_SCHEMA
31+
from .config import AUTOMATION_BLUEPRINT_SCHEMA # noqa: PLC0415
3332

3433
return blueprint.DomainBlueprints(
3534
hass,

homeassistant/components/backup/__init__.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,10 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
9494
if not with_hassio:
9595
reader_writer = CoreBackupReaderWriter(hass)
9696
else:
97-
# pylint: disable-next=import-outside-toplevel, hass-component-root-import
98-
from homeassistant.components.hassio.backup import SupervisorBackupReaderWriter
97+
# pylint: disable-next=hass-component-root-import
98+
from homeassistant.components.hassio.backup import ( # noqa: PLC0415
99+
SupervisorBackupReaderWriter,
100+
)
99101

100102
reader_writer = SupervisorBackupReaderWriter(hass)
101103

homeassistant/components/control4/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ class Control4RuntimeData:
5454
type Control4ConfigEntry = ConfigEntry[Control4RuntimeData]
5555

5656

57-
async def call_c4_api_retry(func, *func_args):
57+
async def call_c4_api_retry(func, *func_args): # noqa: RET503
5858
"""Call C4 API function and retry on failure."""
5959
# Ruff doesn't understand this loop - the exception is always raised after the retries
60-
for i in range(API_RETRY_TIMES): # noqa: RET503
60+
for i in range(API_RETRY_TIMES):
6161
try:
6262
return await func(*func_args)
6363
except client_exceptions.ClientError as exception:

homeassistant/components/conversation/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
271271
)
272272

273273
# Temporary migration. We can remove this in 2024.10
274-
from homeassistant.components.assist_pipeline import ( # pylint: disable=import-outside-toplevel
274+
from homeassistant.components.assist_pipeline import ( # noqa: PLC0415
275275
async_migrate_engine,
276276
)
277277

homeassistant/components/downloader/services.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@ def do_download() -> None:
108108
_LOGGER.debug("%s -> %s", url, final_path)
109109

110110
with open(final_path, "wb") as fil:
111-
for chunk in req.iter_content(1024):
112-
fil.write(chunk)
111+
fil.writelines(req.iter_content(1024))
113112

114113
_LOGGER.debug("Downloading of %s done", url)
115114
service.hass.bus.fire(

homeassistant/components/esphome/dashboard.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,7 @@ async def async_setup(self) -> None:
6363
if not (data := self._data) or not (info := data.get("info")):
6464
return
6565
if is_hassio(self._hass):
66-
from homeassistant.components.hassio import ( # pylint: disable=import-outside-toplevel
67-
get_addons_info,
68-
)
66+
from homeassistant.components.hassio import get_addons_info # noqa: PLC0415
6967

7068
if (addons := get_addons_info(self._hass)) is not None and info[
7169
"addon_slug"

homeassistant/components/frontend/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,8 +364,7 @@ def _frontend_root(dev_repo_path: str | None) -> pathlib.Path:
364364
if dev_repo_path is not None:
365365
return pathlib.Path(dev_repo_path) / "hass_frontend"
366366
# Keep import here so that we can import frontend without installing reqs
367-
# pylint: disable-next=import-outside-toplevel
368-
import hass_frontend
367+
import hass_frontend # noqa: PLC0415
369368

370369
return hass_frontend.where()
371370

homeassistant/components/google_assistant/helpers.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,7 @@ async def async_report_state_all(self, message):
212212
def async_enable_report_state(self) -> None:
213213
"""Enable proactive mode."""
214214
# Circular dep
215-
# pylint: disable-next=import-outside-toplevel
216-
from .report_state import async_enable_report_state
215+
from .report_state import async_enable_report_state # noqa: PLC0415
217216

218217
if self._unsub_report_state is None:
219218
self._unsub_report_state = async_enable_report_state(self.hass, self)
@@ -395,8 +394,7 @@ def async_disable_local_sdk(self) -> None:
395394
async def _handle_local_webhook(self, hass, webhook_id, request):
396395
"""Handle an incoming local SDK message."""
397396
# Circular dep
398-
# pylint: disable-next=import-outside-toplevel
399-
from . import smart_home
397+
from . import smart_home # noqa: PLC0415
400398

401399
self._local_last_active = utcnow()
402400

@@ -655,8 +653,9 @@ def sync_serialize(self, agent_user_id, instance_uuid):
655653
if "matter" in self.hass.config.components and any(
656654
x for x in device_entry.identifiers if x[0] == "matter"
657655
):
658-
# pylint: disable-next=import-outside-toplevel
659-
from homeassistant.components.matter import get_matter_device_info
656+
from homeassistant.components.matter import ( # noqa: PLC0415
657+
get_matter_device_info,
658+
)
660659

661660
# Import matter can block the event loop for multiple seconds
662661
# so we import it here to avoid blocking the event loop during

homeassistant/components/hassio/update_helper.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ async def update_addon(
2929
client = get_supervisor_client(hass)
3030

3131
if backup:
32-
# pylint: disable-next=import-outside-toplevel
33-
from .backup import backup_addon_before_update
32+
from .backup import backup_addon_before_update # noqa: PLC0415
3433

3534
await backup_addon_before_update(hass, addon, addon_name, installed_version)
3635

@@ -50,8 +49,7 @@ async def update_core(hass: HomeAssistant, version: str | None, backup: bool) ->
5049
client = get_supervisor_client(hass)
5150

5251
if backup:
53-
# pylint: disable-next=import-outside-toplevel
54-
from .backup import backup_core_before_update
52+
from .backup import backup_core_before_update # noqa: PLC0415
5553

5654
await backup_core_before_update(hass)
5755

@@ -71,8 +69,7 @@ async def update_os(hass: HomeAssistant, version: str | None, backup: bool) -> N
7169
client = get_supervisor_client(hass)
7270

7371
if backup:
74-
# pylint: disable-next=import-outside-toplevel
75-
from .backup import backup_core_before_update
72+
from .backup import backup_core_before_update # noqa: PLC0415
7673

7774
await backup_core_before_update(hass)
7875

homeassistant/components/homeassistant_hardware/silabs_multiprotocol_addon.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,7 @@ class OptionsFlowHandler(OptionsFlow, ABC):
309309

310310
def __init__(self, config_entry: ConfigEntry) -> None:
311311
"""Set up the options flow."""
312-
# pylint: disable-next=import-outside-toplevel
313-
from homeassistant.components.zha.radio_manager import (
312+
from homeassistant.components.zha.radio_manager import ( # noqa: PLC0415
314313
ZhaMultiPANMigrationHelper,
315314
)
316315

@@ -451,16 +450,11 @@ async def async_step_configure_addon(
451450
self, user_input: dict[str, Any] | None = None
452451
) -> ConfigFlowResult:
453452
"""Configure the Silicon Labs Multiprotocol add-on."""
454-
# pylint: disable-next=import-outside-toplevel
455-
from homeassistant.components.zha import DOMAIN as ZHA_DOMAIN
456-
457-
# pylint: disable-next=import-outside-toplevel
458-
from homeassistant.components.zha.radio_manager import (
453+
from homeassistant.components.zha import DOMAIN as ZHA_DOMAIN # noqa: PLC0415
454+
from homeassistant.components.zha.radio_manager import ( # noqa: PLC0415
459455
ZhaMultiPANMigrationHelper,
460456
)
461-
462-
# pylint: disable-next=import-outside-toplevel
463-
from homeassistant.components.zha.silabs_multiprotocol import (
457+
from homeassistant.components.zha.silabs_multiprotocol import ( # noqa: PLC0415
464458
async_get_channel as async_get_zha_channel,
465459
)
466460

@@ -747,11 +741,8 @@ async def async_step_configure_flasher_addon(
747741
self, user_input: dict[str, Any] | None = None
748742
) -> ConfigFlowResult:
749743
"""Perform initial backup and reconfigure ZHA."""
750-
# pylint: disable-next=import-outside-toplevel
751-
from homeassistant.components.zha import DOMAIN as ZHA_DOMAIN
752-
753-
# pylint: disable-next=import-outside-toplevel
754-
from homeassistant.components.zha.radio_manager import (
744+
from homeassistant.components.zha import DOMAIN as ZHA_DOMAIN # noqa: PLC0415
745+
from homeassistant.components.zha.radio_manager import ( # noqa: PLC0415
755746
ZhaMultiPANMigrationHelper,
756747
)
757748

homeassistant/components/homekit_controller/config_flow.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -355,11 +355,10 @@ async def async_step_bluetooth(
355355
return self.async_abort(reason="ignored_model")
356356

357357
# Late imports in case BLE is not available
358-
# pylint: disable-next=import-outside-toplevel
359-
from aiohomekit.controller.ble.discovery import BleDiscovery
360-
361-
# pylint: disable-next=import-outside-toplevel
362-
from aiohomekit.controller.ble.manufacturer_data import HomeKitAdvertisement
358+
from aiohomekit.controller.ble.discovery import BleDiscovery # noqa: PLC0415
359+
from aiohomekit.controller.ble.manufacturer_data import ( # noqa: PLC0415
360+
HomeKitAdvertisement,
361+
)
363362

364363
mfr_data = discovery_info.manufacturer_data
365364

0 commit comments

Comments
 (0)