From c8f39911cc716c6e5b304ba72c5683e7cc55c478 Mon Sep 17 00:00:00 2001 From: Joost Lekkerkerker Date: Wed, 6 Mar 2024 17:50:41 +0100 Subject: [PATCH] Enable some PERF rules (#112498) Co-authored-by: Franck Nijhof --- homeassistant/components/insteon/utils.py | 2 +- homeassistant/components/matter/diagnostics.py | 2 +- homeassistant/components/mqtt/__init__.py | 2 +- homeassistant/components/prometheus/__init__.py | 2 +- homeassistant/components/systemmonitor/util.py | 2 +- homeassistant/components/velbus/cover.py | 5 +---- pyproject.toml | 3 +++ tests/components/bayesian/test_binary_sensor.py | 6 +++--- 8 files changed, 12 insertions(+), 12 deletions(-) diff --git a/homeassistant/components/insteon/utils.py b/homeassistant/components/insteon/utils.py index d7cbe676eee..1438b6c9c52 100644 --- a/homeassistant/components/insteon/utils.py +++ b/homeassistant/components/insteon/utils.py @@ -145,7 +145,7 @@ def add_insteon_events(hass: HomeAssistant, device: Device) -> None: for name_or_group, event in device.events.items(): if isinstance(name_or_group, int): - for _, event in device.events[name_or_group].items(): + for event in device.events[name_or_group].values(): _register_event(event, async_fire_insteon_event) else: _register_event(event, async_fire_insteon_event) diff --git a/homeassistant/components/matter/diagnostics.py b/homeassistant/components/matter/diagnostics.py index 8846a75b42a..d875f37dcae 100644 --- a/homeassistant/components/matter/diagnostics.py +++ b/homeassistant/components/matter/diagnostics.py @@ -21,7 +21,7 @@ def redact_matter_attributes(node_data: dict[str, Any]) -> dict[str, Any]: """Redact Matter cluster attribute.""" redacted = deepcopy(node_data) for attribute_to_redact in ATTRIBUTES_TO_REDACT: - for attribute_path, _value in redacted["attributes"].items(): + for attribute_path in redacted["attributes"]: _, cluster_id, attribute_id = parse_attribute_path(attribute_path) if cluster_id != attribute_to_redact.cluster_id: continue diff --git a/homeassistant/components/mqtt/__init__.py b/homeassistant/components/mqtt/__init__.py index 1412ad63e68..7da8636b19b 100644 --- a/homeassistant/components/mqtt/__init__.py +++ b/homeassistant/components/mqtt/__init__.py @@ -429,7 +429,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: ] await asyncio.gather(*tasks) - for _, component in mqtt_data.reload_handlers.items(): + for component in mqtt_data.reload_handlers.values(): component() # Fire event diff --git a/homeassistant/components/prometheus/__init__.py b/homeassistant/components/prometheus/__init__.py index 86163704797..317b5c569d4 100644 --- a/homeassistant/components/prometheus/__init__.py +++ b/homeassistant/components/prometheus/__init__.py @@ -259,7 +259,7 @@ class PrometheusMetrics: self, entity_id: str, friendly_name: str | None = None ) -> None: """Remove labelsets matching the given entity id from all metrics.""" - for _, metric in self._metrics.items(): + for metric in self._metrics.values(): for sample in cast(list[prometheus_client.Metric], metric.collect())[ 0 ].samples: diff --git a/homeassistant/components/systemmonitor/util.py b/homeassistant/components/systemmonitor/util.py index c67d4771ff4..75f8d2b2df1 100644 --- a/homeassistant/components/systemmonitor/util.py +++ b/homeassistant/components/systemmonitor/util.py @@ -57,7 +57,7 @@ def get_all_network_interfaces(hass: HomeAssistant) -> set[str]: """Return all network interfaces on system.""" psutil_wrapper: ha_psutil = hass.data[DOMAIN] interfaces: set[str] = set() - for interface, _ in psutil_wrapper.psutil.net_if_addrs().items(): + for interface in psutil_wrapper.psutil.net_if_addrs(): if interface.startswith("veth"): # Don't load docker virtual network interfaces continue diff --git a/homeassistant/components/velbus/cover.py b/homeassistant/components/velbus/cover.py index 46881fcdcaf..7c6f15a808d 100644 --- a/homeassistant/components/velbus/cover.py +++ b/homeassistant/components/velbus/cover.py @@ -26,10 +26,7 @@ async def async_setup_entry( """Set up Velbus switch based on config_entry.""" await hass.data[DOMAIN][entry.entry_id]["tsk"] cntrl = hass.data[DOMAIN][entry.entry_id]["cntrl"] - entities = [] - for channel in cntrl.get_all("cover"): - entities.append(VelbusCover(channel)) - async_add_entities(entities) + async_add_entities(VelbusCover(channel) for channel in cntrl.get_all("cover")) class VelbusCover(VelbusEntity, CoverEntity): diff --git a/pyproject.toml b/pyproject.toml index 765adc656d9..e50f633485b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -600,6 +600,9 @@ select = [ "N804", # First argument of a class method should be named cls "N805", # First argument of a method should be named self "N815", # Variable {name} in class scope should not be mixedCase + "PERF101", # Do not cast an iterable to list before iterating over it + "PERF102", # When using only the {subset} of a dict use the {subset}() method + "PERF203", # try-except within a loop incurs performance overhead "PGH004", # Use specific rule codes when using noqa "PLC0414", # Useless import alias. Import alias does not rename original package. "PLC", # pylint diff --git a/tests/components/bayesian/test_binary_sensor.py b/tests/components/bayesian/test_binary_sensor.py index e4c3e38695a..dba0769bc82 100644 --- a/tests/components/bayesian/test_binary_sensor.py +++ b/tests/components/bayesian/test_binary_sensor.py @@ -402,7 +402,7 @@ async def test_multiple_observations(hass: HomeAssistant) -> None: state = hass.states.get("binary_sensor.test_binary") - for _, attrs in state.attributes.items(): + for attrs in state.attributes.values(): json.dumps(attrs) assert state.attributes.get("occurred_observation_entities") == [] assert state.attributes.get("probability") == 0.2 @@ -474,7 +474,7 @@ async def test_multiple_numeric_observations(hass: HomeAssistant) -> None: state = hass.states.get("binary_sensor.test_binary") - for _, attrs in state.attributes.items(): + for attrs in state.attributes.values(): json.dumps(attrs) assert state.attributes.get("occurred_observation_entities") == [] assert state.attributes.get("probability") == 0.1 @@ -782,7 +782,7 @@ async def test_state_attributes_are_serializable(hass: HomeAssistant) -> None: state.attributes.get("occurred_observation_entities") ) - for _, attrs in state.attributes.items(): + for attrs in state.attributes.values(): json.dumps(attrs)