Update typing 01 (#48013)

This commit is contained in:
Marc Mueller
2021-03-17 17:34:55 +01:00
committed by GitHub
parent 9011a54e7f
commit e55702d635
11 changed files with 303 additions and 313 deletions

View File

@@ -1,4 +1,6 @@
"""Module to help with parsing and generating configuration files."""
from __future__ import annotations
from collections import OrderedDict
import logging
import os
@@ -6,7 +8,7 @@ from pathlib import Path
import re
import shutil
from types import ModuleType
from typing import Any, Callable, Dict, Optional, Sequence, Set, Tuple, Union
from typing import Any, Callable, Sequence
from awesomeversion import AwesomeVersion
import voluptuous as vol
@@ -114,14 +116,14 @@ tts:
def _no_duplicate_auth_provider(
configs: Sequence[Dict[str, Any]]
) -> Sequence[Dict[str, Any]]:
configs: Sequence[dict[str, Any]]
) -> Sequence[dict[str, Any]]:
"""No duplicate auth provider config allowed in a list.
Each type of auth provider can only have one config without optional id.
Unique id is required if same type of auth provider used multiple times.
"""
config_keys: Set[Tuple[str, Optional[str]]] = set()
config_keys: set[tuple[str, str | None]] = set()
for config in configs:
key = (config[CONF_TYPE], config.get(CONF_ID))
if key in config_keys:
@@ -135,8 +137,8 @@ def _no_duplicate_auth_provider(
def _no_duplicate_auth_mfa_module(
configs: Sequence[Dict[str, Any]]
) -> Sequence[Dict[str, Any]]:
configs: Sequence[dict[str, Any]]
) -> Sequence[dict[str, Any]]:
"""No duplicate auth mfa module item allowed in a list.
Each type of mfa module can only have one config without optional id.
@@ -144,7 +146,7 @@ def _no_duplicate_auth_mfa_module(
times.
Note: this is different than auth provider
"""
config_keys: Set[str] = set()
config_keys: set[str] = set()
for config in configs:
key = config.get(CONF_ID, config[CONF_TYPE])
if key in config_keys:
@@ -313,7 +315,7 @@ def _write_default_config(config_dir: str) -> bool:
return False
async def async_hass_config_yaml(hass: HomeAssistant) -> Dict:
async def async_hass_config_yaml(hass: HomeAssistant) -> dict:
"""Load YAML from a Home Assistant configuration file.
This function allow a component inside the asyncio loop to reload its
@@ -337,8 +339,8 @@ async def async_hass_config_yaml(hass: HomeAssistant) -> Dict:
def load_yaml_config_file(
config_path: str, secrets: Optional[Secrets] = None
) -> Dict[Any, Any]:
config_path: str, secrets: Secrets | None = None
) -> dict[Any, Any]:
"""Parse a YAML configuration file.
Raises FileNotFoundError or HomeAssistantError.
@@ -421,9 +423,9 @@ def process_ha_config_upgrade(hass: HomeAssistant) -> None:
def async_log_exception(
ex: Exception,
domain: str,
config: Dict,
config: dict,
hass: HomeAssistant,
link: Optional[str] = None,
link: str | None = None,
) -> None:
"""Log an error for configuration validation.
@@ -437,8 +439,8 @@ def async_log_exception(
@callback
def _format_config_error(
ex: Exception, domain: str, config: Dict, link: Optional[str] = None
) -> Tuple[str, bool]:
ex: Exception, domain: str, config: dict, link: str | None = None
) -> tuple[str, bool]:
"""Generate log exception for configuration validation.
This method must be run in the event loop.
@@ -474,7 +476,7 @@ def _format_config_error(
return message, is_friendly
async def async_process_ha_core_config(hass: HomeAssistant, config: Dict) -> None:
async def async_process_ha_core_config(hass: HomeAssistant, config: dict) -> None:
"""Process the [homeassistant] section from the configuration.
This method is a coroutine.
@@ -603,7 +605,7 @@ async def async_process_ha_core_config(hass: HomeAssistant, config: Dict) -> Non
)
def _log_pkg_error(package: str, component: str, config: Dict, message: str) -> None:
def _log_pkg_error(package: str, component: str, config: dict, message: str) -> None:
"""Log an error while merging packages."""
message = f"Package {package} setup failed. Integration {component} {message}"
@@ -616,7 +618,7 @@ def _log_pkg_error(package: str, component: str, config: Dict, message: str) ->
_LOGGER.error(message)
def _identify_config_schema(module: ModuleType) -> Optional[str]:
def _identify_config_schema(module: ModuleType) -> str | None:
"""Extract the schema and identify list or dict based."""
if not isinstance(module.CONFIG_SCHEMA, vol.Schema): # type: ignore
return None
@@ -664,9 +666,9 @@ def _identify_config_schema(module: ModuleType) -> Optional[str]:
return None
def _recursive_merge(conf: Dict[str, Any], package: Dict[str, Any]) -> Union[bool, str]:
def _recursive_merge(conf: dict[str, Any], package: dict[str, Any]) -> bool | str:
"""Merge package into conf, recursively."""
error: Union[bool, str] = False
error: bool | str = False
for key, pack_conf in package.items():
if isinstance(pack_conf, dict):
if not pack_conf:
@@ -688,10 +690,10 @@ def _recursive_merge(conf: Dict[str, Any], package: Dict[str, Any]) -> Union[boo
async def merge_packages_config(
hass: HomeAssistant,
config: Dict,
packages: Dict[str, Any],
config: dict,
packages: dict[str, Any],
_log_pkg_error: Callable = _log_pkg_error,
) -> Dict:
) -> dict:
"""Merge packages into the top-level configuration. Mutate config."""
PACKAGES_CONFIG_SCHEMA(packages)
for pack_name, pack_conf in packages.items():
@@ -754,7 +756,7 @@ async def merge_packages_config(
async def async_process_component_config(
hass: HomeAssistant, config: ConfigType, integration: Integration
) -> Optional[ConfigType]:
) -> ConfigType | None:
"""Check component configuration and return processed configuration.
Returns None on error.
@@ -879,13 +881,13 @@ async def async_process_component_config(
@callback
def config_without_domain(config: Dict, domain: str) -> Dict:
def config_without_domain(config: dict, domain: str) -> dict:
"""Return a config with all configuration for a domain removed."""
filter_keys = extract_domain_configs(config, domain)
return {key: value for key, value in config.items() if key not in filter_keys}
async def async_check_ha_config_file(hass: HomeAssistant) -> Optional[str]:
async def async_check_ha_config_file(hass: HomeAssistant) -> str | None:
"""Check if Home Assistant configuration file is valid.
This method is a coroutine.
@@ -902,7 +904,7 @@ async def async_check_ha_config_file(hass: HomeAssistant) -> Optional[str]:
@callback
def async_notify_setup_error(
hass: HomeAssistant, component: str, display_link: Optional[str] = None
hass: HomeAssistant, component: str, display_link: str | None = None
) -> None:
"""Print a persistent notification.