Avoid some implicit generic Anys (#54577)

This commit is contained in:
Ville Skyttä 2021-08-17 00:12:06 +03:00 committed by GitHub
parent b72ed68d61
commit 848c0be58a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 69 additions and 29 deletions

View File

@ -17,6 +17,8 @@ from .const import GROUP_ID_ADMIN, GROUP_ID_READ_ONLY, GROUP_ID_USER
from .permissions import PermissionLookup, system_policies from .permissions import PermissionLookup, system_policies
from .permissions.types import PolicyType from .permissions.types import PolicyType
# mypy: disallow-any-generics
STORAGE_VERSION = 1 STORAGE_VERSION = 1
STORAGE_KEY = "auth" STORAGE_KEY = "auth"
GROUP_NAME_ADMIN = "Administrators" GROUP_NAME_ADMIN = "Administrators"
@ -491,7 +493,7 @@ class AuthStore:
self._store.async_delay_save(self._data_to_save, 1) self._store.async_delay_save(self._data_to_save, 1)
@callback @callback
def _data_to_save(self) -> dict: def _data_to_save(self) -> dict[str, list[dict[str, Any]]]:
"""Return the data to store.""" """Return the data to store."""
assert self._users is not None assert self._users is not None
assert self._groups is not None assert self._groups is not None

View File

@ -22,6 +22,8 @@ from ..auth_store import AuthStore
from ..const import MFA_SESSION_EXPIRATION from ..const import MFA_SESSION_EXPIRATION
from ..models import Credentials, RefreshToken, User, UserMeta from ..models import Credentials, RefreshToken, User, UserMeta
# mypy: disallow-any-generics
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
DATA_REQS = "auth_prov_reqs_processed" DATA_REQS = "auth_prov_reqs_processed"
@ -96,7 +98,7 @@ class AuthProvider:
# Implement by extending class # Implement by extending class
async def async_login_flow(self, context: dict | None) -> LoginFlow: async def async_login_flow(self, context: dict[str, Any] | None) -> LoginFlow:
"""Return the data flow for logging in with auth provider. """Return the data flow for logging in with auth provider.
Auth provider should extend LoginFlow and return an instance. Auth provider should extend LoginFlow and return an instance.

View File

@ -17,6 +17,8 @@ from homeassistant.exceptions import HomeAssistantError
from . import AUTH_PROVIDER_SCHEMA, AUTH_PROVIDERS, AuthProvider, LoginFlow from . import AUTH_PROVIDER_SCHEMA, AUTH_PROVIDERS, AuthProvider, LoginFlow
from ..models import Credentials, UserMeta from ..models import Credentials, UserMeta
# mypy: disallow-any-generics
CONF_ARGS = "args" CONF_ARGS = "args"
CONF_META = "meta" CONF_META = "meta"
@ -56,7 +58,7 @@ class CommandLineAuthProvider(AuthProvider):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self._user_meta: dict[str, dict[str, Any]] = {} self._user_meta: dict[str, dict[str, Any]] = {}
async def async_login_flow(self, context: dict | None) -> LoginFlow: async def async_login_flow(self, context: dict[str, Any] | None) -> LoginFlow:
"""Return a flow to login.""" """Return a flow to login."""
return CommandLineLoginFlow(self) return CommandLineLoginFlow(self)

View File

@ -19,6 +19,8 @@ from homeassistant.exceptions import HomeAssistantError
from . import AUTH_PROVIDER_SCHEMA, AUTH_PROVIDERS, AuthProvider, LoginFlow from . import AUTH_PROVIDER_SCHEMA, AUTH_PROVIDERS, AuthProvider, LoginFlow
from ..models import Credentials, UserMeta from ..models import Credentials, UserMeta
# mypy: disallow-any-generics
STORAGE_VERSION = 1 STORAGE_VERSION = 1
STORAGE_KEY = "auth_provider.homeassistant" STORAGE_KEY = "auth_provider.homeassistant"
@ -235,7 +237,7 @@ class HassAuthProvider(AuthProvider):
await data.async_load() await data.async_load()
self.data = data self.data = data
async def async_login_flow(self, context: dict | None) -> LoginFlow: async def async_login_flow(self, context: dict[str, Any] | None) -> LoginFlow:
"""Return a flow to login.""" """Return a flow to login."""
return HassLoginFlow(self) return HassLoginFlow(self)

View File

@ -4,7 +4,7 @@ from __future__ import annotations
from collections import OrderedDict from collections import OrderedDict
from collections.abc import Mapping from collections.abc import Mapping
import hmac import hmac
from typing import cast from typing import Any, cast
import voluptuous as vol import voluptuous as vol
@ -15,6 +15,8 @@ from homeassistant.exceptions import HomeAssistantError
from . import AUTH_PROVIDER_SCHEMA, AUTH_PROVIDERS, AuthProvider, LoginFlow from . import AUTH_PROVIDER_SCHEMA, AUTH_PROVIDERS, AuthProvider, LoginFlow
from ..models import Credentials, UserMeta from ..models import Credentials, UserMeta
# mypy: disallow-any-generics
USER_SCHEMA = vol.Schema( USER_SCHEMA = vol.Schema(
{ {
vol.Required("username"): str, vol.Required("username"): str,
@ -37,7 +39,7 @@ class InvalidAuthError(HomeAssistantError):
class ExampleAuthProvider(AuthProvider): class ExampleAuthProvider(AuthProvider):
"""Example auth provider based on hardcoded usernames and passwords.""" """Example auth provider based on hardcoded usernames and passwords."""
async def async_login_flow(self, context: dict | None) -> LoginFlow: async def async_login_flow(self, context: dict[str, Any] | None) -> LoginFlow:
"""Return a flow to login.""" """Return a flow to login."""
return ExampleLoginFlow(self) return ExampleLoginFlow(self)

View File

@ -7,7 +7,7 @@ from __future__ import annotations
from collections.abc import Mapping from collections.abc import Mapping
import hmac import hmac
from typing import cast from typing import Any, cast
import voluptuous as vol import voluptuous as vol
@ -19,6 +19,8 @@ import homeassistant.helpers.config_validation as cv
from . import AUTH_PROVIDER_SCHEMA, AUTH_PROVIDERS, AuthProvider, LoginFlow from . import AUTH_PROVIDER_SCHEMA, AUTH_PROVIDERS, AuthProvider, LoginFlow
from ..models import Credentials, UserMeta from ..models import Credentials, UserMeta
# mypy: disallow-any-generics
AUTH_PROVIDER_TYPE = "legacy_api_password" AUTH_PROVIDER_TYPE = "legacy_api_password"
CONF_API_PASSWORD = "api_password" CONF_API_PASSWORD = "api_password"
@ -44,7 +46,7 @@ class LegacyApiPasswordAuthProvider(AuthProvider):
"""Return api_password.""" """Return api_password."""
return str(self.config[CONF_API_PASSWORD]) return str(self.config[CONF_API_PASSWORD])
async def async_login_flow(self, context: dict | None) -> LoginFlow: async def async_login_flow(self, context: dict[str, Any] | None) -> LoginFlow:
"""Return a flow to login.""" """Return a flow to login."""
return LegacyLoginFlow(self) return LegacyLoginFlow(self)

View File

@ -27,6 +27,8 @@ from . import AUTH_PROVIDER_SCHEMA, AUTH_PROVIDERS, AuthProvider, LoginFlow
from .. import InvalidAuthError from .. import InvalidAuthError
from ..models import Credentials, RefreshToken, UserMeta from ..models import Credentials, RefreshToken, UserMeta
# mypy: disallow-any-generics
IPAddress = Union[IPv4Address, IPv6Address] IPAddress = Union[IPv4Address, IPv6Address]
IPNetwork = Union[IPv4Network, IPv6Network] IPNetwork = Union[IPv4Network, IPv6Network]
@ -97,7 +99,7 @@ class TrustedNetworksAuthProvider(AuthProvider):
"""Trusted Networks auth provider does not support MFA.""" """Trusted Networks auth provider does not support MFA."""
return False return False
async def async_login_flow(self, context: dict | None) -> LoginFlow: async def async_login_flow(self, context: dict[str, Any] | None) -> LoginFlow:
"""Return a flow to login.""" """Return a flow to login."""
assert context is not None assert context is not None
ip_addr = cast(IPAddress, context.get("ip_address")) ip_addr = cast(IPAddress, context.get("ip_address"))

View File

@ -21,7 +21,12 @@ from homeassistant.exceptions import (
) )
from homeassistant.helpers import device_registry, entity_registry from homeassistant.helpers import device_registry, entity_registry
from homeassistant.helpers.event import Event from homeassistant.helpers.event import Event
from homeassistant.helpers.typing import UNDEFINED, DiscoveryInfoType, UndefinedType from homeassistant.helpers.typing import (
UNDEFINED,
ConfigType,
DiscoveryInfoType,
UndefinedType,
)
from homeassistant.setup import async_process_deps_reqs, async_setup_component from homeassistant.setup import async_process_deps_reqs, async_setup_component
from homeassistant.util.decorator import Registry from homeassistant.util.decorator import Registry
import homeassistant.util.uuid as uuid_util import homeassistant.util.uuid as uuid_util
@ -598,7 +603,10 @@ class ConfigEntriesFlowManager(data_entry_flow.FlowManager):
"""Manage all the config entry flows that are in progress.""" """Manage all the config entry flows that are in progress."""
def __init__( def __init__(
self, hass: HomeAssistant, config_entries: ConfigEntries, hass_config: dict self,
hass: HomeAssistant,
config_entries: ConfigEntries,
hass_config: ConfigType,
) -> None: ) -> None:
"""Initialize the config entry flow manager.""" """Initialize the config entry flow manager."""
super().__init__(hass) super().__init__(hass)
@ -748,7 +756,7 @@ class ConfigEntries:
An instance of this object is available via `hass.config_entries`. An instance of this object is available via `hass.config_entries`.
""" """
def __init__(self, hass: HomeAssistant, hass_config: dict) -> None: def __init__(self, hass: HomeAssistant, hass_config: ConfigType) -> None:
"""Initialize the entry manager.""" """Initialize the entry manager."""
self.hass = hass self.hass = hass
self.flow = ConfigEntriesFlowManager(hass, self, hass_config) self.flow = ConfigEntriesFlowManager(hass, self, hass_config)

View File

@ -9,6 +9,8 @@ import attr
if TYPE_CHECKING: if TYPE_CHECKING:
from .core import Context from .core import Context
# mypy: disallow-any-generics
class HomeAssistantError(Exception): class HomeAssistantError(Exception):
"""General Home Assistant exception occurred.""" """General Home Assistant exception occurred."""
@ -42,7 +44,7 @@ class ConditionError(HomeAssistantError):
"""Return indentation.""" """Return indentation."""
return " " * indent + message return " " * indent + message
def output(self, indent: int) -> Generator: def output(self, indent: int) -> Generator[str, None, None]:
"""Yield an indented representation.""" """Yield an indented representation."""
raise NotImplementedError() raise NotImplementedError()
@ -58,7 +60,7 @@ class ConditionErrorMessage(ConditionError):
# A message describing this error # A message describing this error
message: str = attr.ib() message: str = attr.ib()
def output(self, indent: int) -> Generator: def output(self, indent: int) -> Generator[str, None, None]:
"""Yield an indented representation.""" """Yield an indented representation."""
yield self._indent(indent, f"In '{self.type}' condition: {self.message}") yield self._indent(indent, f"In '{self.type}' condition: {self.message}")
@ -74,7 +76,7 @@ class ConditionErrorIndex(ConditionError):
# The error that this error wraps # The error that this error wraps
error: ConditionError = attr.ib() error: ConditionError = attr.ib()
def output(self, indent: int) -> Generator: def output(self, indent: int) -> Generator[str, None, None]:
"""Yield an indented representation.""" """Yield an indented representation."""
if self.total > 1: if self.total > 1:
yield self._indent( yield self._indent(
@ -93,7 +95,7 @@ class ConditionErrorContainer(ConditionError):
# List of ConditionErrors that this error wraps # List of ConditionErrors that this error wraps
errors: Sequence[ConditionError] = attr.ib() errors: Sequence[ConditionError] = attr.ib()
def output(self, indent: int) -> Generator: def output(self, indent: int) -> Generator[str, None, None]:
"""Yield an indented representation.""" """Yield an indented representation."""
for item in self.errors: for item in self.errors:
yield from item.output(indent) yield from item.output(indent)

View File

@ -4,6 +4,7 @@ from __future__ import annotations
import asyncio import asyncio
from collections.abc import Iterable from collections.abc import Iterable
import logging import logging
from typing import Any
from homeassistant import config as conf_util from homeassistant import config as conf_util
from homeassistant.const import SERVICE_RELOAD from homeassistant.const import SERVICE_RELOAD
@ -15,11 +16,13 @@ from homeassistant.helpers.typing import ConfigType
from homeassistant.loader import async_get_integration from homeassistant.loader import async_get_integration
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
# mypy: disallow-any-generics
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
async def async_reload_integration_platforms( async def async_reload_integration_platforms(
hass: HomeAssistant, integration_name: str, integration_platforms: Iterable hass: HomeAssistant, integration_name: str, integration_platforms: Iterable[str]
) -> None: ) -> None:
"""Reload an integration's platforms. """Reload an integration's platforms.
@ -62,7 +65,7 @@ async def _resetup_platform(
if not conf: if not conf:
return return
root_config: dict = {integration_platform: []} root_config: dict[str, Any] = {integration_platform: []}
# Extract only the config for template, ignore the rest. # Extract only the config for template, ignore the rest.
for p_type, p_config in config_per_platform(conf, integration_platform): for p_type, p_config in config_per_platform(conf, integration_platform):
if p_type != integration_name: if p_type != integration_name:
@ -102,7 +105,7 @@ async def _async_setup_platform(
hass: HomeAssistant, hass: HomeAssistant,
integration_name: str, integration_name: str,
integration_platform: str, integration_platform: str,
platform_configs: list[dict], platform_configs: list[dict[str, Any]],
) -> None: ) -> None:
"""Platform for the first time when new configuration is added.""" """Platform for the first time when new configuration is added."""
if integration_platform not in hass.data: if integration_platform not in hass.data:
@ -120,7 +123,7 @@ async def _async_setup_platform(
async def _async_reconfig_platform( async def _async_reconfig_platform(
platform: EntityPlatform, platform_configs: list[dict] platform: EntityPlatform, platform_configs: list[dict[str, Any]]
) -> None: ) -> None:
"""Reconfigure an already loaded platform.""" """Reconfigure an already loaded platform."""
await platform.async_reset() await platform.async_reset()
@ -155,7 +158,7 @@ def async_get_platform_without_config_entry(
async def async_setup_reload_service( async def async_setup_reload_service(
hass: HomeAssistant, domain: str, platforms: Iterable hass: HomeAssistant, domain: str, platforms: Iterable[str]
) -> None: ) -> None:
"""Create the reload service for the domain.""" """Create the reload service for the domain."""
if hass.services.has_service(domain, SERVICE_RELOAD): if hass.services.has_service(domain, SERVICE_RELOAD):
@ -171,7 +174,9 @@ async def async_setup_reload_service(
) )
def setup_reload_service(hass: HomeAssistant, domain: str, platforms: Iterable) -> None: def setup_reload_service(
hass: HomeAssistant, domain: str, platforms: Iterable[str]
) -> None:
"""Sync version of async_setup_reload_service.""" """Sync version of async_setup_reload_service."""
asyncio.run_coroutine_threadsafe( asyncio.run_coroutine_threadsafe(
async_setup_reload_service(hass, domain, platforms), async_setup_reload_service(hass, domain, platforms),

View File

@ -8,6 +8,8 @@ from homeassistant.core import HomeAssistant, callback
from . import template from . import template
# mypy: disallow-any-generics
class ScriptVariables: class ScriptVariables:
"""Class to hold and render script variables.""" """Class to hold and render script variables."""
@ -65,6 +67,6 @@ class ScriptVariables:
return rendered_variables return rendered_variables
def as_dict(self) -> dict: def as_dict(self) -> dict[str, Any]:
"""Return dict version of this class.""" """Return dict version of this class."""
return self.variables return self.variables

View File

@ -15,10 +15,10 @@ from homeassistant.config import get_default_config_dir
from homeassistant.requirements import pip_kwargs from homeassistant.requirements import pip_kwargs
from homeassistant.util.package import install_package, is_installed, is_virtual_env from homeassistant.util.package import install_package, is_installed, is_virtual_env
# mypy: allow-untyped-defs, no-warn-return-any # mypy: allow-untyped-defs, disallow-any-generics, no-warn-return-any
def run(args: list) -> int: def run(args: list[str]) -> int:
"""Run a script.""" """Run a script."""
scripts = [] scripts = []
path = os.path.dirname(__file__) path = os.path.dirname(__file__)

View File

@ -16,10 +16,13 @@ from homeassistant.const import (
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_START,
PLATFORM_FORMAT, PLATFORM_FORMAT,
) )
from homeassistant.core import CALLBACK_TYPE
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.typing import ConfigType from homeassistant.helpers.typing import ConfigType
from homeassistant.util import dt as dt_util, ensure_unique_string from homeassistant.util import dt as dt_util, ensure_unique_string
# mypy: disallow-any-generics
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
ATTR_COMPONENT = "component" ATTR_COMPONENT = "component"
@ -422,7 +425,7 @@ def _async_when_setup(
hass.async_create_task(when_setup()) hass.async_create_task(when_setup())
return return
listeners: list[Callable] = [] listeners: list[CALLBACK_TYPE] = []
async def _matched_event(event: core.Event) -> None: async def _matched_event(event: core.Event) -> None:
"""Call the callback when we matched an event.""" """Call the callback when we matched an event."""
@ -443,7 +446,7 @@ def _async_when_setup(
@core.callback @core.callback
def async_get_loaded_integrations(hass: core.HomeAssistant) -> set: def async_get_loaded_integrations(hass: core.HomeAssistant) -> set[str]:
"""Return the complete list of loaded integrations.""" """Return the complete list of loaded integrations."""
integrations = set() integrations = set()
for component in hass.config.components: for component in hass.config.components:
@ -457,7 +460,9 @@ def async_get_loaded_integrations(hass: core.HomeAssistant) -> set:
@contextlib.contextmanager @contextlib.contextmanager
def async_start_setup(hass: core.HomeAssistant, components: Iterable) -> Generator: def async_start_setup(
hass: core.HomeAssistant, components: Iterable[str]
) -> Generator[None, None, None]:
"""Keep track of when setup starts and finishes.""" """Keep track of when setup starts and finishes."""
setup_started = hass.data.setdefault(DATA_SETUP_STARTED, {}) setup_started = hass.data.setdefault(DATA_SETUP_STARTED, {})
started = dt_util.utcnow() started = dt_util.utcnow()

View File

@ -6,6 +6,8 @@ import math
import attr import attr
# mypy: disallow-any-generics
# Official CSS3 colors from w3.org: # Official CSS3 colors from w3.org:
# https://www.w3.org/TR/2010/PR-css3-color-20101028/#html4 # https://www.w3.org/TR/2010/PR-css3-color-20101028/#html4
# names do not have spaces in them so that we can compare against # names do not have spaces in them so that we can compare against
@ -392,7 +394,9 @@ def color_hs_to_xy(
return color_RGB_to_xy(*color_hs_to_RGB(iH, iS), Gamut) return color_RGB_to_xy(*color_hs_to_RGB(iH, iS), Gamut)
def _match_max_scale(input_colors: tuple, output_colors: tuple) -> tuple: def _match_max_scale(
input_colors: tuple[int, ...], output_colors: tuple[int, ...]
) -> tuple[int, ...]:
"""Match the maximum value of the output to the input.""" """Match the maximum value of the output to the input."""
max_in = max(input_colors) max_in = max(input_colors)
max_out = max(output_colors) max_out = max(output_colors)