From 30e24117614f7ecb832ec72ba9d8a258de949b8f Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Fri, 18 Feb 2022 18:15:57 +0100 Subject: [PATCH] Add type ignore error codes [last ones] (#66816) --- homeassistant/components/automation/logbook.py | 4 ++-- homeassistant/components/sensor/__init__.py | 7 +++++-- homeassistant/helpers/collection.py | 4 ++-- homeassistant/helpers/entity.py | 5 +++-- homeassistant/helpers/entity_platform.py | 4 ++-- homeassistant/helpers/event.py | 8 ++++---- homeassistant/helpers/state.py | 4 ++-- homeassistant/helpers/sun.py | 2 +- homeassistant/helpers/trigger.py | 4 ++-- homeassistant/setup.py | 4 ++-- homeassistant/util/logging.py | 4 ++-- 11 files changed, 27 insertions(+), 23 deletions(-) diff --git a/homeassistant/components/automation/logbook.py b/homeassistant/components/automation/logbook.py index 901972595e4..86fb797ea31 100644 --- a/homeassistant/components/automation/logbook.py +++ b/homeassistant/components/automation/logbook.py @@ -8,11 +8,11 @@ from .const import DOMAIN @callback -def async_describe_events(hass: HomeAssistant, async_describe_event): # type: ignore +def async_describe_events(hass: HomeAssistant, async_describe_event): # type: ignore[no-untyped-def] """Describe logbook events.""" @callback - def async_describe_logbook_event(event: LazyEventPartialState): # type: ignore + def async_describe_logbook_event(event: LazyEventPartialState): # type: ignore[no-untyped-def] """Describe a logbook event.""" data = event.data message = "has been triggered" diff --git a/homeassistant/components/sensor/__init__.py b/homeassistant/components/sensor/__init__.py index f374ebaeb38..3414f13268f 100644 --- a/homeassistant/components/sensor/__init__.py +++ b/homeassistant/components/sensor/__init__.py @@ -397,7 +397,10 @@ class SensorEntity(Entity): # Received a date value if value is not None and device_class == DEVICE_CLASS_DATE: try: - return value.isoformat() # type: ignore + # We cast the value, to avoid using isinstance, but satisfy + # typechecking. The errors are guarded in this try. + value = cast(date, value) + return value.isoformat() except (AttributeError, TypeError) as err: raise ValueError( f"Invalid date: {self.entity_id} has a date device class " @@ -434,7 +437,7 @@ class SensorEntity(Entity): prec = len(value_s) - value_s.index(".") - 1 if "." in value_s else 0 # Suppress ValueError (Could not convert sensor_value to float) with suppress(ValueError): - temp = units.temperature(float(value), unit_of_measurement) # type: ignore + temp = units.temperature(float(value), unit_of_measurement) # type: ignore[arg-type] value = round(temp) if prec == 0 else round(temp, prec) return value diff --git a/homeassistant/helpers/collection.py b/homeassistant/helpers/collection.py index f6f9c968f10..9017c60c23f 100644 --- a/homeassistant/helpers/collection.py +++ b/homeassistant/helpers/collection.py @@ -330,7 +330,7 @@ def sync_entity_lifecycle( create_entity: Callable[[dict], Entity], ) -> None: """Map a collection to an entity component.""" - entities = {} + entities: dict[str, Entity] = {} ent_reg = entity_registry.async_get(hass) async def _add_entity(change_set: CollectionChangeSet) -> Entity: @@ -348,7 +348,7 @@ def sync_entity_lifecycle( entities.pop(change_set.item_id) async def _update_entity(change_set: CollectionChangeSet) -> None: - await entities[change_set.item_id].async_update_config(change_set.item) # type: ignore + await entities[change_set.item_id].async_update_config(change_set.item) # type: ignore[attr-defined] _func_map: dict[ str, Callable[[CollectionChangeSet], Coroutine[Any, Any, Entity | None]] diff --git a/homeassistant/helpers/entity.py b/homeassistant/helpers/entity.py index bb600556991..8e4f6bc8b58 100644 --- a/homeassistant/helpers/entity.py +++ b/homeassistant/helpers/entity.py @@ -707,10 +707,11 @@ class Entity(ABC): await self.parallel_updates.acquire() try: + task: asyncio.Future[None] if hasattr(self, "async_update"): - task = self.hass.async_create_task(self.async_update()) # type: ignore + task = self.hass.async_create_task(self.async_update()) # type: ignore[attr-defined] elif hasattr(self, "update"): - task = self.hass.async_add_executor_job(self.update) # type: ignore + task = self.hass.async_add_executor_job(self.update) # type: ignore[attr-defined] else: return diff --git a/homeassistant/helpers/entity_platform.py b/homeassistant/helpers/entity_platform.py index 799b209f16e..cc252f82782 100644 --- a/homeassistant/helpers/entity_platform.py +++ b/homeassistant/helpers/entity_platform.py @@ -172,7 +172,7 @@ class EntityPlatform: def async_create_setup_task() -> Coroutine: """Get task to set up platform.""" if getattr(platform, "async_setup_platform", None): - return platform.async_setup_platform( # type: ignore + return platform.async_setup_platform( # type: ignore[no-any-return,union-attr] hass, platform_config, self._async_schedule_add_entities, @@ -183,7 +183,7 @@ class EntityPlatform: # we don't want to track this task in case it blocks startup. return hass.loop.run_in_executor( # type: ignore[return-value] None, - platform.setup_platform, # type: ignore + platform.setup_platform, # type: ignore[union-attr] hass, platform_config, self._schedule_add_entities, diff --git a/homeassistant/helpers/event.py b/homeassistant/helpers/event.py index cbafd2e7e95..71b2cf1a585 100644 --- a/homeassistant/helpers/event.py +++ b/homeassistant/helpers/event.py @@ -1328,8 +1328,8 @@ def async_track_time_interval( interval: timedelta, ) -> CALLBACK_TYPE: """Add a listener that fires repetitively at every timedelta interval.""" - remove = None - interval_listener_job = None + remove: CALLBACK_TYPE + interval_listener_job: HassJob[None] job = HassJob(action) @@ -1344,7 +1344,7 @@ def async_track_time_interval( nonlocal interval_listener_job remove = async_track_point_in_utc_time( - hass, interval_listener_job, next_interval() # type: ignore + hass, interval_listener_job, next_interval() ) hass.async_run_hass_job(job, now) @@ -1353,7 +1353,7 @@ def async_track_time_interval( def remove_listener() -> None: """Remove interval listener.""" - remove() # type: ignore + remove() return remove_listener diff --git a/homeassistant/helpers/state.py b/homeassistant/helpers/state.py index 38647792b7a..75ca96ea246 100644 --- a/homeassistant/helpers/state.py +++ b/homeassistant/helpers/state.py @@ -102,12 +102,12 @@ async def async_reproduce_state( return try: - platform: ModuleType | None = integration.get_platform("reproduce_state") + platform: ModuleType = integration.get_platform("reproduce_state") except ImportError: _LOGGER.warning("Integration %s does not support reproduce state", domain) return - await platform.async_reproduce_states( # type: ignore + await platform.async_reproduce_states( hass, states_by_domain, context=context, reproduce_options=reproduce_options ) diff --git a/homeassistant/helpers/sun.py b/homeassistant/helpers/sun.py index 3c18dcc3278..09a329cd275 100644 --- a/homeassistant/helpers/sun.py +++ b/homeassistant/helpers/sun.py @@ -116,7 +116,7 @@ def get_astral_event_date( kwargs["observer_elevation"] = elevation try: - return getattr(location, event)(date, **kwargs) # type: ignore + return getattr(location, event)(date, **kwargs) # type: ignore[no-any-return] except ValueError: # Event never occurs for specified date. return None diff --git a/homeassistant/helpers/trigger.py b/homeassistant/helpers/trigger.py index 175a29fcc5d..0b18ad9aa42 100644 --- a/homeassistant/helpers/trigger.py +++ b/homeassistant/helpers/trigger.py @@ -83,7 +83,7 @@ async def async_initialize_triggers( triggers.append(platform.async_attach_trigger(hass, conf, action, info)) attach_results = await asyncio.gather(*triggers, return_exceptions=True) - removes = [] + removes: list[Callable[[], None]] = [] for result in attach_results: if isinstance(result, HomeAssistantError): @@ -103,7 +103,7 @@ async def async_initialize_triggers( log_cb(logging.INFO, "Initialized trigger") @callback - def remove_triggers(): # type: ignore + def remove_triggers() -> None: """Remove triggers.""" for remove in removes: remove() diff --git a/homeassistant/setup.py b/homeassistant/setup.py index 7b2f963102e..36292989dce 100644 --- a/homeassistant/setup.py +++ b/homeassistant/setup.py @@ -66,10 +66,10 @@ async def async_setup_component( if domain in hass.config.components: return True - setup_tasks = hass.data.setdefault(DATA_SETUP, {}) + setup_tasks: dict[str, asyncio.Task[bool]] = hass.data.setdefault(DATA_SETUP, {}) if domain in setup_tasks: - return await setup_tasks[domain] # type: ignore + return await setup_tasks[domain] task = setup_tasks[domain] = hass.async_create_task( _async_setup_component(hass, domain, config) diff --git a/homeassistant/util/logging.py b/homeassistant/util/logging.py index 8dba52ebe9d..d09feec5237 100644 --- a/homeassistant/util/logging.py +++ b/homeassistant/util/logging.py @@ -69,11 +69,11 @@ def async_activate_log_queue_handler(hass: HomeAssistant) -> None: This allows us to avoid blocking I/O and formatting messages in the event loop as log messages are written in another thread. """ - simple_queue = queue.SimpleQueue() # type: ignore + simple_queue: queue.SimpleQueue[logging.Handler] = queue.SimpleQueue() queue_handler = HomeAssistantQueueHandler(simple_queue) logging.root.addHandler(queue_handler) - migrated_handlers = [] + migrated_handlers: list[logging.Handler] = [] for handler in logging.root.handlers[:]: if handler is queue_handler: continue -- GitLab