Various type hint related improvements (#54971)

* Avoid some implicit generic Anys

* Fix hassio discovery view type hints

* Fix http view result type in assert message
This commit is contained in:
Ville Skyttä 2021-08-21 14:58:49 +03:00 committed by GitHub
parent de354f96fe
commit 59809503d1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 22 additions and 16 deletions

View file

@ -4,7 +4,9 @@ from __future__ import annotations
import re
from typing import Final
LEASES_REGEX: Final[re.Pattern] = re.compile(
# mypy: disallow-any-generics
LEASES_REGEX: Final[re.Pattern[str]] = re.compile(
r"(?P<ip>([0-9]{1,3}[\.]){3}[0-9]{1,3})"
+ r"\smac:\s(?P<mac>([0-9a-f]{2}[:-]){5}([0-9a-f]{2}))"
+ r"\svalid\sfor:\s(?P<timevalid>(-?\d+))"

View file

@ -8,7 +8,7 @@ from aiohttp.web_exceptions import HTTPServiceUnavailable
from homeassistant import config_entries
from homeassistant.components.http import HomeAssistantView
from homeassistant.const import ATTR_NAME, ATTR_SERVICE, EVENT_HOMEASSISTANT_START
from homeassistant.core import callback
from homeassistant.core import HomeAssistant, callback
from .const import ATTR_ADDON, ATTR_CONFIG, ATTR_DISCOVERY, ATTR_UUID
from .handler import HassioAPIError
@ -17,7 +17,7 @@ _LOGGER = logging.getLogger(__name__)
@callback
def async_setup_discovery_view(hass: HomeAssistantView, hassio):
def async_setup_discovery_view(hass: HomeAssistant, hassio):
"""Discovery setup."""
hassio_discovery = HassIODiscovery(hass, hassio)
hass.http.register_view(hassio_discovery)
@ -49,7 +49,7 @@ class HassIODiscovery(HomeAssistantView):
name = "api:hassio_push:discovery"
url = "/api/hassio_push/discovery/{uuid}"
def __init__(self, hass: HomeAssistantView, hassio):
def __init__(self, hass: HomeAssistant, hassio):
"""Initialize WebView."""
self.hass = hass
self.hassio = hassio

View file

@ -158,7 +158,7 @@ def request_handler_factory(
else:
assert (
False
), f"Result should be None, string, bytes or Response. Got: {result}"
), f"Result should be None, string, bytes or StreamResponse. Got: {result}"
return web.Response(body=bresult, status=status_code)

View file

@ -69,6 +69,8 @@ from .trace import (
trace_stack_top,
)
# mypy: disallow-any-generics
FROM_CONFIG_FORMAT = "{}_from_config"
ASYNC_FROM_CONFIG_FORMAT = "async_{}_from_config"
@ -113,7 +115,7 @@ def condition_trace_update_result(**kwargs: Any) -> None:
@contextmanager
def trace_condition(variables: TemplateVarsType) -> Generator:
def trace_condition(variables: TemplateVarsType) -> Generator[TraceElement, None, None]:
"""Trace condition evaluation."""
should_pop = True
trace_element = trace_stack_top(trace_stack_cv)

View file

@ -71,8 +71,8 @@ class TrackStates:
"""
all_states: bool
entities: set
domains: set
entities: set[str]
domains: set[str]
@dataclass
@ -394,7 +394,7 @@ def async_track_entity_registry_updated_event(
@callback
def _async_dispatch_domain_event(
hass: HomeAssistant, event: Event, callbacks: dict[str, list]
hass: HomeAssistant, event: Event, callbacks: dict[str, list[HassJob]]
) -> None:
domain = split_entity_id(event.data["entity_id"])[0]
@ -620,7 +620,7 @@ class _TrackStateChangeFiltered:
self._listeners.pop(listener_name)()
@callback
def _setup_entities_listener(self, domains: set, entities: set) -> None:
def _setup_entities_listener(self, domains: set[str], entities: set[str]) -> None:
if domains:
entities = entities.copy()
entities.update(self.hass.states.async_entity_ids(domains))
@ -634,7 +634,7 @@ class _TrackStateChangeFiltered:
)
@callback
def _setup_domains_listener(self, domains: set) -> None:
def _setup_domains_listener(self, domains: set[str]) -> None:
if not domains:
return

View file

@ -21,7 +21,7 @@ class TraceElement:
self._child_run_id: str | None = None
self._error: Exception | None = None
self.path: str = path
self._result: dict | None = None
self._result: dict[str, Any] | None = None
self.reuse_by_child = False
self._timestamp = dt_util.utcnow()

View file

@ -17,6 +17,8 @@ from homeassistant.loader import (
from homeassistant.util.async_ import gather_with_concurrency
from homeassistant.util.json import load_json
# mypy: disallow-any-generics
_LOGGER = logging.getLogger(__name__)
TRANSLATION_LOAD_LOCK = "translation_load_lock"
@ -24,7 +26,7 @@ TRANSLATION_FLATTEN_CACHE = "translation_flatten_cache"
LOCALE_EN = "en"
def recursive_flatten(prefix: Any, data: dict) -> dict[str, Any]:
def recursive_flatten(prefix: Any, data: dict[str, Any]) -> dict[str, Any]:
"""Return a flattened representation of dict data."""
output = {}
for key, value in data.items():
@ -212,7 +214,7 @@ class _TranslationCache:
self,
language: str,
category: str,
components: set,
components: set[str],
) -> list[dict[str, dict[str, Any]]]:
"""Load resources into the cache."""
components_to_load = components - self.loaded.setdefault(language, set())
@ -224,7 +226,7 @@ class _TranslationCache:
return [cached.get(component, {}).get(category, {}) for component in components]
async def _async_load(self, language: str, components: set) -> None:
async def _async_load(self, language: str, components: set[str]) -> None:
"""Populate the cache for a given set of components."""
_LOGGER.debug(
"Cache miss for %s: %s",
@ -247,7 +249,7 @@ class _TranslationCache:
def _build_category_cache(
self,
language: str,
components: set,
components: set[str],
translation_strings: dict[str, dict[str, Any]],
) -> None:
"""Extract resources into the cache."""