Type check all helpers (#25373)
* Type check all helpers, add inline exclusions for work in progress * Remove unused Script._template_cache * Add some missing type hints * Remove unneeded type: ignore * Type hint fixes * Mypy assistance tweaks * Don't look for None in deprecated config "at most once" check * Avoid None name slugify attempt when generating entity id * Avoid None state store attempt on entity remove
This commit is contained in:
parent
0653f57fb4
commit
d64f1e767c
19 changed files with 119 additions and 69 deletions
|
@ -27,6 +27,9 @@ from homeassistant.exceptions import TemplateError
|
|||
from homeassistant.helpers.logging import KeywordStyleAdapter
|
||||
from homeassistant.util import slugify as util_slugify
|
||||
|
||||
|
||||
# mypy: allow-incomplete-defs, allow-untyped-calls, allow-untyped-defs
|
||||
# mypy: no-check-untyped-defs, no-warn-return-any
|
||||
# pylint: disable=invalid-name
|
||||
|
||||
TIME_PERIOD_ERROR = "offset {} should be format 'HH:MM' or 'HH:MM:SS'"
|
||||
|
@ -92,7 +95,8 @@ def boolean(value: Any) -> bool:
|
|||
if value in ('0', 'false', 'no', 'off', 'disable'):
|
||||
return False
|
||||
elif isinstance(value, Number):
|
||||
return value != 0
|
||||
# type ignore: https://github.com/python/mypy/issues/3186
|
||||
return value != 0 # type: ignore
|
||||
raise vol.Invalid('invalid boolean value {}'.format(value))
|
||||
|
||||
|
||||
|
@ -161,7 +165,7 @@ def isdir(value: Any) -> str:
|
|||
return dir_in
|
||||
|
||||
|
||||
def ensure_list(value: Union[T, Sequence[T]]) -> Sequence[T]:
|
||||
def ensure_list(value: Union[T, Sequence[T], None]) -> Sequence[T]:
|
||||
"""Wrap value in list if it is not one."""
|
||||
if value is None:
|
||||
return []
|
||||
|
@ -556,7 +560,8 @@ def deprecated(key: str,
|
|||
else:
|
||||
# Unclear when it is None, but it happens, so let's guard.
|
||||
# https://github.com/home-assistant/home-assistant/issues/24982
|
||||
module_name = __name__
|
||||
# type ignore/unreachable: https://github.com/python/typeshed/pull/3137
|
||||
module_name = __name__ # type: ignore
|
||||
|
||||
if replacement_key and invalidation_version:
|
||||
warning = ("The '{key}' option (with value '{value}') is"
|
||||
|
@ -606,13 +611,15 @@ def deprecated(key: str,
|
|||
config.pop(key)
|
||||
else:
|
||||
value = default
|
||||
if (replacement_key
|
||||
and (replacement_key not in config
|
||||
or default == config.get(replacement_key))
|
||||
and value is not None):
|
||||
config[replacement_key] = value
|
||||
keys = [key]
|
||||
if replacement_key:
|
||||
keys.append(replacement_key)
|
||||
if value is not None and (
|
||||
replacement_key not in config or
|
||||
default == config.get(replacement_key)):
|
||||
config[replacement_key] = value
|
||||
|
||||
return has_at_most_one_key(key, replacement_key)(config)
|
||||
return has_at_most_one_key(*keys)(config)
|
||||
|
||||
return validator
|
||||
|
||||
|
@ -739,7 +746,7 @@ CONDITION_SCHEMA = vol.Any(
|
|||
ZONE_CONDITION_SCHEMA,
|
||||
AND_CONDITION_SCHEMA,
|
||||
OR_CONDITION_SCHEMA,
|
||||
)
|
||||
) # type: vol.Schema
|
||||
|
||||
_SCRIPT_DELAY_SCHEMA = vol.Schema({
|
||||
vol.Optional(CONF_ALIAS): string,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue