From d09fff595cf2ca3e7735309909f41501d83a75a0 Mon Sep 17 00:00:00 2001 From: Marc Mueller <30130371+cdce8p@users.noreply.github.com> Date: Wed, 20 Jul 2022 03:03:22 +0200 Subject: [PATCH] Rename existing TypeVars referencing Self type (#75473) --- homeassistant/backports/enum.py | 6 +++--- homeassistant/components/esphome/__init__.py | 6 +++--- homeassistant/components/zha/core/channels/__init__.py | 10 ++++++---- homeassistant/config_entries.py | 6 ++++-- homeassistant/helpers/restore_state.py | 4 ++-- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/homeassistant/backports/enum.py b/homeassistant/backports/enum.py index 9a96704a836..939d6e7669b 100644 --- a/homeassistant/backports/enum.py +++ b/homeassistant/backports/enum.py @@ -4,15 +4,15 @@ from __future__ import annotations from enum import Enum from typing import Any, TypeVar -_StrEnumT = TypeVar("_StrEnumT", bound="StrEnum") +_StrEnumSelfT = TypeVar("_StrEnumSelfT", bound="StrEnum") class StrEnum(str, Enum): """Partial backport of Python 3.11's StrEnum for our basic use cases.""" def __new__( - cls: type[_StrEnumT], value: str, *args: Any, **kwargs: Any - ) -> _StrEnumT: + cls: type[_StrEnumSelfT], value: str, *args: Any, **kwargs: Any + ) -> _StrEnumSelfT: """Create a new StrEnum instance.""" if not isinstance(value, str): raise TypeError(f"{value!r} is not a string") diff --git a/homeassistant/components/esphome/__init__.py b/homeassistant/components/esphome/__init__.py index 3d8455d3150..5d7b0efc18d 100644 --- a/homeassistant/components/esphome/__init__.py +++ b/homeassistant/components/esphome/__init__.py @@ -58,7 +58,7 @@ from .entry_data import RuntimeEntryData DOMAIN = "esphome" CONF_NOISE_PSK = "noise_psk" _LOGGER = logging.getLogger(__name__) -_T = TypeVar("_T") +_DomainDataSelfT = TypeVar("_DomainDataSelfT", bound="DomainData") STORAGE_VERSION = 1 @@ -101,11 +101,11 @@ class DomainData: ) @classmethod - def get(cls: type[_T], hass: HomeAssistant) -> _T: + def get(cls: type[_DomainDataSelfT], hass: HomeAssistant) -> _DomainDataSelfT: """Get the global DomainData instance stored in hass.data.""" # Don't use setdefault - this is a hot code path if DOMAIN in hass.data: - return cast(_T, hass.data[DOMAIN]) + return cast(_DomainDataSelfT, hass.data[DOMAIN]) ret = hass.data[DOMAIN] = cls() return ret diff --git a/homeassistant/components/zha/core/channels/__init__.py b/homeassistant/components/zha/core/channels/__init__.py index 9042856b456..849cdc29e47 100644 --- a/homeassistant/components/zha/core/channels/__init__.py +++ b/homeassistant/components/zha/core/channels/__init__.py @@ -36,8 +36,8 @@ if TYPE_CHECKING: from ...entity import ZhaEntity from ..device import ZHADevice -_ChannelsT = TypeVar("_ChannelsT", bound="Channels") -_ChannelPoolT = TypeVar("_ChannelPoolT", bound="ChannelPool") +_ChannelsSelfT = TypeVar("_ChannelsSelfT", bound="Channels") +_ChannelPoolSelfT = TypeVar("_ChannelPoolSelfT", bound="ChannelPool") _ChannelsDictType = dict[str, base.ZigbeeChannel] @@ -104,7 +104,7 @@ class Channels: } @classmethod - def new(cls: type[_ChannelsT], zha_device: ZHADevice) -> _ChannelsT: + def new(cls: type[_ChannelsSelfT], zha_device: ZHADevice) -> _ChannelsSelfT: """Create new instance.""" channels = cls(zha_device) for ep_id in sorted(zha_device.device.endpoints): @@ -272,7 +272,9 @@ class ChannelPool: ) @classmethod - def new(cls: type[_ChannelPoolT], channels: Channels, ep_id: int) -> _ChannelPoolT: + def new( + cls: type[_ChannelPoolSelfT], channels: Channels, ep_id: int + ) -> _ChannelPoolSelfT: """Create new channels for an endpoint.""" pool = cls(channels, ep_id) pool.add_all_channels() diff --git a/homeassistant/config_entries.py b/homeassistant/config_entries.py index 232d3e5cbf1..b5d5804f100 100644 --- a/homeassistant/config_entries.py +++ b/homeassistant/config_entries.py @@ -73,7 +73,7 @@ PATH_CONFIG = ".config_entries.json" SAVE_DELAY = 1 -_T = TypeVar("_T", bound="ConfigEntryState") +_ConfigEntryStateSelfT = TypeVar("_ConfigEntryStateSelfT", bound="ConfigEntryState") _R = TypeVar("_R") @@ -97,7 +97,9 @@ class ConfigEntryState(Enum): _recoverable: bool - def __new__(cls: type[_T], value: str, recoverable: bool) -> _T: + def __new__( + cls: type[_ConfigEntryStateSelfT], value: str, recoverable: bool + ) -> _ConfigEntryStateSelfT: """Create new ConfigEntryState.""" obj = object.__new__(cls) obj._value_ = value diff --git a/homeassistant/helpers/restore_state.py b/homeassistant/helpers/restore_state.py index 4f2d1dd0503..314daecde48 100644 --- a/homeassistant/helpers/restore_state.py +++ b/homeassistant/helpers/restore_state.py @@ -32,7 +32,7 @@ STATE_DUMP_INTERVAL = timedelta(minutes=15) # How long should a saved state be preserved if the entity no longer exists STATE_EXPIRATION = timedelta(days=7) -_StoredStateT = TypeVar("_StoredStateT", bound="StoredState") +_StoredStateSelfT = TypeVar("_StoredStateSelfT", bound="StoredState") class ExtraStoredData: @@ -82,7 +82,7 @@ class StoredState: return result @classmethod - def from_dict(cls: type[_StoredStateT], json_dict: dict) -> _StoredStateT: + def from_dict(cls: type[_StoredStateSelfT], json_dict: dict) -> _StoredStateSelfT: """Initialize a stored state from a dict.""" extra_data_dict = json_dict.get("extra_data") extra_data = RestoredExtraData(extra_data_dict) if extra_data_dict else None