mirror of
https://github.com/home-assistant/core.git
synced 2025-12-24 21:06:19 +00:00
Update typing 02 (#48014)
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
"""Translation string lookup helpers."""
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
from collections import ChainMap
|
||||
import logging
|
||||
from typing import Any, Dict, List, Optional, Set
|
||||
from typing import Any
|
||||
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.loader import (
|
||||
@@ -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) -> dict[str, Any]:
|
||||
"""Return a flattened representation of dict data."""
|
||||
output = {}
|
||||
for key, value in data.items():
|
||||
@@ -38,7 +40,7 @@ def recursive_flatten(prefix: Any, data: Dict) -> Dict[str, Any]:
|
||||
@callback
|
||||
def component_translation_path(
|
||||
component: str, language: str, integration: Integration
|
||||
) -> Optional[str]:
|
||||
) -> str | None:
|
||||
"""Return the translation json file location for a component.
|
||||
|
||||
For component:
|
||||
@@ -69,8 +71,8 @@ def component_translation_path(
|
||||
|
||||
|
||||
def load_translations_files(
|
||||
translation_files: Dict[str, str]
|
||||
) -> Dict[str, Dict[str, Any]]:
|
||||
translation_files: dict[str, str]
|
||||
) -> dict[str, dict[str, Any]]:
|
||||
"""Load and parse translation.json files."""
|
||||
loaded = {}
|
||||
for component, translation_file in translation_files.items():
|
||||
@@ -90,13 +92,13 @@ def load_translations_files(
|
||||
|
||||
|
||||
def _merge_resources(
|
||||
translation_strings: Dict[str, Dict[str, Any]],
|
||||
components: Set[str],
|
||||
translation_strings: dict[str, dict[str, Any]],
|
||||
components: set[str],
|
||||
category: str,
|
||||
) -> Dict[str, Dict[str, Any]]:
|
||||
) -> dict[str, dict[str, Any]]:
|
||||
"""Build and merge the resources response for the given components and platforms."""
|
||||
# Build response
|
||||
resources: Dict[str, Dict[str, Any]] = {}
|
||||
resources: dict[str, dict[str, Any]] = {}
|
||||
for component in components:
|
||||
if "." not in component:
|
||||
domain = component
|
||||
@@ -131,10 +133,10 @@ def _merge_resources(
|
||||
|
||||
|
||||
def _build_resources(
|
||||
translation_strings: Dict[str, Dict[str, Any]],
|
||||
components: Set[str],
|
||||
translation_strings: dict[str, dict[str, Any]],
|
||||
components: set[str],
|
||||
category: str,
|
||||
) -> Dict[str, Dict[str, Any]]:
|
||||
) -> dict[str, dict[str, Any]]:
|
||||
"""Build the resources response for the given components."""
|
||||
# Build response
|
||||
return {
|
||||
@@ -146,8 +148,8 @@ def _build_resources(
|
||||
|
||||
|
||||
async def async_get_component_strings(
|
||||
hass: HomeAssistantType, language: str, components: Set[str]
|
||||
) -> Dict[str, Any]:
|
||||
hass: HomeAssistantType, language: str, components: set[str]
|
||||
) -> dict[str, Any]:
|
||||
"""Load translations."""
|
||||
domains = list({loaded.split(".")[-1] for loaded in components})
|
||||
integrations = dict(
|
||||
@@ -160,7 +162,7 @@ async def async_get_component_strings(
|
||||
)
|
||||
)
|
||||
|
||||
translations: Dict[str, Any] = {}
|
||||
translations: dict[str, Any] = {}
|
||||
|
||||
# Determine paths of missing components/platforms
|
||||
files_to_load = {}
|
||||
@@ -205,15 +207,15 @@ class _TranslationCache:
|
||||
def __init__(self, hass: HomeAssistantType) -> None:
|
||||
"""Initialize the cache."""
|
||||
self.hass = hass
|
||||
self.loaded: Dict[str, Set[str]] = {}
|
||||
self.cache: Dict[str, Dict[str, Dict[str, Any]]] = {}
|
||||
self.loaded: dict[str, set[str]] = {}
|
||||
self.cache: dict[str, dict[str, dict[str, Any]]] = {}
|
||||
|
||||
async def async_fetch(
|
||||
self,
|
||||
language: str,
|
||||
category: str,
|
||||
components: Set,
|
||||
) -> List[Dict[str, Dict[str, Any]]]:
|
||||
components: set,
|
||||
) -> 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) -> None:
|
||||
"""Populate the cache for a given set of components."""
|
||||
_LOGGER.debug(
|
||||
"Cache miss for %s: %s",
|
||||
@@ -247,12 +249,12 @@ class _TranslationCache:
|
||||
def _build_category_cache(
|
||||
self,
|
||||
language: str,
|
||||
components: Set,
|
||||
translation_strings: Dict[str, Dict[str, Any]],
|
||||
components: set,
|
||||
translation_strings: dict[str, dict[str, Any]],
|
||||
) -> None:
|
||||
"""Extract resources into the cache."""
|
||||
cached = self.cache.setdefault(language, {})
|
||||
categories: Set[str] = set()
|
||||
categories: set[str] = set()
|
||||
for resource in translation_strings.values():
|
||||
categories.update(resource)
|
||||
|
||||
@@ -263,7 +265,7 @@ class _TranslationCache:
|
||||
new_resources = resource_func(translation_strings, components, category)
|
||||
|
||||
for component, resource in new_resources.items():
|
||||
category_cache: Dict[str, Any] = cached.setdefault(
|
||||
category_cache: dict[str, Any] = cached.setdefault(
|
||||
component, {}
|
||||
).setdefault(category, {})
|
||||
|
||||
@@ -283,9 +285,9 @@ async def async_get_translations(
|
||||
hass: HomeAssistantType,
|
||||
language: str,
|
||||
category: str,
|
||||
integration: Optional[str] = None,
|
||||
config_flow: Optional[bool] = None,
|
||||
) -> Dict[str, Any]:
|
||||
integration: str | None = None,
|
||||
config_flow: bool | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Return all backend translations.
|
||||
|
||||
If integration specified, load it for that one.
|
||||
|
||||
Reference in New Issue
Block a user