Skip to content

Commit 86430a7

Browse files
committed
Remove the unnecessary cast for exceptions in remove_timeseries
Either `mypy` was improved in general or using `zip()` helped it to figure out the type narrowing, but it seems it can now narrow the type of `exceptions` without the need of a cast. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent afb253b commit 86430a7

File tree

1 file changed

+7
-14
lines changed

1 file changed

+7
-14
lines changed

src/frequenz/sdk/timeseries/_resampling.py

+7-14
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
from collections.abc import AsyncIterator, Callable, Coroutine, Sequence
1515
from dataclasses import dataclass
1616
from datetime import datetime, timedelta, timezone
17-
from typing import cast
1817

1918
from frequenz.channels.timer import Timer, TriggerAllMissed, _to_microseconds
2019
from frequenz.quantities import Quantity
@@ -506,19 +505,13 @@ async def resample(self, *, one_shot: bool = False) -> None:
506505
)
507506

508507
self._window_end += self._config.resampling_period
509-
# We need the cast because mypy is not able to infer that this can only
510-
# contain Exception | CancelledError because of the condition in the list
511-
# comprehension below.
512-
exceptions = cast(
513-
dict[Source, Exception | asyncio.CancelledError],
514-
{
515-
source: result
516-
for source, result in zip(resampler_sources, results)
517-
# CancelledError inherits from BaseException, but we don't want
518-
# to catch *all* BaseExceptions here.
519-
if isinstance(result, (Exception, asyncio.CancelledError))
520-
},
521-
)
508+
exceptions = {
509+
source: result
510+
for source, result in zip(resampler_sources, results)
511+
# CancelledError inherits from BaseException, but we don't want
512+
# to catch *all* BaseExceptions here.
513+
if isinstance(result, (Exception, asyncio.CancelledError))
514+
}
522515
if exceptions:
523516
raise ResamplingError(exceptions)
524517
if one_shot:

0 commit comments

Comments
 (0)