From 8abce25948792e4625ac2365fdc6eac9fcbfef3c Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Mon, 23 Jan 2023 09:04:40 +0100 Subject: [PATCH] Update Union typing (4) [Py310] (#86427) --- homeassistant/auth/permissions/types.py | 25 +++++++++---------- .../auth/providers/trusted_networks.py | 6 ++--- homeassistant/components/camera/prefs.py | 4 +-- homeassistant/components/energy/data.py | 12 ++++++--- homeassistant/components/http/__init__.py | 4 +-- homeassistant/components/mqtt/models.py | 4 +-- homeassistant/helpers/config_entry_flow.py | 4 +-- homeassistant/helpers/script.py | 4 +-- homeassistant/helpers/service_info/mqtt.py | 3 +-- homeassistant/helpers/storage.py | 4 +-- homeassistant/util/yaml/loader.py | 6 ++--- 11 files changed, 39 insertions(+), 37 deletions(-) diff --git a/homeassistant/auth/permissions/types.py b/homeassistant/auth/permissions/types.py index e335ba9f989..0aa8807211a 100644 --- a/homeassistant/auth/permissions/types.py +++ b/homeassistant/auth/permissions/types.py @@ -1,29 +1,28 @@ """Common code for permissions.""" from collections.abc import Mapping -from typing import Union # MyPy doesn't support recursion yet. So writing it out as far as we need. -ValueType = Union[ +ValueType = ( # Example: entities.all = { read: true, control: true } - Mapping[str, bool], - bool, - None, -] + Mapping[str, bool] + | bool + | None +) # Example: entities.domains = { light: … } SubCategoryDict = Mapping[str, ValueType] -SubCategoryType = Union[SubCategoryDict, bool, None] +SubCategoryType = SubCategoryDict | bool | None -CategoryType = Union[ +CategoryType = ( # Example: entities.domains - Mapping[str, SubCategoryType], + Mapping[str, SubCategoryType] # Example: entities.all - Mapping[str, ValueType], - bool, - None, -] + | Mapping[str, ValueType] + | bool + | None +) # Example: { entities: … } PolicyType = Mapping[str, CategoryType] diff --git a/homeassistant/auth/providers/trusted_networks.py b/homeassistant/auth/providers/trusted_networks.py index cf142fa1219..a6c4c19b02f 100644 --- a/homeassistant/auth/providers/trusted_networks.py +++ b/homeassistant/auth/providers/trusted_networks.py @@ -14,7 +14,7 @@ from ipaddress import ( ip_address, ip_network, ) -from typing import Any, Union, cast +from typing import Any, cast import voluptuous as vol @@ -27,8 +27,8 @@ from . import AUTH_PROVIDER_SCHEMA, AUTH_PROVIDERS, AuthProvider, LoginFlow from .. import InvalidAuthError from ..models import Credentials, RefreshToken, UserMeta -IPAddress = Union[IPv4Address, IPv6Address] -IPNetwork = Union[IPv4Network, IPv6Network] +IPAddress = IPv4Address | IPv6Address +IPNetwork = IPv4Network | IPv6Network CONF_TRUSTED_NETWORKS = "trusted_networks" CONF_TRUSTED_USERS = "trusted_users" diff --git a/homeassistant/components/camera/prefs.py b/homeassistant/components/camera/prefs.py index f1bb0a0a840..28e4e1eeacb 100644 --- a/homeassistant/components/camera/prefs.py +++ b/homeassistant/components/camera/prefs.py @@ -2,7 +2,7 @@ from __future__ import annotations from dataclasses import asdict, dataclass -from typing import Final, Union, cast +from typing import Final, cast from homeassistant.components.stream import Orientation from homeassistant.core import HomeAssistant @@ -33,7 +33,7 @@ class CameraPreferences: self._hass = hass # The orientation prefs are stored in in the entity registry options # The preload_stream prefs are stored in this Store - self._store = Store[dict[str, dict[str, Union[bool, Orientation]]]]( + self._store = Store[dict[str, dict[str, bool | Orientation]]]( hass, STORAGE_VERSION, STORAGE_KEY ) self._dynamic_stream_settings_by_entity_id: dict[ diff --git a/homeassistant/components/energy/data.py b/homeassistant/components/energy/data.py index 339c0c638e2..6f6b481b044 100644 --- a/homeassistant/components/energy/data.py +++ b/homeassistant/components/energy/data.py @@ -4,7 +4,7 @@ from __future__ import annotations import asyncio from collections import Counter from collections.abc import Awaitable, Callable -from typing import Literal, TypedDict, Union +from typing import Literal, TypedDict import voluptuous as vol @@ -120,9 +120,13 @@ class WaterSourceType(TypedDict): number_energy_price: float | None # Price for energy ($/m³) -SourceType = Union[ - GridSourceType, SolarSourceType, BatterySourceType, GasSourceType, WaterSourceType -] +SourceType = ( + GridSourceType + | SolarSourceType + | BatterySourceType + | GasSourceType + | WaterSourceType +) class DeviceConsumption(TypedDict): diff --git a/homeassistant/components/http/__init__.py b/homeassistant/components/http/__init__.py index 55b7226b119..1c201725c00 100644 --- a/homeassistant/components/http/__init__.py +++ b/homeassistant/components/http/__init__.py @@ -7,7 +7,7 @@ import logging import os import ssl from tempfile import NamedTemporaryFile -from typing import Any, Final, TypedDict, Union, cast +from typing import Any, Final, TypedDict, cast from aiohttp import web from aiohttp.typedefs import StrOrURL @@ -498,7 +498,7 @@ async def start_http_server_and_save_config( if CONF_TRUSTED_PROXIES in conf: conf[CONF_TRUSTED_PROXIES] = [ - str(cast(Union[IPv4Network, IPv6Network], ip).network_address) + str(cast(IPv4Network | IPv6Network, ip).network_address) for ip in conf[CONF_TRUSTED_PROXIES] ] diff --git a/homeassistant/components/mqtt/models.py b/homeassistant/components/mqtt/models.py index 408e455365e..a88fb97b833 100644 --- a/homeassistant/components/mqtt/models.py +++ b/homeassistant/components/mqtt/models.py @@ -8,7 +8,7 @@ from collections.abc import Callable, Coroutine from dataclasses import dataclass, field import datetime as dt import logging -from typing import TYPE_CHECKING, Any, TypedDict, Union +from typing import TYPE_CHECKING, Any, TypedDict import attr @@ -39,7 +39,7 @@ _LOGGER = logging.getLogger(__name__) ATTR_THIS = "this" -PublishPayloadType = Union[str, bytes, int, float, None] +PublishPayloadType = str | bytes | int | float | None @attr.s(slots=True, frozen=True) diff --git a/homeassistant/helpers/config_entry_flow.py b/homeassistant/helpers/config_entry_flow.py index c9d6ffe6065..6cdedf98f97 100644 --- a/homeassistant/helpers/config_entry_flow.py +++ b/homeassistant/helpers/config_entry_flow.py @@ -3,7 +3,7 @@ from __future__ import annotations from collections.abc import Awaitable, Callable import logging -from typing import TYPE_CHECKING, Any, Generic, TypeVar, Union, cast +from typing import TYPE_CHECKING, Any, Generic, TypeVar, cast from homeassistant import config_entries from homeassistant.components import onboarding @@ -176,7 +176,7 @@ def register_discovery_flow( ) -> None: """Register flow for discovered integrations that not require auth.""" - class DiscoveryFlow(DiscoveryFlowHandler[Union[Awaitable[bool], bool]]): + class DiscoveryFlow(DiscoveryFlowHandler[Awaitable[bool] | bool]): """Discovery flow handler.""" def __init__(self) -> None: diff --git a/homeassistant/helpers/script.py b/homeassistant/helpers/script.py index 1d45954e0a7..34a0d4de2d4 100644 --- a/homeassistant/helpers/script.py +++ b/homeassistant/helpers/script.py @@ -11,7 +11,7 @@ from functools import partial import itertools import logging from types import MappingProxyType -from typing import Any, TypedDict, Union, cast +from typing import Any, TypedDict, cast import async_timeout import voluptuous as vol @@ -1110,7 +1110,7 @@ async def _async_stop_scripts_at_shutdown(hass, event): ) -_VarsType = Union[dict[str, Any], MappingProxyType] +_VarsType = dict[str, Any] | MappingProxyType def _referenced_extract_ids(data: Any, key: str, found: set[str]) -> None: diff --git a/homeassistant/helpers/service_info/mqtt.py b/homeassistant/helpers/service_info/mqtt.py index fcf5d4744f1..3626f9b5758 100644 --- a/homeassistant/helpers/service_info/mqtt.py +++ b/homeassistant/helpers/service_info/mqtt.py @@ -1,11 +1,10 @@ """MQTT Discovery data.""" from dataclasses import dataclass import datetime as dt -from typing import Union from homeassistant.data_entry_flow import BaseServiceInfo -ReceivePayloadType = Union[str, bytes] +ReceivePayloadType = str | bytes @dataclass diff --git a/homeassistant/helpers/storage.py b/homeassistant/helpers/storage.py index 73c0d6e1d55..087f98e4c42 100644 --- a/homeassistant/helpers/storage.py +++ b/homeassistant/helpers/storage.py @@ -9,7 +9,7 @@ import inspect from json import JSONEncoder import logging import os -from typing import Any, Generic, TypeVar, Union +from typing import Any, Generic, TypeVar from homeassistant.const import EVENT_HOMEASSISTANT_FINAL_WRITE from homeassistant.core import CALLBACK_TYPE, CoreState, Event, HomeAssistant, callback @@ -24,7 +24,7 @@ _LOGGER = logging.getLogger(__name__) STORAGE_SEMAPHORE = "storage_semaphore" -_T = TypeVar("_T", bound=Union[Mapping[str, Any], Sequence[Any]]) +_T = TypeVar("_T", bound=Mapping[str, Any] | Sequence[Any]) @bind_hass diff --git a/homeassistant/util/yaml/loader.py b/homeassistant/util/yaml/loader.py index 6520ca60e81..bf8a4e9541a 100644 --- a/homeassistant/util/yaml/loader.py +++ b/homeassistant/util/yaml/loader.py @@ -8,7 +8,7 @@ from io import StringIO, TextIOWrapper import logging import os from pathlib import Path -from typing import Any, TextIO, TypeVar, Union, overload +from typing import Any, TextIO, TypeVar, overload import yaml @@ -29,7 +29,7 @@ from .objects import Input, NodeListClass, NodeStrClass # mypy: allow-untyped-calls, no-warn-return-any -JSON_TYPE = Union[list, dict, str] # pylint: disable=invalid-name +JSON_TYPE = list | dict | str # pylint: disable=invalid-name _DictT = TypeVar("_DictT", bound=dict) _LOGGER = logging.getLogger(__name__) @@ -154,7 +154,7 @@ class SafeLineLoader(yaml.SafeLoader): return getattr(self.stream, "name", "") -LoaderType = Union[SafeLineLoader, SafeLoader] +LoaderType = SafeLineLoader | SafeLoader def load_yaml(fname: str, secrets: Secrets | None = None) -> JSON_TYPE: