From 268fc2be2090598245336b0275c16217a370c11b Mon Sep 17 00:00:00 2001 From: andrey-git Date: Tue, 24 Jul 2018 22:30:31 +0300 Subject: [PATCH 1/2] Add disallow_untyped_calls to mypy check. --- homeassistant/auth/__init__.py | 7 +++++-- homeassistant/components/__init__.py | 3 ++- .../components/persistent_notification/__init__.py | 11 +++++++---- homeassistant/config_entries.py | 14 +++++++------- homeassistant/core.py | 13 +++++-------- homeassistant/helpers/entity_values.py | 4 +++- homeassistant/helpers/signal.py | 6 +++--- mypy.ini | 2 ++ 8 files changed, 34 insertions(+), 26 deletions(-) diff --git a/homeassistant/auth/__init__.py b/homeassistant/auth/__init__.py index 62c416a988300d..35804cd8483cdc 100644 --- a/homeassistant/auth/__init__.py +++ b/homeassistant/auth/__init__.py @@ -2,9 +2,10 @@ import asyncio import logging from collections import OrderedDict +from typing import List, Awaitable from homeassistant import data_entry_flow -from homeassistant.core import callback +from homeassistant.core import callback, HomeAssistant from . import models from . import auth_store @@ -13,7 +14,9 @@ _LOGGER = logging.getLogger(__name__) -async def auth_manager_from_config(hass, provider_configs): +async def auth_manager_from_config( + hass: HomeAssistant, + provider_configs: List[dict]) -> Awaitable['AuthManager']: """Initialize an auth manager from config.""" store = auth_store.AuthStore(hass) if provider_configs: diff --git a/homeassistant/components/__init__.py b/homeassistant/components/__init__.py index f0c4f7bb3e2380..c08d979910d39d 100644 --- a/homeassistant/components/__init__.py +++ b/homeassistant/components/__init__.py @@ -10,6 +10,7 @@ import asyncio import itertools as it import logging +from typing import Awaitable import homeassistant.core as ha import homeassistant.config as conf_util @@ -109,7 +110,7 @@ def async_reload_core_config(hass): @asyncio.coroutine -def async_setup(hass, config): +def async_setup(hass: ha.HomeAssistant, config: dict) -> Awaitable[bool]: """Set up general services related to Home Assistant.""" @asyncio.coroutine def async_handle_turn_service(service): diff --git a/homeassistant/components/persistent_notification/__init__.py b/homeassistant/components/persistent_notification/__init__.py index cce3550d35c8f0..2850a5f96cd90b 100644 --- a/homeassistant/components/persistent_notification/__init__.py +++ b/homeassistant/components/persistent_notification/__init__.py @@ -6,10 +6,11 @@ """ import asyncio import logging +from typing import Awaitable import voluptuous as vol -from homeassistant.core import callback +from homeassistant.core import callback, HomeAssistant from homeassistant.exceptions import TemplateError from homeassistant.loader import bind_hass from homeassistant.helpers import config_validation as cv @@ -58,7 +59,8 @@ def dismiss(hass, notification_id): @callback @bind_hass -def async_create(hass, message, title=None, notification_id=None): +def async_create(hass: HomeAssistant, message: str, title: str = None, + notification_id: str = None) -> None: """Generate a notification.""" data = { key: value for key, value in [ @@ -68,7 +70,8 @@ def async_create(hass, message, title=None, notification_id=None): ] if value is not None } - hass.async_add_job(hass.services.async_call(DOMAIN, SERVICE_CREATE, data)) + hass.async_create_task( + hass.services.async_call(DOMAIN, SERVICE_CREATE, data)) @callback @@ -81,7 +84,7 @@ def async_dismiss(hass, notification_id): @asyncio.coroutine -def async_setup(hass, config): +def async_setup(hass: HomeAssistant, config: dict) -> Awaitable[bool]: """Set up the persistent notification component.""" @callback def create_service(call): diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index 8e2bb3fa5df9c4..12420e989eea74 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -113,7 +113,7 @@ async def async_step_discovery(info): import logging import uuid -from typing import Set, Optional # noqa pylint: disable=unused-import +from typing import Set, Optional, List # noqa pylint: disable=unused-import from homeassistant import data_entry_flow from homeassistant.core import callback, HomeAssistant @@ -270,19 +270,19 @@ class ConfigEntries: An instance of this object is available via `hass.config_entries`. """ - def __init__(self, hass, hass_config): + def __init__(self, hass: HomeAssistant, hass_config: dict) -> None: """Initialize the entry manager.""" self.hass = hass self.flow = data_entry_flow.FlowManager( hass, self._async_create_flow, self._async_finish_flow) self._hass_config = hass_config - self._entries = None + self._entries = [] # type: List[ConfigEntry] self._store = hass.helpers.storage.Store(STORAGE_VERSION, STORAGE_KEY) @callback - def async_domains(self): + def async_domains(self) -> List[str]: """Return domains for which we have entries.""" - seen = set() # type: Set[ConfigEntry] + seen = set() # type: Set[str] result = [] for entry in self._entries: @@ -293,7 +293,7 @@ def async_domains(self): return result @callback - def async_entries(self, domain=None): + def async_entries(self, domain: str = None) -> List[ConfigEntry]: """Return all entries or entries for a specific domain.""" if domain is None: return list(self._entries) @@ -319,7 +319,7 @@ async def async_remove(self, entry_id): 'require_restart': not unloaded } - async def async_load(self): + async def async_load(self) -> None: """Handle loading the config.""" # Migrating for config entries stored before 0.73 config = await self.hass.helpers.storage.async_migrator( diff --git a/homeassistant/core.py b/homeassistant/core.py index a7684d130ae496..0894f45a911b16 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -855,14 +855,11 @@ def __init__(self, hass: HomeAssistant) -> None: self._hass = hass self._async_unsub_call_event = None # type: Optional[CALLBACK_TYPE] - def _gen_unique_id() -> Iterator[str]: - cur_id = 1 - while True: - yield '{}-{}'.format(id(self), cur_id) - cur_id += 1 - - gen = _gen_unique_id() - self._generate_unique_id = lambda: next(gen) + def _generate_unique_id(self) -> Iterator[str]: + cur_id = 1 + while True: + yield '{}-{}'.format(id(self), cur_id) + cur_id += 1 @property def services(self) -> Dict[str, Dict[str, Service]]: diff --git a/homeassistant/helpers/entity_values.py b/homeassistant/helpers/entity_values.py index 5caa6b93131f24..77739f8adabbf8 100644 --- a/homeassistant/helpers/entity_values.py +++ b/homeassistant/helpers/entity_values.py @@ -2,6 +2,7 @@ from collections import OrderedDict import fnmatch import re +from typing import Dict from homeassistant.core import split_entity_id @@ -9,7 +10,8 @@ class EntityValues: """Class to store entity id based values.""" - def __init__(self, exact=None, domain=None, glob=None): + def __init__(self, exact: Dict = None, domain: Dict = None, + glob: Dict = None) -> None: """Initialize an EntityConfigDict.""" self._cache = {} self._exact = exact diff --git a/homeassistant/helpers/signal.py b/homeassistant/helpers/signal.py index 3ea52388d3388c..824b32177cdb54 100644 --- a/homeassistant/helpers/signal.py +++ b/homeassistant/helpers/signal.py @@ -3,7 +3,7 @@ import signal import sys -from homeassistant.core import callback +from homeassistant.core import callback, HomeAssistant from homeassistant.const import RESTART_EXIT_CODE from homeassistant.loader import bind_hass @@ -12,13 +12,13 @@ @callback @bind_hass -def async_register_signal_handling(hass): +def async_register_signal_handling(hass: HomeAssistant) -> None: """Register system signal handler for core.""" if sys.platform != 'win32': @callback def async_signal_handle(exit_code): """Wrap signal handling.""" - hass.async_add_job(hass.async_stop(exit_code)) + hass.async_create_task(hass.async_stop(exit_code)) try: hass.loop.add_signal_handler( diff --git a/mypy.ini b/mypy.ini index c92786e643fd5b..875aec5eda7993 100644 --- a/mypy.ini +++ b/mypy.ini @@ -1,5 +1,6 @@ [mypy] check_untyped_defs = true +disallow_untyped_calls = true follow_imports = silent ignore_missing_imports = true warn_incomplete_stub = true @@ -16,4 +17,5 @@ disallow_untyped_defs = false [mypy-homeassistant.util.yaml] warn_return_any = false +disallow_untyped_calls = false From 1cc1066294e7655009b38969b3dfb51a4cefeeda Mon Sep 17 00:00:00 2001 From: andrey-git Date: Wed, 25 Jul 2018 13:24:53 +0300 Subject: [PATCH 2/2] Fix generator --- homeassistant/core.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/homeassistant/core.py b/homeassistant/core.py index 0894f45a911b16..e72bae3792f876 100644 --- a/homeassistant/core.py +++ b/homeassistant/core.py @@ -995,7 +995,7 @@ async def async_call(self, domain: str, service: str, This method is a coroutine. """ - call_id = self._generate_unique_id() + call_id = next(self._generate_unique_id()) event_data = { ATTR_DOMAIN: domain.lower(),