From e8e84fb76427f797ae9efe26756622c1be5773c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Wed, 24 Jul 2019 23:18:40 +0300 Subject: [PATCH] Type check homeassistant.scripts (#25464) * Run mypy on homeassistant.scripts, disabling bunch of checks for now * Declare async_initialize in AuthProvider * Add some type hints * Remove unreachable code * Help mypy out * Script docstring fixes --- homeassistant/auth/providers/__init__.py | 4 ++++ homeassistant/scripts/__init__.py | 4 +++- homeassistant/scripts/auth.py | 2 ++ homeassistant/scripts/benchmark/__init__.py | 11 +++++++---- homeassistant/scripts/check_config.py | 17 ++++++++++------- homeassistant/scripts/credstash.py | 3 +++ homeassistant/scripts/ensure_config.py | 2 ++ homeassistant/scripts/keyring.py | 7 +++++-- homeassistant/scripts/macos/__init__.py | 2 ++ mypyrc | 1 + 10 files changed, 39 insertions(+), 14 deletions(-) diff --git a/homeassistant/auth/providers/__init__.py b/homeassistant/auth/providers/__init__.py index 8828782c886..83cbbf4ae9d 100644 --- a/homeassistant/auth/providers/__init__.py +++ b/homeassistant/auth/providers/__init__.py @@ -108,6 +108,10 @@ class AuthProvider: """ raise NotImplementedError + async def async_initialize(self) -> None: + """Initialize the auth provider.""" + pass + async def auth_provider_from_config( hass: HomeAssistant, store: AuthStore, diff --git a/homeassistant/scripts/__init__.py b/homeassistant/scripts/__init__.py index b0e7917a806..dcfd7810fef 100644 --- a/homeassistant/scripts/__init__.py +++ b/homeassistant/scripts/__init__.py @@ -14,6 +14,8 @@ from homeassistant.util.package import ( install_package, is_virtual_env, is_installed) +# mypy: allow-untyped-defs, allow-incomplete-defs, no-warn-return-any + def run(args: List) -> int: """Run a script.""" scripts = [] @@ -57,7 +59,7 @@ def run(args: List) -> int: print('Aborting script, could not install dependency', req) return 1 - return script.run(args[1:]) + return script.run(args[1:]) # type: ignore def extract_config_dir(args=None) -> str: diff --git a/homeassistant/scripts/auth.py b/homeassistant/scripts/auth.py index be57957ef8c..facc784f374 100644 --- a/homeassistant/scripts/auth.py +++ b/homeassistant/scripts/auth.py @@ -10,6 +10,8 @@ from homeassistant.core import HomeAssistant from homeassistant.config import get_default_config_dir +# mypy: allow-untyped-calls, allow-untyped-defs + def run(args): """Handle Home Assistant auth provider script.""" parser = argparse.ArgumentParser( diff --git a/homeassistant/scripts/benchmark/__init__.py b/homeassistant/scripts/benchmark/__init__.py index d159f7dcedd..01b8473b4db 100644 --- a/homeassistant/scripts/benchmark/__init__.py +++ b/homeassistant/scripts/benchmark/__init__.py @@ -5,17 +5,22 @@ from contextlib import suppress from datetime import datetime import logging from timeit import default_timer as timer +from typing import Callable, Dict from homeassistant import core from homeassistant.const import ( ATTR_NOW, EVENT_STATE_CHANGED, EVENT_TIME_CHANGED) from homeassistant.util import dt as dt_util -BENCHMARKS = {} + +# mypy: allow-untyped-calls, allow-untyped-defs, no-check-untyped-defs +# mypy: no-warn-return-any + +BENCHMARKS = {} # type: Dict[str, Callable] def run(args): - """Handle ensure configuration commandline script.""" + """Handle benchmark commandline script.""" # Disable logging logging.getLogger('homeassistant.core').setLevel(logging.CRITICAL) @@ -40,8 +45,6 @@ def run(args): loop.run_until_complete(hass.async_stop()) loop.close() - return 0 - def benchmark(func): """Decorate to mark a benchmark.""" diff --git a/homeassistant/scripts/check_config.py b/homeassistant/scripts/check_config.py index bb4f685d144..f4a7461bebc 100644 --- a/homeassistant/scripts/check_config.py +++ b/homeassistant/scripts/check_config.py @@ -5,7 +5,7 @@ import logging import os from collections import OrderedDict from glob import glob -from typing import Dict, List, Sequence +from typing import Dict, List, Sequence, Any, Tuple, Callable from unittest.mock import patch from homeassistant import bootstrap, core @@ -14,6 +14,9 @@ from homeassistant.helpers.check_config import async_check_ha_config_file import homeassistant.util.yaml.loader as yaml_loader from homeassistant.exceptions import HomeAssistantError + +# mypy: allow-untyped-calls, allow-untyped-defs + REQUIREMENTS = ('colorlog==4.0.2',) _LOGGER = logging.getLogger(__name__) @@ -24,7 +27,7 @@ MOCKS = { 'load*': ("homeassistant.config.load_yaml", yaml_loader.load_yaml), 'secrets': ("homeassistant.util.yaml.loader.secret_yaml", yaml_loader.secret_yaml), -} +} # type: Dict[str, Tuple[str, Callable]] SILENCE = ( 'homeassistant.scripts.check_config.yaml_loader.clear_secret_cache', ) @@ -49,7 +52,7 @@ def color(the_color, *args, reset=None): def run(script_args: List) -> int: - """Handle ensure config commandline script.""" + """Handle check config commandline script.""" parser = argparse.ArgumentParser( description="Check Home Assistant configuration.") parser.add_argument( @@ -81,7 +84,7 @@ def run(script_args: List) -> int: res = check(config_dir, args.secrets) - domain_info = [] + domain_info = [] # type: List[str] if args.info: domain_info = args.info.split(',') @@ -121,7 +124,7 @@ def run(script_args: List) -> int: dump_dict(res['components'].get(domain, None)) if args.secrets: - flatsecret = {} + flatsecret = {} # type: Dict[str, str] for sfn, sdict in res['secret_cache'].items(): sss = [] @@ -152,9 +155,9 @@ def check(config_dir, secrets=False): 'yaml_files': OrderedDict(), # yaml_files loaded 'secrets': OrderedDict(), # secret cache and secrets loaded 'except': OrderedDict(), # exceptions raised (with config) - 'components': None, # successful components + #'components' is a HomeAssistantConfig # noqa: E265 'secret_cache': None, - } + } # type: Dict[str, Any] # pylint: disable=possibly-unused-variable def mock_load(filename): diff --git a/homeassistant/scripts/credstash.py b/homeassistant/scripts/credstash.py index e2950f8d7a0..c75f71cdc02 100644 --- a/homeassistant/scripts/credstash.py +++ b/homeassistant/scripts/credstash.py @@ -4,6 +4,9 @@ import getpass from homeassistant.util.yaml import _SECRET_NAMESPACE + +# mypy: allow-untyped-defs + REQUIREMENTS = ['credstash==1.15.0'] diff --git a/homeassistant/scripts/ensure_config.py b/homeassistant/scripts/ensure_config.py index 068735d9e17..8778a50af1c 100644 --- a/homeassistant/scripts/ensure_config.py +++ b/homeassistant/scripts/ensure_config.py @@ -6,6 +6,8 @@ from homeassistant.core import HomeAssistant import homeassistant.config as config_util +# mypy: allow-untyped-calls, allow-untyped-defs + def run(args): """Handle ensure config commandline script.""" parser = argparse.ArgumentParser( diff --git a/homeassistant/scripts/keyring.py b/homeassistant/scripts/keyring.py index f7fa33aca37..281faadff58 100644 --- a/homeassistant/scripts/keyring.py +++ b/homeassistant/scripts/keyring.py @@ -5,6 +5,9 @@ import os from homeassistant.util.yaml import _SECRET_NAMESPACE + +# mypy: allow-untyped-defs + REQUIREMENTS = ['keyring==17.1.1', 'keyrings.alt==3.1.1'] @@ -39,9 +42,9 @@ def run(args): return 1 if args.action == 'set': - the_secret = getpass.getpass( + entered_secret = getpass.getpass( 'Please enter the secret for {}: '.format(args.name)) - keyring.set_password(_SECRET_NAMESPACE, args.name, the_secret) + keyring.set_password(_SECRET_NAMESPACE, args.name, entered_secret) print('Secret {} set successfully'.format(args.name)) elif args.action == 'get': the_secret = keyring.get_password(_SECRET_NAMESPACE, args.name) diff --git a/homeassistant/scripts/macos/__init__.py b/homeassistant/scripts/macos/__init__.py index 6c6557897ee..52c12c92904 100644 --- a/homeassistant/scripts/macos/__init__.py +++ b/homeassistant/scripts/macos/__init__.py @@ -3,6 +3,8 @@ import os import time +# mypy: allow-untyped-calls, allow-untyped-defs + def install_osx(): """Set up to run via launchd on OS X.""" with os.popen('which hass') as inp: diff --git a/mypyrc b/mypyrc index 0129a21a23f..ece4678abe6 100644 --- a/mypyrc +++ b/mypyrc @@ -1,4 +1,5 @@ homeassistant/*.py homeassistant/auth/ homeassistant/helpers/ +homeassistant/scripts/ homeassistant/util/