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, local_only: bool | None = None,
) -> None: ) -> None:
"""Update a user.""" """Update a user."""
kwargs: dict[str, Any] = {} kwargs: dict[str, Any] = {
attr_name: value
for attr_name, value in ( for attr_name, value in (
("name", name), ("name", name),
("group_ids", group_ids), ("group_ids", group_ids),
("local_only", local_only), ("local_only", local_only),
): )
if value is not None: if value is not None
kwargs[attr_name] = value }
await self._store.async_update_user(user, **kwargs) await self._store.async_update_user(user, **kwargs)
if is_active is not None: if is_active is not None:

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -196,15 +196,19 @@ class ZWaveLock(ZWaveBaseEntity, LockEntity):
) -> None: ) -> None:
"""Set the lock configuration.""" """Set the lock configuration."""
params: dict[str, Any] = {"operation_type": operation_type} params: dict[str, Any] = {"operation_type": operation_type}
params.update(
{
attr: val
for attr, val in ( for attr, val in (
("lock_timeout_configuration", lock_timeout), ("lock_timeout_configuration", lock_timeout),
("auto_relock_time", auto_relock_time), ("auto_relock_time", auto_relock_time),
("hold_and_release_time", hold_and_release_time), ("hold_and_release_time", hold_and_release_time),
("twist_assist", twist_assist), ("twist_assist", twist_assist),
("block_to_block", block_to_block), ("block_to_block", block_to_block),
): )
if val is not None: if val is not None
params[attr] = val }
)
configuration = DoorLockCCConfigurationSetOptions(**params) configuration = DoorLockCCConfigurationSetOptions(**params)
result = await set_configuration( result = await set_configuration(
self.info.node.endpoints[self.info.primary_value.endpoint or 0], 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.""" """Update name of area."""
old = self.areas[area_id] old = self.areas[area_id]
new_values = {} new_values = {
attr_name: value
for attr_name, value in ( for attr_name, value in (
("aliases", aliases), ("aliases", aliases),
("icon", icon), ("icon", icon),
("labels", labels), ("labels", labels),
("picture", picture), ("picture", picture),
("floor_id", floor_id), ("floor_id", floor_id),
): )
if value is not UNDEFINED and value != getattr(old, attr_name): if value is not UNDEFINED and value != getattr(old, attr_name)
new_values[attr_name] = value }
if name is not UNDEFINED and name != old.name: if name is not UNDEFINED and name != old.name:
new_values["name"] = name new_values["name"] = name