Use PEP 695 TypeVar syntax (#133049)

This commit is contained in:
Marc Mueller 2024-12-12 16:01:57 +01:00 committed by GitHub
parent 0a748252e7
commit 5c6e4ad191
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 13 additions and 20 deletions

View File

@ -6,7 +6,6 @@ from collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass
import logging import logging
from math import ceil from math import ceil
from typing import Generic, TypeVar
from motionblindsble.const import ( from motionblindsble.const import (
MotionBlindType, MotionBlindType,
@ -45,11 +44,9 @@ _LOGGER = logging.getLogger(__name__)
PARALLEL_UPDATES = 0 PARALLEL_UPDATES = 0
_T = TypeVar("_T")
@dataclass(frozen=True, kw_only=True) @dataclass(frozen=True, kw_only=True)
class MotionblindsBLESensorEntityDescription(SensorEntityDescription, Generic[_T]): class MotionblindsBLESensorEntityDescription[_T](SensorEntityDescription):
"""Entity description of a sensor entity with initial_value attribute.""" """Entity description of a sensor entity with initial_value attribute."""
initial_value: str | None = None initial_value: str | None = None
@ -110,7 +107,7 @@ async def async_setup_entry(
async_add_entities(entities) async_add_entities(entities)
class MotionblindsBLESensorEntity(MotionblindsBLEEntity, SensorEntity, Generic[_T]): class MotionblindsBLESensorEntity[_T](MotionblindsBLEEntity, SensorEntity):
"""Representation of a sensor entity.""" """Representation of a sensor entity."""
entity_description: MotionblindsBLESensorEntityDescription[_T] entity_description: MotionblindsBLESensorEntityDescription[_T]

View File

@ -4,7 +4,6 @@ from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass
from typing import Generic, TypeVar
from powerfox import Device, PowerMeter, WaterMeter from powerfox import Device, PowerMeter, WaterMeter
@ -22,11 +21,11 @@ from . import PowerfoxConfigEntry
from .coordinator import PowerfoxDataUpdateCoordinator from .coordinator import PowerfoxDataUpdateCoordinator
from .entity import PowerfoxEntity from .entity import PowerfoxEntity
T = TypeVar("T", PowerMeter, WaterMeter)
@dataclass(frozen=True, kw_only=True) @dataclass(frozen=True, kw_only=True)
class PowerfoxSensorEntityDescription(Generic[T], SensorEntityDescription): class PowerfoxSensorEntityDescription[T: (PowerMeter, WaterMeter)](
SensorEntityDescription
):
"""Describes Poweropti sensor entity.""" """Describes Poweropti sensor entity."""
value_fn: Callable[[T], float | int | None] value_fn: Callable[[T], float | int | None]

View File

@ -5,7 +5,7 @@ from __future__ import annotations
from collections.abc import Callable from collections.abc import Callable
from dataclasses import dataclass from dataclasses import dataclass
from operator import attrgetter, methodcaller from operator import attrgetter, methodcaller
from typing import TYPE_CHECKING, Generic, TypeVar from typing import TYPE_CHECKING
from tesla_powerwall import GridState, MeterResponse, MeterType from tesla_powerwall import GridState, MeterResponse, MeterType
@ -35,14 +35,12 @@ from .models import BatteryResponse, PowerwallConfigEntry, PowerwallRuntimeData
_METER_DIRECTION_EXPORT = "export" _METER_DIRECTION_EXPORT = "export"
_METER_DIRECTION_IMPORT = "import" _METER_DIRECTION_IMPORT = "import"
_ValueParamT = TypeVar("_ValueParamT") type _ValueType = float | int | str | None
_ValueT = TypeVar("_ValueT", bound=float | int | str | None)
@dataclass(frozen=True, kw_only=True) @dataclass(frozen=True, kw_only=True)
class PowerwallSensorEntityDescription( class PowerwallSensorEntityDescription[_ValueParamT, _ValueT: _ValueType](
SensorEntityDescription, SensorEntityDescription
Generic[_ValueParamT, _ValueT],
): ):
"""Describes Powerwall entity.""" """Describes Powerwall entity."""
@ -389,7 +387,7 @@ class PowerWallImportSensor(PowerWallEnergyDirectionSensor):
return meter.get_energy_imported() return meter.get_energy_imported()
class PowerWallBatterySensor(BatteryEntity, SensorEntity, Generic[_ValueT]): class PowerWallBatterySensor[_ValueT: _ValueType](BatteryEntity, SensorEntity):
"""Representation of an Powerwall Battery sensor.""" """Representation of an Powerwall Battery sensor."""
entity_description: PowerwallSensorEntityDescription[BatteryResponse, _ValueT] entity_description: PowerwallSensorEntityDescription[BatteryResponse, _ValueT]

View File

@ -90,7 +90,6 @@ RANDOM_MICROSECOND_MIN = 50000
RANDOM_MICROSECOND_MAX = 500000 RANDOM_MICROSECOND_MAX = 500000
_TypedDictT = TypeVar("_TypedDictT", bound=Mapping[str, Any]) _TypedDictT = TypeVar("_TypedDictT", bound=Mapping[str, Any])
_StateEventDataT = TypeVar("_StateEventDataT", bound=EventStateEventData)
@dataclass(slots=True, frozen=True) @dataclass(slots=True, frozen=True)
@ -333,7 +332,7 @@ def async_track_state_change_event(
@callback @callback
def _async_dispatch_entity_id_event_soon( def _async_dispatch_entity_id_event_soon[_StateEventDataT: EventStateEventData](
hass: HomeAssistant, hass: HomeAssistant,
callbacks: dict[str, list[HassJob[[Event[_StateEventDataT]], Any]]], callbacks: dict[str, list[HassJob[[Event[_StateEventDataT]], Any]]],
event: Event[_StateEventDataT], event: Event[_StateEventDataT],
@ -343,7 +342,7 @@ def _async_dispatch_entity_id_event_soon(
@callback @callback
def _async_dispatch_entity_id_event( def _async_dispatch_entity_id_event[_StateEventDataT: EventStateEventData](
hass: HomeAssistant, hass: HomeAssistant,
callbacks: dict[str, list[HassJob[[Event[_StateEventDataT]], Any]]], callbacks: dict[str, list[HassJob[[Event[_StateEventDataT]], Any]]],
event: Event[_StateEventDataT], event: Event[_StateEventDataT],
@ -363,7 +362,7 @@ def _async_dispatch_entity_id_event(
@callback @callback
def _async_state_filter( def _async_state_filter[_StateEventDataT: EventStateEventData](
hass: HomeAssistant, hass: HomeAssistant,
callbacks: dict[str, list[HassJob[[Event[_StateEventDataT]], Any]]], callbacks: dict[str, list[HassJob[[Event[_StateEventDataT]], Any]]],
event_data: _StateEventDataT, event_data: _StateEventDataT,