From aa508b5106d0fd650dba8de1c7373f2ee6abe786 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Thu, 15 Aug 2019 18:53:25 +0300 Subject: [PATCH] Complete some incomplete type hints in helpers (#25953) --- homeassistant/helpers/check_config.py | 3 +-- homeassistant/helpers/config_entry_flow.py | 7 +++++-- homeassistant/helpers/device_registry.py | 6 ++++-- homeassistant/helpers/entity.py | 11 +++++------ homeassistant/helpers/entity_registry.py | 6 ++++-- homeassistant/helpers/service.py | 5 +++-- homeassistant/helpers/storage.py | 12 ++++++------ 7 files changed, 28 insertions(+), 22 deletions(-) diff --git a/homeassistant/helpers/check_config.py b/homeassistant/helpers/check_config.py index 331599e9b0f..bc39d5d5720 100644 --- a/homeassistant/helpers/check_config.py +++ b/homeassistant/helpers/check_config.py @@ -27,8 +27,7 @@ import homeassistant.util.yaml.loader as yaml_loader from homeassistant.exceptions import HomeAssistantError -# mypy: allow-incomplete-defs, allow-untyped-calls, allow-untyped-defs -# mypy: no-warn-return-any +# mypy: allow-untyped-calls, allow-untyped-defs, no-warn-return-any CheckConfigError = namedtuple("CheckConfigError", "message domain config") diff --git a/homeassistant/helpers/config_entry_flow.py b/homeassistant/helpers/config_entry_flow.py index 28b2fb495d0..f341cc2ce02 100644 --- a/homeassistant/helpers/config_entry_flow.py +++ b/homeassistant/helpers/config_entry_flow.py @@ -2,9 +2,10 @@ from functools import partial from homeassistant import config_entries +from .typing import HomeAssistantType -# mypy: allow-incomplete-defs, allow-untyped-defs +# mypy: allow-untyped-defs def register_discovery_flow(domain, title, discovery_function, connection_class): @@ -130,7 +131,9 @@ class WebhookFlowHandler(config_entries.ConfigFlow): ) -async def webhook_async_remove_entry(hass, entry) -> None: +async def webhook_async_remove_entry( + hass: HomeAssistantType, entry: config_entries.ConfigEntry +) -> None: """Remove a webhook config entry.""" if not entry.data.get("cloudhook") or "cloud" not in hass.config.components: return diff --git a/homeassistant/helpers/device_registry.py b/homeassistant/helpers/device_registry.py index a4d83c4eba9..19b4a1333b6 100644 --- a/homeassistant/helpers/device_registry.py +++ b/homeassistant/helpers/device_registry.py @@ -13,7 +13,7 @@ from homeassistant.loader import bind_hass from .typing import HomeAssistantType -# mypy: allow-incomplete-defs, allow-untyped-calls, allow-untyped-defs +# mypy: allow-untyped-calls, allow-untyped-defs # mypy: no-check-untyped-defs, no-warn-return-any _LOGGER = logging.getLogger(__name__) @@ -84,7 +84,9 @@ class DeviceRegistry: return self.devices.get(device_id) @callback - def async_get_device(self, identifiers: set, connections: set): + def async_get_device( + self, identifiers: set, connections: set + ) -> Optional[DeviceEntry]: """Check if device is registered.""" for device in self.devices.values(): if any(iden in device.identifiers for iden in identifiers) or any( diff --git a/homeassistant/helpers/entity.py b/homeassistant/helpers/entity.py index fa4352822ed..6508de08143 100644 --- a/homeassistant/helpers/entity.py +++ b/homeassistant/helpers/entity.py @@ -3,7 +3,7 @@ from datetime import timedelta import logging import functools as ft from timeit import default_timer as timer -from typing import Optional, List, Iterable +from typing import Any, Optional, List, Iterable from homeassistant.const import ( ATTR_ASSUMED_STATE, @@ -34,8 +34,7 @@ from homeassistant.util.async_ import run_callback_threadsafe from homeassistant.util import dt as dt_util -# mypy: allow-incomplete-defs, allow-untyped-defs, no-check-untyped-defs -# mypy: no-warn-return-any +# mypy: allow-untyped-defs, no-check-untyped-defs, no-warn-return-any _LOGGER = logging.getLogger(__name__) SLOW_UPDATE_WARNING = 10 @@ -532,7 +531,7 @@ class ToggleEntity(Entity): """Return True if entity is on.""" raise NotImplementedError() - def turn_on(self, **kwargs) -> None: + def turn_on(self, **kwargs: Any) -> None: """Turn the entity on.""" raise NotImplementedError() @@ -543,7 +542,7 @@ class ToggleEntity(Entity): """ return self.hass.async_add_job(ft.partial(self.turn_on, **kwargs)) - def turn_off(self, **kwargs) -> None: + def turn_off(self, **kwargs: Any) -> None: """Turn the entity off.""" raise NotImplementedError() @@ -554,7 +553,7 @@ class ToggleEntity(Entity): """ return self.hass.async_add_job(ft.partial(self.turn_off, **kwargs)) - def toggle(self, **kwargs) -> None: + def toggle(self, **kwargs: Any) -> None: """Toggle the entity.""" if self.is_on: self.turn_off(**kwargs) diff --git a/homeassistant/helpers/entity_registry.py b/homeassistant/helpers/entity_registry.py index bb546ba7b83..9a7be9ecc36 100644 --- a/homeassistant/helpers/entity_registry.py +++ b/homeassistant/helpers/entity_registry.py @@ -24,7 +24,7 @@ from homeassistant.util.yaml import load_yaml from .typing import HomeAssistantType -# mypy: allow-incomplete-defs, allow-untyped-calls, allow-untyped-defs +# mypy: allow-untyped-calls, allow-untyped-defs # mypy: no-check-untyped-defs, no-warn-return-any PATH_REGISTRY = "entity_registry.yaml" @@ -91,7 +91,9 @@ class EntityRegistry: return self.entities.get(entity_id) @callback - def async_get_entity_id(self, domain: str, platform: str, unique_id: str): + def async_get_entity_id( + self, domain: str, platform: str, unique_id: str + ) -> Optional[str]: """Check if an entity_id is currently registered.""" for entity in self.entities.values(): if ( diff --git a/homeassistant/helpers/service.py b/homeassistant/helpers/service.py index d702fce8d8b..07e070df8c5 100644 --- a/homeassistant/helpers/service.py +++ b/homeassistant/helpers/service.py @@ -18,12 +18,13 @@ from homeassistant.exceptions import ( from homeassistant.helpers import template, typing from homeassistant.loader import async_get_integration, bind_hass from homeassistant.util.yaml import load_yaml +from homeassistant.util.yaml.loader import JSON_TYPE import homeassistant.helpers.config_validation as cv from homeassistant.util.async_ import run_coroutine_threadsafe from homeassistant.helpers.typing import HomeAssistantType -# mypy: allow-incomplete-defs, allow-untyped-defs, no-check-untyped-defs +# mypy: allow-untyped-defs, no-check-untyped-defs CONF_SERVICE = "service" CONF_SERVICE_TEMPLATE = "service_template" @@ -161,7 +162,7 @@ async def async_extract_entity_ids(hass, service_call, expand_group=True): return extracted -async def _load_services_file(hass: HomeAssistantType, domain: str): +async def _load_services_file(hass: HomeAssistantType, domain: str) -> JSON_TYPE: """Load services file for an integration.""" integration = await async_get_integration(hass, domain) try: diff --git a/homeassistant/helpers/storage.py b/homeassistant/helpers/storage.py index 5bb912adafe..368753cd626 100644 --- a/homeassistant/helpers/storage.py +++ b/homeassistant/helpers/storage.py @@ -6,14 +6,13 @@ import os from typing import Dict, List, Optional, Callable, Union, Any, Type from homeassistant.const import EVENT_HOMEASSISTANT_STOP -from homeassistant.core import callback +from homeassistant.core import HomeAssistant, callback from homeassistant.loader import bind_hass from homeassistant.util import json as json_util from homeassistant.helpers.event import async_call_later -# mypy: allow-incomplete-defs, allow-untyped-calls, allow-untyped-defs -# mypy: no-warn-return-any +# mypy: allow-untyped-calls, allow-untyped-defs, no-warn-return-any STORAGE_DIR = ".storage" _LOGGER = logging.getLogger(__name__) @@ -59,7 +58,7 @@ class Store: def __init__( self, - hass, + hass: HomeAssistant, version: int, key: str, private: bool = False, @@ -94,6 +93,7 @@ class Store: """ if self._load_task is None: self._load_task = self.hass.async_add_job(self._async_load()) + assert self._load_task is not None return await self._load_task @@ -138,7 +138,7 @@ class Store: @callback def async_delay_save( self, data_func: Callable[[], Dict], delay: Optional[int] = None - ): + ) -> None: """Save data with an optional delay.""" self._data = {"version": self.version, "key": self.key, "data_func": data_func} @@ -201,7 +201,7 @@ class Store: except (json_util.SerializationError, json_util.WriteError) as err: _LOGGER.error("Error writing config for %s: %s", self.key, err) - def _write_data(self, path: str, data: Dict): + def _write_data(self, path: str, data: Dict) -> None: """Write the data.""" if not os.path.isdir(os.path.dirname(path)): os.makedirs(os.path.dirname(path))