Skip to content

Commit 46427cd

Browse files
committed
Use future annotations in resampling
Use built-in types and collections.abc types for type hinting as it can be done on old Python versions when using: from __future__ import annotations Also it seems like the type checker works better, because a cast is not necessary anymore. So imports from typing are needed still because this doesn't work for type aliases, they still need stuff from typing. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent 9a8b9d8 commit 46427cd

File tree

2 files changed

+16
-19
lines changed

2 files changed

+16
-19
lines changed

src/frequenz/sdk/actor/_resampling.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
"""An actor to resample microgrid component metrics."""
55

6+
from __future__ import annotations
7+
68
import asyncio
79
import dataclasses
810
import logging
9-
from typing import Set
1011

1112
from frequenz.channels import Receiver, Sender
1213

@@ -117,7 +118,7 @@ async def run(self) -> None:
117118
118119
# noqa: DAR401 error
119120
"""
120-
tasks_to_cancel: Set[asyncio.Task] = set()
121+
tasks_to_cancel: set[asyncio.Task] = set()
121122
try:
122123
subscriptions_task = asyncio.create_task(
123124
self._process_resampling_requests()

src/frequenz/sdk/timeseries/resampling.py

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33

44
"""Timeseries resampler."""
55

6+
from __future__ import annotations
7+
68
import asyncio
79
import logging
810
import math
911
from collections import deque
1012
from datetime import datetime, timedelta
11-
from typing import Callable, Deque, Dict, Sequence, Union, cast
13+
from typing import Callable, Sequence
1214

1315
from frequenz.channels.util import Timer
1416

@@ -90,7 +92,7 @@ class ResamplingError(RuntimeError):
9092
"""
9193

9294
def __init__(
93-
self, exceptions: Dict[Source, Union[Exception, asyncio.CancelledError]]
95+
self, exceptions: dict[Source, Exception | asyncio.CancelledError]
9496
) -> None:
9597
"""Create an instance.
9698
@@ -166,7 +168,7 @@ def __init__(
166168
self._resampling_period_s = resampling_period_s
167169
self._max_data_age_in_periods: float = max_data_age_in_periods
168170
self._resampling_function: ResamplingFunction = resampling_function
169-
self._resamplers: Dict[Source, _StreamingHelper] = {}
171+
self._resamplers: dict[Source, _StreamingHelper] = {}
170172
self._timer: Timer = Timer(self._resampling_period_s)
171173

172174
async def stop(self) -> None:
@@ -240,19 +242,13 @@ async def resample(self, *, one_shot: bool = False) -> None:
240242
*[r.resample(timer_timestamp) for r in self._resamplers.values()],
241243
return_exceptions=True,
242244
)
243-
# We need to cast this because Python is not smart enough to figure
244-
# out the key value can't be None (not even adding another
245-
# condition checking for None in the dict expression).
246-
exceptions = cast(
247-
Dict[Source, Union[Exception, asyncio.CancelledError]],
248-
{
249-
source: results[i]
250-
for i, source in enumerate(self._resamplers)
251-
# CancelledError inherits from BaseException, but we don't want
252-
# to catch *all* BaseExceptions here.
253-
if isinstance(results[i], (Exception, asyncio.CancelledError))
254-
},
255-
)
245+
exceptions = {
246+
source: results[i]
247+
for i, source in enumerate(self._resamplers)
248+
# CancelledError inherits from BaseException, but we don't want
249+
# to catch *all* BaseExceptions here.
250+
if isinstance(results[i], (Exception, asyncio.CancelledError))
251+
}
256252
if exceptions:
257253
raise ResamplingError(exceptions)
258254
if one_shot:
@@ -293,7 +289,7 @@ def __init__(
293289
"""
294290
self._resampling_period_s = resampling_period_s
295291
self._max_data_age_in_periods: float = max_data_age_in_periods
296-
self._buffer: Deque[Sample] = deque()
292+
self._buffer: deque[Sample] = deque()
297293
self._resampling_function: ResamplingFunction = resampling_function
298294

299295
def add_sample(self, sample: Sample) -> None:

0 commit comments

Comments
 (0)