Skip to content

Commit cf67a68

Browse files
authored
Use PEP 695 TypeVar syntax for paperless_ngx (home-assistant#147156)
1 parent b003429 commit cf67a68

File tree

3 files changed

+16
-20
lines changed

3 files changed

+16
-20
lines changed

homeassistant/components/paperless_ngx/coordinator.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
from abc import abstractmethod
66
from dataclasses import dataclass
77
from datetime import timedelta
8-
from typing import TypeVar
98

109
from pypaperless import Paperless
1110
from pypaperless.exceptions import (
@@ -25,8 +24,6 @@
2524

2625
type PaperlessConfigEntry = ConfigEntry[PaperlessData]
2726

28-
TData = TypeVar("TData")
29-
3027
UPDATE_INTERVAL_STATISTICS = timedelta(seconds=120)
3128
UPDATE_INTERVAL_STATUS = timedelta(seconds=300)
3229

@@ -39,7 +36,7 @@ class PaperlessData:
3936
status: PaperlessStatusCoordinator
4037

4138

42-
class PaperlessCoordinator(DataUpdateCoordinator[TData]):
39+
class PaperlessCoordinator[DataT](DataUpdateCoordinator[DataT]):
4340
"""Coordinator to manage fetching Paperless-ngx API."""
4441

4542
config_entry: PaperlessConfigEntry
@@ -63,7 +60,7 @@ def __init__(
6360
update_interval=update_interval,
6461
)
6562

66-
async def _async_update_data(self) -> TData:
63+
async def _async_update_data(self) -> DataT:
6764
"""Update data via internal method."""
6865
try:
6966
return await self._async_update_data_internal()
@@ -89,7 +86,7 @@ async def _async_update_data(self) -> TData:
8986
) from err
9087

9188
@abstractmethod
92-
async def _async_update_data_internal(self) -> TData:
89+
async def _async_update_data_internal(self) -> DataT:
9390
"""Update data via paperless-ngx API."""
9491

9592

homeassistant/components/paperless_ngx/entity.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,24 @@
22

33
from __future__ import annotations
44

5-
from typing import Generic, TypeVar
6-
75
from homeassistant.components.sensor import EntityDescription
86
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
97
from homeassistant.helpers.update_coordinator import CoordinatorEntity
108

119
from .const import DOMAIN
1210
from .coordinator import PaperlessCoordinator
1311

14-
TCoordinator = TypeVar("TCoordinator", bound=PaperlessCoordinator)
15-
1612

17-
class PaperlessEntity(CoordinatorEntity[TCoordinator], Generic[TCoordinator]):
13+
class PaperlessEntity[CoordinatorT: PaperlessCoordinator](
14+
CoordinatorEntity[CoordinatorT]
15+
):
1816
"""Defines a base Paperless-ngx entity."""
1917

2018
_attr_has_entity_name = True
2119

2220
def __init__(
2321
self,
24-
coordinator: TCoordinator,
22+
coordinator: CoordinatorT,
2523
description: EntityDescription,
2624
) -> None:
2725
"""Initialize the Paperless-ngx entity."""

homeassistant/components/paperless_ngx/sensor.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from collections.abc import Callable
66
from dataclasses import dataclass
7-
from typing import Generic
87

98
from pypaperless.models import Statistic, Status
109
from pypaperless.models.common import StatusType
@@ -23,23 +22,23 @@
2322

2423
from .coordinator import (
2524
PaperlessConfigEntry,
25+
PaperlessCoordinator,
2626
PaperlessStatisticCoordinator,
2727
PaperlessStatusCoordinator,
28-
TData,
2928
)
30-
from .entity import PaperlessEntity, TCoordinator
29+
from .entity import PaperlessEntity
3130

3231
PARALLEL_UPDATES = 0
3332

3433

3534
@dataclass(frozen=True, kw_only=True)
36-
class PaperlessEntityDescription(SensorEntityDescription, Generic[TData]):
35+
class PaperlessEntityDescription[DataT](SensorEntityDescription):
3736
"""Describes Paperless-ngx sensor entity."""
3837

39-
value_fn: Callable[[TData], StateType]
38+
value_fn: Callable[[DataT], StateType]
4039

4140

42-
SENSOR_STATISTICS: tuple[PaperlessEntityDescription, ...] = (
41+
SENSOR_STATISTICS: tuple[PaperlessEntityDescription[Statistic], ...] = (
4342
PaperlessEntityDescription[Statistic](
4443
key="documents_total",
4544
translation_key="documents_total",
@@ -78,7 +77,7 @@ class PaperlessEntityDescription(SensorEntityDescription, Generic[TData]):
7877
),
7978
)
8079

81-
SENSOR_STATUS: tuple[PaperlessEntityDescription, ...] = (
80+
SENSOR_STATUS: tuple[PaperlessEntityDescription[Status], ...] = (
8281
PaperlessEntityDescription[Status](
8382
key="storage_total",
8483
translation_key="storage_total",
@@ -258,7 +257,9 @@ async def async_setup_entry(
258257
async_add_entities(entities)
259258

260259

261-
class PaperlessSensor(PaperlessEntity[TCoordinator], SensorEntity):
260+
class PaperlessSensor[CoordinatorT: PaperlessCoordinator](
261+
PaperlessEntity[CoordinatorT], SensorEntity
262+
):
262263
"""Defines a Paperless-ngx sensor entity."""
263264

264265
entity_description: PaperlessEntityDescription

0 commit comments

Comments
 (0)