From ef8b8fbbaaf3f5909a9de10e8d7a335e66a1ab94 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Fri, 17 Jan 2025 12:28:27 +0100 Subject: [PATCH] Enable RUF023 (#135830) --- homeassistant/components/bluetooth/manager.py | 6 ++-- homeassistant/components/bluetooth/match.py | 10 +++--- homeassistant/components/esphome/manager.py | 10 +++--- .../components/google_assistant/helpers.py | 2 +- homeassistant/components/http/web_runner.py | 2 +- .../components/recorder/models/legacy.py | 8 ++--- .../components/system_log/__init__.py | 10 +++--- .../components/websocket_api/connection.py | 20 +++++------ .../components/websocket_api/http.py | 18 +++++----- homeassistant/core.py | 32 ++++++++--------- homeassistant/helpers/icon.py | 2 +- homeassistant/helpers/intent.py | 14 ++++---- homeassistant/helpers/service.py | 2 +- homeassistant/helpers/template.py | 34 +++++++++---------- homeassistant/helpers/trace.py | 4 +-- homeassistant/helpers/translation.py | 2 +- pyproject.toml | 1 + tests/components/recorder/db_schema_16.py | 4 +-- tests/components/recorder/db_schema_18.py | 4 +-- tests/components/recorder/db_schema_22.py | 4 +-- tests/components/recorder/db_schema_23.py | 4 +-- .../db_schema_23_with_newer_columns.py | 4 +-- tests/components/recorder/db_schema_25.py | 6 ++-- tests/components/recorder/db_schema_28.py | 6 ++-- 24 files changed, 105 insertions(+), 104 deletions(-) diff --git a/homeassistant/components/bluetooth/manager.py b/homeassistant/components/bluetooth/manager.py index d8b3eef7685..09be8f960e9 100644 --- a/homeassistant/components/bluetooth/manager.py +++ b/homeassistant/components/bluetooth/manager.py @@ -50,11 +50,11 @@ class HomeAssistantBluetoothManager(BluetoothManager): """Manage Bluetooth for Home Assistant.""" __slots__ = ( - "hass", - "storage", - "_integration_matcher", "_callback_index", "_cancel_logging_listener", + "_integration_matcher", + "hass", + "storage", ) def __init__( diff --git a/homeassistant/components/bluetooth/match.py b/homeassistant/components/bluetooth/match.py index ee62420b692..6307d3ca93b 100644 --- a/homeassistant/components/bluetooth/match.py +++ b/homeassistant/components/bluetooth/match.py @@ -92,7 +92,7 @@ def seen_all_fields( class IntegrationMatcher: """Integration matcher for the bluetooth integration.""" - __slots__ = ("_integration_matchers", "_matched", "_matched_connectable", "_index") + __slots__ = ("_index", "_integration_matchers", "_matched", "_matched_connectable") def __init__(self, integration_matchers: list[BluetoothMatcher]) -> None: """Initialize the matcher.""" @@ -164,12 +164,12 @@ class BluetoothMatcherIndexBase[ __slots__ = ( "local_name", - "service_uuid", - "service_data_uuid", "manufacturer_id", - "service_uuid_set", - "service_data_uuid_set", "manufacturer_id_set", + "service_data_uuid", + "service_data_uuid_set", + "service_uuid", + "service_uuid_set", ) def __init__(self) -> None: diff --git a/homeassistant/components/esphome/manager.py b/homeassistant/components/esphome/manager.py index 7fcd859142a..b382622281e 100644 --- a/homeassistant/components/esphome/manager.py +++ b/homeassistant/components/esphome/manager.py @@ -134,16 +134,16 @@ class ESPHomeManager: """Class to manage an ESPHome connection.""" __slots__ = ( - "hass", - "host", - "password", - "entry", "cli", "device_id", "domain_data", + "entry", + "entry_data", + "hass", + "host", + "password", "reconnect_logic", "zeroconf_instance", - "entry_data", ) def __init__( diff --git a/homeassistant/components/google_assistant/helpers.py b/homeassistant/components/google_assistant/helpers.py index 76869487ee3..4309a99c0ca 100644 --- a/homeassistant/components/google_assistant/helpers.py +++ b/homeassistant/components/google_assistant/helpers.py @@ -521,7 +521,7 @@ def supported_traits_for_state(state: State) -> list[type[trait._Trait]]: class GoogleEntity: """Adaptation of Entity expressed in Google's terms.""" - __slots__ = ("hass", "config", "state", "entity_id", "_traits") + __slots__ = ("_traits", "config", "entity_id", "hass", "state") def __init__( self, hass: HomeAssistant, config: AbstractConfig, state: State diff --git a/homeassistant/components/http/web_runner.py b/homeassistant/components/http/web_runner.py index 4ca39eaab0c..f633433c9e4 100644 --- a/homeassistant/components/http/web_runner.py +++ b/homeassistant/components/http/web_runner.py @@ -22,7 +22,7 @@ class HomeAssistantTCPSite(web.BaseSite): is merged. """ - __slots__ = ("_host", "_port", "_reuse_address", "_reuse_port", "_hosturl") + __slots__ = ("_host", "_hosturl", "_port", "_reuse_address", "_reuse_port") def __init__( self, diff --git a/homeassistant/components/recorder/models/legacy.py b/homeassistant/components/recorder/models/legacy.py index a469aa49ab2..b5e67ff050b 100644 --- a/homeassistant/components/recorder/models/legacy.py +++ b/homeassistant/components/recorder/models/legacy.py @@ -24,12 +24,12 @@ class LegacyLazyState(State): """A lazy version of core State after schema 31.""" __slots__ = [ - "_row", "_attributes", - "_last_changed_ts", - "_last_updated_ts", - "_last_reported_ts", "_context", + "_last_changed_ts", + "_last_reported_ts", + "_last_updated_ts", + "_row", "attr_cache", ] diff --git a/homeassistant/components/system_log/__init__.py b/homeassistant/components/system_log/__init__.py index 22950aa9f1e..191a2b5feb8 100644 --- a/homeassistant/components/system_log/__init__.py +++ b/homeassistant/components/system_log/__init__.py @@ -163,16 +163,16 @@ class LogEntry: """Store HA log entries.""" __slots__ = ( + "count", + "exception", "first_occurred", - "timestamp", - "name", + "key", "level", "message", - "exception", + "name", "root_cause", "source", - "count", - "key", + "timestamp", ) def __init__( diff --git a/homeassistant/components/websocket_api/connection.py b/homeassistant/components/websocket_api/connection.py index 817444a970b..12473c86255 100644 --- a/homeassistant/components/websocket_api/connection.py +++ b/homeassistant/components/websocket_api/connection.py @@ -40,17 +40,17 @@ class ActiveConnection: """Handle an active websocket client connection.""" __slots__ = ( - "logger", - "hass", - "send_message", - "user", - "refresh_token_id", - "subscriptions", - "last_id", - "can_coalesce", - "supported_features", - "handlers", "binary_handlers", + "can_coalesce", + "handlers", + "hass", + "last_id", + "logger", + "refresh_token_id", + "send_message", + "subscriptions", + "supported_features", + "user", ) def __init__( diff --git a/homeassistant/components/websocket_api/http.py b/homeassistant/components/websocket_api/http.py index b718d8e28c8..8bfa9480ff4 100644 --- a/homeassistant/components/websocket_api/http.py +++ b/homeassistant/components/websocket_api/http.py @@ -70,20 +70,20 @@ class WebSocketHandler: """Handle an active websocket client connection.""" __slots__ = ( - "_hass", - "_loop", - "_request", - "_wsock", - "_handle_task", - "_writer_task", - "_closing", "_authenticated", - "_logger", - "_peak_checker_unsub", + "_closing", "_connection", + "_handle_task", + "_hass", + "_logger", + "_loop", "_message_queue", + "_peak_checker_unsub", "_ready_future", "_release_ready_queue_size", + "_request", + "_writer_task", + "_wsock", ) def __init__(self, hass: HomeAssistant, request: web.Request) -> None: diff --git a/homeassistant/core.py b/homeassistant/core.py index 58f96ed0ad2..74bcd844823 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -332,7 +332,7 @@ class HassJob[**_P, _R_co]: we run the job. """ - __slots__ = ("target", "name", "_cancel_on_shutdown", "_cache") + __slots__ = ("_cache", "_cancel_on_shutdown", "name", "target") def __init__( self, @@ -1246,7 +1246,7 @@ class HomeAssistant: class Context: """The context that triggered something.""" - __slots__ = ("id", "user_id", "parent_id", "origin_event", "_cache") + __slots__ = ("_cache", "id", "origin_event", "parent_id", "user_id") def __init__( self, @@ -1321,12 +1321,12 @@ class Event(Generic[_DataT]): """Representation of an event within the bus.""" __slots__ = ( - "event_type", + "_cache", + "context", "data", + "event_type", "origin", "time_fired_timestamp", - "context", - "_cache", ) def __init__( @@ -1767,18 +1767,18 @@ class State: """ __slots__ = ( - "entity_id", - "state", + "_cache", "attributes", + "context", + "domain", + "entity_id", "last_changed", "last_reported", "last_updated", - "context", - "state_info", - "domain", - "object_id", "last_updated_timestamp", - "_cache", + "object_id", + "state", + "state_info", ) def __init__( @@ -2066,7 +2066,7 @@ class States(UserDict[str, State]): class StateMachine: """Helper class that tracks the state of different entities.""" - __slots__ = ("_states", "_states_data", "_reservations", "_bus", "_loop") + __slots__ = ("_bus", "_loop", "_reservations", "_states", "_states_data") def __init__(self, bus: EventBus, loop: asyncio.events.AbstractEventLoop) -> None: """Initialize state machine.""" @@ -2404,7 +2404,7 @@ class SupportsResponse(enum.StrEnum): class Service: """Representation of a callable service.""" - __slots__ = ["job", "schema", "domain", "service", "supports_response"] + __slots__ = ["domain", "job", "schema", "service", "supports_response"] def __init__( self, @@ -2431,7 +2431,7 @@ class Service: class ServiceCall: """Representation of a call to a service.""" - __slots__ = ("hass", "domain", "service", "data", "context", "return_response") + __slots__ = ("context", "data", "domain", "hass", "return_response", "service") def __init__( self, @@ -2464,7 +2464,7 @@ class ServiceCall: class ServiceRegistry: """Offer the services over the eventbus.""" - __slots__ = ("_services", "_hass") + __slots__ = ("_hass", "_services") def __init__(self, hass: HomeAssistant) -> None: """Initialize a service registry.""" diff --git a/homeassistant/helpers/icon.py b/homeassistant/helpers/icon.py index ce8205eb915..a8c1b0b2186 100644 --- a/homeassistant/helpers/icon.py +++ b/homeassistant/helpers/icon.py @@ -78,7 +78,7 @@ async def _async_get_component_icons( class _IconsCache: """Cache for icons.""" - __slots__ = ("_hass", "_loaded", "_cache", "_lock") + __slots__ = ("_cache", "_hass", "_loaded", "_lock") def __init__(self, hass: HomeAssistant) -> None: """Initialize the cache.""" diff --git a/homeassistant/helpers/intent.py b/homeassistant/helpers/intent.py index 5fa0da96dc1..2874269892c 100644 --- a/homeassistant/helpers/intent.py +++ b/homeassistant/helpers/intent.py @@ -1202,17 +1202,17 @@ class Intent: """Hold the intent.""" __slots__ = [ + "assistant", + "category", + "context", + "conversation_agent_id", + "device_id", "hass", - "platform", "intent_type", + "language", + "platform", "slots", "text_input", - "context", - "language", - "category", - "assistant", - "device_id", - "conversation_agent_id", ] def __init__( diff --git a/homeassistant/helpers/service.py b/homeassistant/helpers/service.py index 8e9754ccb4d..255739c0059 100644 --- a/homeassistant/helpers/service.py +++ b/homeassistant/helpers/service.py @@ -225,7 +225,7 @@ class ServiceParams(TypedDict): class ServiceTargetSelector: """Class to hold a target selector for a service.""" - __slots__ = ("entity_ids", "device_ids", "area_ids", "floor_ids", "label_ids") + __slots__ = ("area_ids", "device_ids", "entity_ids", "floor_ids", "label_ids") def __init__(self, service_call: ServiceCall) -> None: """Extract ids from service call data.""" diff --git a/homeassistant/helpers/template.py b/homeassistant/helpers/template.py index 4625c3000ba..21d49df2a67 100644 --- a/homeassistant/helpers/template.py +++ b/homeassistant/helpers/template.py @@ -386,19 +386,19 @@ class RenderInfo: """Holds information about a template render.""" __slots__ = ( - "template", - "filter_lifecycle", - "filter", "_result", - "is_static", - "exception", "all_states", "all_states_lifecycle", "domains", "domains_lifecycle", "entities", - "rate_limit", + "exception", + "filter", + "filter_lifecycle", "has_time", + "is_static", + "rate_limit", + "template", ) def __init__(self, template: Template) -> None: @@ -507,17 +507,17 @@ class Template: __slots__ = ( "__weakref__", - "template", + "_compiled", + "_compiled_code", + "_exc_info", + "_hash_cache", + "_limited", + "_log_fn", + "_renders", + "_strict", "hass", "is_static", - "_compiled_code", - "_compiled", - "_exc_info", - "_limited", - "_strict", - "_log_fn", - "_hash_cache", - "_renders", + "template", ) def __init__(self, template: str, hass: HomeAssistant | None = None) -> None: @@ -991,7 +991,7 @@ class StateTranslated: class DomainStates: """Class to expose a specific HA domain as attributes.""" - __slots__ = ("_hass", "_domain") + __slots__ = ("_domain", "_hass") __setitem__ = _readonly __delitem__ = _readonly @@ -1035,7 +1035,7 @@ class DomainStates: class TemplateStateBase(State): """Class to represent a state object in a template.""" - __slots__ = ("_hass", "_collect", "_entity_id", "_state") + __slots__ = ("_collect", "_entity_id", "_hass", "_state") _state: State diff --git a/homeassistant/helpers/trace.py b/homeassistant/helpers/trace.py index 431a7a7d1f8..d191d474480 100644 --- a/homeassistant/helpers/trace.py +++ b/homeassistant/helpers/trace.py @@ -23,11 +23,11 @@ class TraceElement: "_child_run_id", "_error", "_last_variables", - "path", "_result", - "reuse_by_child", "_timestamp", "_variables", + "path", + "reuse_by_child", ) def __init__(self, variables: TemplateVarsType, path: str) -> None: diff --git a/homeassistant/helpers/translation.py b/homeassistant/helpers/translation.py index 01c47aa8d0d..fdfefc9bff4 100644 --- a/homeassistant/helpers/translation.py +++ b/homeassistant/helpers/translation.py @@ -147,7 +147,7 @@ class _TranslationsCacheData: class _TranslationCache: """Cache for flattened translations.""" - __slots__ = ("hass", "cache_data", "lock") + __slots__ = ("cache_data", "hass", "lock") def __init__(self, hass: HomeAssistant) -> None: """Initialize the cache.""" diff --git a/pyproject.toml b/pyproject.toml index 0623d681df7..84233aba242 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -765,6 +765,7 @@ select = [ "RUF020", # {never_like} | T is equivalent to T "RUF021", # Parenthesize a and b expressions when chaining and and or together, to make the precedence clear "RUF022", # Sort __all__ + "RUF023", # Sort __slots__ "RUF024", # Do not pass mutable objects as values to dict.fromkeys "RUF026", # default_factory is a positional-only argument to defaultdict "RUF030", # print() call in assert statement is likely unintentional diff --git a/tests/components/recorder/db_schema_16.py b/tests/components/recorder/db_schema_16.py index d7ca35c9341..522bd6ea367 100644 --- a/tests/components/recorder/db_schema_16.py +++ b/tests/components/recorder/db_schema_16.py @@ -347,11 +347,11 @@ class LazyState(State): """A lazy version of core State.""" __slots__ = [ - "_row", "_attributes", + "_context", "_last_changed", "_last_updated", - "_context", + "_row", ] def __init__(self, row) -> None: # pylint: disable=super-init-not-called diff --git a/tests/components/recorder/db_schema_18.py b/tests/components/recorder/db_schema_18.py index adb71dffb9e..026227f68a0 100644 --- a/tests/components/recorder/db_schema_18.py +++ b/tests/components/recorder/db_schema_18.py @@ -360,11 +360,11 @@ class LazyState(State): """A lazy version of core State.""" __slots__ = [ - "_row", "_attributes", + "_context", "_last_changed", "_last_updated", - "_context", + "_row", ] def __init__(self, row) -> None: # pylint: disable=super-init-not-called diff --git a/tests/components/recorder/db_schema_22.py b/tests/components/recorder/db_schema_22.py index c0d607b12a7..770d25c9cf2 100644 --- a/tests/components/recorder/db_schema_22.py +++ b/tests/components/recorder/db_schema_22.py @@ -479,11 +479,11 @@ class LazyState(State): """A lazy version of core State.""" __slots__ = [ - "_row", "_attributes", + "_context", "_last_changed", "_last_updated", - "_context", + "_row", ] def __init__(self, row) -> None: # pylint: disable=super-init-not-called diff --git a/tests/components/recorder/db_schema_23.py b/tests/components/recorder/db_schema_23.py index f60b7b49df4..8cf3e16e5a8 100644 --- a/tests/components/recorder/db_schema_23.py +++ b/tests/components/recorder/db_schema_23.py @@ -469,11 +469,11 @@ class LazyState(State): """A lazy version of core State.""" __slots__ = [ - "_row", "_attributes", + "_context", "_last_changed", "_last_updated", - "_context", + "_row", ] def __init__(self, row) -> None: # pylint: disable=super-init-not-called diff --git a/tests/components/recorder/db_schema_23_with_newer_columns.py b/tests/components/recorder/db_schema_23_with_newer_columns.py index 4cc1074de41..2ba62ba78f5 100644 --- a/tests/components/recorder/db_schema_23_with_newer_columns.py +++ b/tests/components/recorder/db_schema_23_with_newer_columns.py @@ -593,11 +593,11 @@ class LazyState(State): """A lazy version of core State.""" __slots__ = [ - "_row", "_attributes", + "_context", "_last_changed", "_last_updated", - "_context", + "_row", ] def __init__(self, row) -> None: # pylint: disable=super-init-not-called diff --git a/tests/components/recorder/db_schema_25.py b/tests/components/recorder/db_schema_25.py index d989cacb76a..3b7c4a300c2 100644 --- a/tests/components/recorder/db_schema_25.py +++ b/tests/components/recorder/db_schema_25.py @@ -529,12 +529,12 @@ class LazyState(State): """A lazy version of core State.""" __slots__ = [ - "_row", + "_attr_cache", "_attributes", + "_context", "_last_changed", "_last_updated", - "_context", - "_attr_cache", + "_row", ] def __init__( # pylint: disable=super-init-not-called diff --git a/tests/components/recorder/db_schema_28.py b/tests/components/recorder/db_schema_28.py index 8c984b61f6c..4d7f893de25 100644 --- a/tests/components/recorder/db_schema_28.py +++ b/tests/components/recorder/db_schema_28.py @@ -694,12 +694,12 @@ class LazyState(State): """A lazy version of core State.""" __slots__ = [ - "_row", + "_attr_cache", "_attributes", + "_context", "_last_changed", "_last_updated", - "_context", - "_attr_cache", + "_row", ] def __init__( # pylint: disable=super-init-not-called