From 82607977efaa385e6097509ada5a1153f95214b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Mon, 8 Feb 2021 12:59:46 +0200 Subject: [PATCH] Various type hint improvements (#46144) --- homeassistant/config.py | 5 +++-- homeassistant/helpers/area_registry.py | 4 +++- homeassistant/helpers/discovery.py | 13 ++++++++++--- homeassistant/helpers/entity_component.py | 4 +++- homeassistant/helpers/reload.py | 8 ++++---- homeassistant/helpers/script.py | 2 +- homeassistant/helpers/service.py | 8 ++++++-- homeassistant/helpers/update_coordinator.py | 6 ++++-- 8 files changed, 34 insertions(+), 16 deletions(-) diff --git a/homeassistant/config.py b/homeassistant/config.py index 2da9b0331c9..5cd4a0700e5 100644 --- a/homeassistant/config.py +++ b/homeassistant/config.py @@ -51,6 +51,7 @@ from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import config_per_platform, extract_domain_configs import homeassistant.helpers.config_validation as cv from homeassistant.helpers.entity_values import EntityValues +from homeassistant.helpers.typing import ConfigType from homeassistant.loader import Integration, IntegrationNotFound from homeassistant.requirements import ( RequirementsNotFound, @@ -734,8 +735,8 @@ async def merge_packages_config( async def async_process_component_config( - hass: HomeAssistant, config: Dict, integration: Integration -) -> Optional[Dict]: + hass: HomeAssistant, config: ConfigType, integration: Integration +) -> Optional[ConfigType]: """Check component configuration and return processed configuration. Returns None on error. diff --git a/homeassistant/helpers/area_registry.py b/homeassistant/helpers/area_registry.py index 1a919996f86..bdd231686e2 100644 --- a/homeassistant/helpers/area_registry.py +++ b/homeassistant/helpers/area_registry.py @@ -11,6 +11,8 @@ from homeassistant.util import slugify from .typing import HomeAssistantType +# mypy: disallow-any-generics + DATA_REGISTRY = "area_registry" EVENT_AREA_REGISTRY_UPDATED = "area_registry_updated" STORAGE_KEY = "core.area_registry" @@ -25,7 +27,7 @@ class AreaEntry: name: str = attr.ib() id: Optional[str] = attr.ib(default=None) - def generate_id(self, existing_ids: Container) -> None: + def generate_id(self, existing_ids: Container[str]) -> None: """Initialize ID.""" suggestion = suggestion_base = slugify(self.name) tries = 1 diff --git a/homeassistant/helpers/discovery.py b/homeassistant/helpers/discovery.py index acde8d73a50..0770e6798f1 100644 --- a/homeassistant/helpers/discovery.py +++ b/homeassistant/helpers/discovery.py @@ -9,6 +9,7 @@ from typing import Any, Callable, Collection, Dict, Optional, Union from homeassistant import core, setup from homeassistant.const import ATTR_DISCOVERED, ATTR_SERVICE, EVENT_PLATFORM_DISCOVERED +from homeassistant.core import CALLBACK_TYPE from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.loader import bind_hass from homeassistant.util.async_ import run_callback_threadsafe @@ -16,10 +17,14 @@ from homeassistant.util.async_ import run_callback_threadsafe EVENT_LOAD_PLATFORM = "load_platform.{}" ATTR_PLATFORM = "platform" +# mypy: disallow-any-generics + @bind_hass def listen( - hass: core.HomeAssistant, service: Union[str, Collection[str]], callback: Callable + hass: core.HomeAssistant, + service: Union[str, Collection[str]], + callback: CALLBACK_TYPE, ) -> None: """Set up listener for discovery of specific service. @@ -31,7 +36,9 @@ def listen( @core.callback @bind_hass def async_listen( - hass: core.HomeAssistant, service: Union[str, Collection[str]], callback: Callable + hass: core.HomeAssistant, + service: Union[str, Collection[str]], + callback: CALLBACK_TYPE, ) -> None: """Set up listener for discovery of specific service. @@ -94,7 +101,7 @@ async def async_discover( @bind_hass def listen_platform( - hass: core.HomeAssistant, component: str, callback: Callable + hass: core.HomeAssistant, component: str, callback: CALLBACK_TYPE ) -> None: """Register a platform loader listener.""" run_callback_threadsafe( diff --git a/homeassistant/helpers/entity_component.py b/homeassistant/helpers/entity_component.py index 0f1f04e3aec..6fb8696d845 100644 --- a/homeassistant/helpers/entity_component.py +++ b/homeassistant/helpers/entity_component.py @@ -272,7 +272,9 @@ class EntityComponent: if found: await found.async_remove_entity(entity_id) - async def async_prepare_reload(self, *, skip_reset: bool = False) -> Optional[dict]: + async def async_prepare_reload( + self, *, skip_reset: bool = False + ) -> Optional[ConfigType]: """Prepare reloading this entity component. This method must be run in the event loop. diff --git a/homeassistant/helpers/reload.py b/homeassistant/helpers/reload.py index e596027b7e1..8ff454eab6f 100644 --- a/homeassistant/helpers/reload.py +++ b/homeassistant/helpers/reload.py @@ -2,7 +2,7 @@ import asyncio import logging -from typing import Any, Dict, Iterable, List, Optional +from typing import Dict, Iterable, List, Optional from homeassistant import config as conf_util from homeassistant.const import SERVICE_RELOAD @@ -10,7 +10,7 @@ from homeassistant.core import Event, callback from homeassistant.exceptions import HomeAssistantError from homeassistant.helpers import config_per_platform from homeassistant.helpers.entity_platform import EntityPlatform, async_get_platforms -from homeassistant.helpers.typing import HomeAssistantType +from homeassistant.helpers.typing import ConfigType, HomeAssistantType from homeassistant.loader import async_get_integration from homeassistant.setup import async_setup_component @@ -49,7 +49,7 @@ async def _resetup_platform( hass: HomeAssistantType, integration_name: str, integration_platform: str, - unprocessed_conf: Dict, + unprocessed_conf: ConfigType, ) -> None: """Resetup a platform.""" integration = await async_get_integration(hass, integration_platform) @@ -129,7 +129,7 @@ async def _async_reconfig_platform( async def async_integration_yaml_config( hass: HomeAssistantType, integration_name: str -) -> Optional[Dict[Any, Any]]: +) -> Optional[ConfigType]: """Fetch the latest yaml configuration for an integration.""" integration = await async_get_integration(hass, integration_name) diff --git a/homeassistant/helpers/script.py b/homeassistant/helpers/script.py index 8706f765b50..56accf9cf49 100644 --- a/homeassistant/helpers/script.py +++ b/homeassistant/helpers/script.py @@ -779,7 +779,7 @@ async def _async_stop_scripts_at_shutdown(hass, event): _VarsType = Union[Dict[str, Any], MappingProxyType] -def _referenced_extract_ids(data: Dict, key: str, found: Set[str]) -> None: +def _referenced_extract_ids(data: Dict[str, Any], key: str, found: Set[str]) -> None: """Extract referenced IDs.""" if not data: return diff --git a/homeassistant/helpers/service.py b/homeassistant/helpers/service.py index c95f942c6dc..c83fa4a7763 100644 --- a/homeassistant/helpers/service.py +++ b/homeassistant/helpers/service.py @@ -669,10 +669,14 @@ def async_register_admin_service( @bind_hass @ha.callback -def verify_domain_control(hass: HomeAssistantType, domain: str) -> Callable: +def verify_domain_control( + hass: HomeAssistantType, domain: str +) -> Callable[[Callable[[ha.ServiceCall], Any]], Callable[[ha.ServiceCall], Any]]: """Ensure permission to access any entity under domain in service call.""" - def decorator(service_handler: Callable[[ha.ServiceCall], Any]) -> Callable: + def decorator( + service_handler: Callable[[ha.ServiceCall], Any] + ) -> Callable[[ha.ServiceCall], Any]: """Decorate.""" if not asyncio.iscoroutinefunction(service_handler): raise HomeAssistantError("Can only decorate async functions.") diff --git a/homeassistant/helpers/update_coordinator.py b/homeassistant/helpers/update_coordinator.py index 8df2c57b1e7..b2424a06927 100644 --- a/homeassistant/helpers/update_coordinator.py +++ b/homeassistant/helpers/update_coordinator.py @@ -3,7 +3,7 @@ import asyncio from datetime import datetime, timedelta import logging from time import monotonic -from typing import Awaitable, Callable, Generic, List, Optional, TypeVar +from typing import Any, Awaitable, Callable, Generic, List, Optional, TypeVar import urllib.error import aiohttp @@ -21,6 +21,8 @@ REQUEST_REFRESH_DEFAULT_IMMEDIATE = True T = TypeVar("T") +# mypy: disallow-any-generics + class UpdateFailed(Exception): """Raised when an update has failed.""" @@ -231,7 +233,7 @@ class DataUpdateCoordinator(Generic[T]): class CoordinatorEntity(entity.Entity): """A class for entities using DataUpdateCoordinator.""" - def __init__(self, coordinator: DataUpdateCoordinator) -> None: + def __init__(self, coordinator: DataUpdateCoordinator[Any]) -> None: """Create the entity with a DataUpdateCoordinator.""" self.coordinator = coordinator