Skip to content

Commit 3d9e002

Browse files
committed
Explicitly cast the exceptions dict
The new `mypy` version doesn't seem to be able to figure out that the dict can only have `Exception | asyncio.CancelledError` as values, even when the dict comprehension is being filtered by using `ininstance()`. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent cd208ad commit 3d9e002

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

src/frequenz/sdk/timeseries/_resampling.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
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
1718

1819
from frequenz.channels.util import Timer
1920
from frequenz.channels.util._timer import _to_microseconds
@@ -501,13 +502,19 @@ async def resample(self, *, one_shot: bool = False) -> None:
501502
)
502503

503504
self._window_end += self._config.resampling_period
504-
exceptions = {
505-
source: results[i]
506-
for i, source in enumerate(self._resamplers)
507-
# CancelledError inherits from BaseException, but we don't want
508-
# to catch *all* BaseExceptions here.
509-
if isinstance(results[i], (Exception, asyncio.CancelledError))
510-
}
505+
# We need the cast because mypy is not able to infer that this can only
506+
# contain Exception | CancelledError because of the condition in the list
507+
# comprehension below.
508+
exceptions = cast(
509+
dict[Source, Exception | asyncio.CancelledError],
510+
{
511+
source: results[i]
512+
for i, source in enumerate(self._resamplers)
513+
# CancelledError inherits from BaseException, but we don't want
514+
# to catch *all* BaseExceptions here.
515+
if isinstance(results[i], (Exception, asyncio.CancelledError))
516+
},
517+
)
511518
if exceptions:
512519
raise ResamplingError(exceptions)
513520
if one_shot:

0 commit comments

Comments
 (0)