Rename existing TypeVars referencing Self type (#75473)

pull/71534/head
Marc Mueller 2022-07-20 03:03:22 +02:00 committed by GitHub
parent b04c3e9adc
commit d09fff595c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 18 additions and 14 deletions

View File

@ -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")

View File

@ -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

View File

@ -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()

View File

@ -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

View File

@ -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