Fix ruff manual-dict-comprehension PERF403 (#120723)

* Fix PERF403

* Fix

* Fix
This commit is contained in:
Joost Lekkerkerker 2024-06-28 14:17:47 +02:00 committed by GitHub
parent c7906f90a3
commit 1fdd056c0e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
21 changed files with 234 additions and 223 deletions

View file

@ -363,15 +363,15 @@ class AuthManager:
local_only: bool | None = None,
) -> None:
"""Update a user."""
kwargs: dict[str, Any] = {}
kwargs: dict[str, Any] = {
attr_name: value
for attr_name, value in (
("name", name),
("group_ids", group_ids),
("local_only", local_only),
):
if value is not None:
kwargs[attr_name] = value
)
if value is not None
}
await self._store.async_update_user(user, **kwargs)
if is_active is not None:

View file

@ -105,14 +105,18 @@ class AuthStore:
"perm_lookup": self._perm_lookup,
}
kwargs.update(
{
attr_name: value
for attr_name, value in (
("is_owner", is_owner),
("is_active", is_active),
("local_only", local_only),
("system_generated", system_generated),
):
if value is not None:
kwargs[attr_name] = value
)
if value is not None
}
)
new_user = models.User(**kwargs)

View file

@ -304,6 +304,9 @@ async def async_update_pipeline(
updates.pop("id")
# Refactor this once we bump to Python 3.12
# and have https://peps.python.org/pep-0692/
updates.update(
{
key: val
for key, val in (
("conversation_engine", conversation_engine),
("conversation_language", conversation_language),
@ -316,9 +319,10 @@ async def async_update_pipeline(
("tts_voice", tts_voice),
("wake_word_entity", wake_word_entity),
("wake_word_id", wake_word_id),
):
if val is not UNDEFINED:
updates[key] = val
)
if val is not UNDEFINED
}
)
await pipeline_data.pipeline_store.async_update_item(pipeline.id, updates)

View file

@ -99,8 +99,7 @@ class Blueprint:
inputs = {}
for key, value in self.data[CONF_BLUEPRINT][CONF_INPUT].items():
if value and CONF_INPUT in value:
for key, value in value[CONF_INPUT].items():
inputs[key] = value
inputs.update(dict(value[CONF_INPUT]))
else:
inputs[key] = value
return inputs

View file

@ -180,6 +180,9 @@ class CloudPreferences:
"""Update user preferences."""
prefs = {**self._prefs}
prefs.update(
{
key: value
for key, value in (
(PREF_ENABLE_GOOGLE, google_enabled),
(PREF_ENABLE_ALEXA, alexa_enabled),
@ -195,9 +198,10 @@ class CloudPreferences:
(PREF_REMOTE_DOMAIN, remote_domain),
(PREF_GOOGLE_CONNECTED, google_connected),
(PREF_REMOTE_ALLOW_REMOTE_ENABLE, remote_allow_remote_enable),
):
if value is not UNDEFINED:
prefs[key] = value
)
if value is not UNDEFINED
}
)
await self._save_prefs(prefs)

View file

@ -188,7 +188,8 @@ class GdacsEvent(GeolocationEvent):
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the device state attributes."""
attributes = {}
return {
key: value
for key, value in (
(ATTR_EXTERNAL_ID, self._external_id),
(ATTR_DESCRIPTION, self._description),
@ -201,7 +202,6 @@ class GdacsEvent(GeolocationEvent):
(ATTR_POPULATION, self._population),
(ATTR_SEVERITY, self._severity),
(ATTR_VULNERABILITY, self._vulnerability),
):
if value or isinstance(value, bool):
attributes[key] = value
return attributes
)
if value or isinstance(value, bool)
}

View file

@ -133,7 +133,8 @@ class GdacsSensor(SensorEntity):
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the device state attributes."""
attributes: dict[str, Any] = {}
return {
key: value
for key, value in (
(ATTR_STATUS, self._status),
(ATTR_LAST_UPDATE, self._last_update),
@ -142,7 +143,6 @@ class GdacsSensor(SensorEntity):
(ATTR_CREATED, self._created),
(ATTR_UPDATED, self._updated),
(ATTR_REMOVED, self._removed),
):
if value or isinstance(value, bool):
attributes[key] = value
return attributes
)
if value or isinstance(value, bool)
}

View file

@ -156,7 +156,8 @@ class GeonetnzQuakesEvent(GeolocationEvent):
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the device state attributes."""
attributes = {}
return {
key: value
for key, value in (
(ATTR_EXTERNAL_ID, self._external_id),
(ATTR_DEPTH, self._depth),
@ -165,7 +166,6 @@ class GeonetnzQuakesEvent(GeolocationEvent):
(ATTR_MMI, self._mmi),
(ATTR_QUALITY, self._quality),
(ATTR_TIME, self._time),
):
if value or isinstance(value, bool):
attributes[key] = value
return attributes
)
if value or isinstance(value, bool)
}

View file

@ -137,7 +137,8 @@ class GeonetnzQuakesSensor(SensorEntity):
@property
def extra_state_attributes(self):
"""Return the device state attributes."""
attributes = {}
return {
key: value
for key, value in (
(ATTR_STATUS, self._status),
(ATTR_LAST_UPDATE, self._last_update),
@ -146,7 +147,6 @@ class GeonetnzQuakesSensor(SensorEntity):
(ATTR_CREATED, self._created),
(ATTR_UPDATED, self._updated),
(ATTR_REMOVED, self._removed),
):
if value or isinstance(value, bool):
attributes[key] = value
return attributes
)
if value or isinstance(value, bool)
}

View file

@ -154,7 +154,8 @@ class GeonetnzVolcanoSensor(SensorEntity):
@property
def extra_state_attributes(self):
"""Return the device state attributes."""
attributes = {}
return {
key: value
for key, value in (
(ATTR_EXTERNAL_ID, self._external_id),
(ATTR_ACTIVITY, self._activity),
@ -164,7 +165,6 @@ class GeonetnzVolcanoSensor(SensorEntity):
(ATTR_DISTANCE, self._distance),
(ATTR_LAST_UPDATE, self._feed_last_update),
(ATTR_LAST_UPDATE_SUCCESSFUL, self._feed_last_update_successful),
):
if value or isinstance(value, bool):
attributes[key] = value
return attributes
)
if value or isinstance(value, bool)
}

View file

@ -262,9 +262,13 @@ async def handle_devices_execute(
),
EXECUTE_LIMIT,
)
for entity_id, result in zip(executions, execute_results, strict=False):
if result is not None:
results[entity_id] = result
results.update(
{
entity_id: result
for entity_id, result in zip(executions, execute_results, strict=False)
if result is not None
}
)
except TimeoutError:
pass

View file

@ -426,10 +426,7 @@ class HTML5NotificationService(BaseNotificationService):
@property
def targets(self):
"""Return a dictionary of registered targets."""
targets = {}
for registration in self.registrations:
targets[registration] = registration
return targets
return {registration: registration for registration in self.registrations}
def dismiss(self, **kwargs):
"""Dismisses a notification."""

View file

@ -224,7 +224,8 @@ class IgnSismologiaLocationEvent(GeolocationEvent):
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the device state attributes."""
attributes = {}
return {
key: value
for key, value in (
(ATTR_EXTERNAL_ID, self._external_id),
(ATTR_TITLE, self._title),
@ -232,7 +233,6 @@ class IgnSismologiaLocationEvent(GeolocationEvent):
(ATTR_MAGNITUDE, self._magnitude),
(ATTR_PUBLICATION_DATE, self._publication_date),
(ATTR_IMAGE_URL, self._image_url),
):
if value or isinstance(value, bool):
attributes[key] = value
return attributes
)
if value or isinstance(value, bool)
}

View file

@ -106,18 +106,15 @@ class KaiterraAirQuality(AirQualityEntity):
@property
def extra_state_attributes(self):
"""Return the device state attributes."""
data = {}
attributes = [
return {
attr: value
for attr, value in (
(ATTR_VOC, self.volatile_organic_compounds),
(ATTR_AQI_LEVEL, self.air_quality_index_level),
(ATTR_AQI_POLLUTANT, self.air_quality_index_pollutant),
]
for attr, value in attributes:
if value is not None:
data[attr] = value
return data
)
if value is not None
}
async def async_added_to_hass(self):
"""Register callback."""

View file

@ -75,9 +75,11 @@ class KrakenOptionsFlowHandler(OptionsFlow):
tracked_asset_pairs = self.config_entry.options.get(
CONF_TRACKED_ASSET_PAIRS, []
)
for tracked_asset_pair in tracked_asset_pairs:
tradable_asset_pairs_for_multi_select[tracked_asset_pair] = (
tracked_asset_pair
tradable_asset_pairs_for_multi_select.update(
{
tracked_asset_pair: tracked_asset_pair
for tracked_asset_pair in tracked_asset_pairs
}
)
options = {

View file

@ -244,11 +244,7 @@ class MicrosoftFaceGroupEntity(Entity):
@property
def extra_state_attributes(self):
"""Return device specific state attributes."""
attr = {}
for name, p_id in self._api.store[self._id].items():
attr[name] = p_id
return attr
return dict(self._api.store[self._id])
class MicrosoftFace:

View file

@ -269,7 +269,8 @@ class NswRuralFireServiceLocationEvent(GeolocationEvent):
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the device state attributes."""
attributes = {}
return {
key: value
for key, value in (
(ATTR_EXTERNAL_ID, self._external_id),
(ATTR_CATEGORY, self._category),
@ -281,7 +282,6 @@ class NswRuralFireServiceLocationEvent(GeolocationEvent):
(ATTR_FIRE, self._fire),
(ATTR_SIZE, self._size),
(ATTR_RESPONSIBLE_AGENCY, self._responsible_agency),
):
if value or isinstance(value, bool):
attributes[key] = value
return attributes
)
if value or isinstance(value, bool)
}

View file

@ -223,14 +223,14 @@ class QldBushfireLocationEvent(GeolocationEvent):
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the device state attributes."""
attributes = {}
return {
key: value
for key, value in (
(ATTR_EXTERNAL_ID, self._external_id),
(ATTR_CATEGORY, self._category),
(ATTR_PUBLICATION_DATE, self._publication_date),
(ATTR_UPDATED_DATE, self._updated_date),
(ATTR_STATUS, self._status),
):
if value or isinstance(value, bool):
attributes[key] = value
return attributes
)
if value or isinstance(value, bool)
}

View file

@ -276,7 +276,8 @@ class UsgsEarthquakesEvent(GeolocationEvent):
@property
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the device state attributes."""
attributes = {}
return {
key: value
for key, value in (
(ATTR_EXTERNAL_ID, self._external_id),
(ATTR_PLACE, self._place),
@ -286,7 +287,6 @@ class UsgsEarthquakesEvent(GeolocationEvent):
(ATTR_STATUS, self._status),
(ATTR_TYPE, self._type),
(ATTR_ALERT, self._alert),
):
if value or isinstance(value, bool):
attributes[key] = value
return attributes
)
if value or isinstance(value, bool)
}

View file

@ -196,15 +196,19 @@ class ZWaveLock(ZWaveBaseEntity, LockEntity):
) -> None:
"""Set the lock configuration."""
params: dict[str, Any] = {"operation_type": operation_type}
params.update(
{
attr: val
for attr, val in (
("lock_timeout_configuration", lock_timeout),
("auto_relock_time", auto_relock_time),
("hold_and_release_time", hold_and_release_time),
("twist_assist", twist_assist),
("block_to_block", block_to_block),
):
if val is not None:
params[attr] = val
)
if val is not None
}
)
configuration = DoorLockCCConfigurationSetOptions(**params)
result = await set_configuration(
self.info.node.endpoints[self.info.primary_value.endpoint or 0],

View file

@ -315,17 +315,17 @@ class AreaRegistry(BaseRegistry[AreasRegistryStoreData]):
"""Update name of area."""
old = self.areas[area_id]
new_values = {}
new_values = {
attr_name: value
for attr_name, value in (
("aliases", aliases),
("icon", icon),
("labels", labels),
("picture", picture),
("floor_id", floor_id),
):
if value is not UNDEFINED and value != getattr(old, attr_name):
new_values[attr_name] = value
)
if value is not UNDEFINED and value != getattr(old, attr_name)
}
if name is not UNDEFINED and name != old.name:
new_values["name"] = name