Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add disallow_untyped_calls to mypy check. #15661

Merged
merged 3 commits into from
Jul 31, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions homeassistant/auth/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
11 changes: 7 additions & 4 deletions homeassistant/components/persistent_notification/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 [
Expand All @@ -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
Expand All @@ -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):
Expand Down
14 changes: 7 additions & 7 deletions homeassistant/config_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand All @@ -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(
Expand Down
13 changes: 5 additions & 8 deletions homeassistant/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not the same.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lambdas can't be typed and mypy doesn't always automatically can infer type.

The difference I see is that previously if there were several instances of ServiceRegistry - each would get their own IDs. Now the IDs are globally unique.

Any other difference?

Copy link
Member

@balloob balloob Jul 25, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling this function returns an iterator. In the old approach it would wrap that generator function in a lambda calling next on the return value, resulting in an ID being returned.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right. Wrapped the call in a next()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is also not going to work as it will generate a new generator each time it is called. However, may I suggest you leave it alone and I have a PR incoming in ~30 minutes that will a) blow your mind and b) remove that function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removing that function in #15674

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merged

cur_id = 1
while True:
yield '{}-{}'.format(id(self), cur_id)
cur_id += 1

@property
def services(self) -> Dict[str, Dict[str, Service]]:
Expand Down
4 changes: 3 additions & 1 deletion homeassistant/helpers/entity_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
from collections import OrderedDict
import fnmatch
import re
from typing import Dict

from homeassistant.core import split_entity_id


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
Expand Down
6 changes: 3 additions & 3 deletions homeassistant/helpers/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[mypy]
check_untyped_defs = true
disallow_untyped_calls = true
follow_imports = silent
ignore_missing_imports = true
warn_incomplete_stub = true
Expand All @@ -16,4 +17,5 @@ disallow_untyped_defs = false

[mypy-homeassistant.util.yaml]
warn_return_any = false
disallow_untyped_calls = false