|
14 | 14 | from collections.abc import AsyncIterator, Callable, Coroutine, Sequence |
15 | 15 | from dataclasses import dataclass |
16 | 16 | from datetime import datetime, timedelta, timezone |
| 17 | +from typing import cast |
17 | 18 |
|
18 | 19 | from frequenz.channels.util import Timer |
19 | 20 | from frequenz.channels.util._timer import _to_microseconds |
@@ -501,13 +502,19 @@ async def resample(self, *, one_shot: bool = False) -> None: |
501 | 502 | ) |
502 | 503 |
|
503 | 504 | 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 | + ) |
511 | 518 | if exceptions: |
512 | 519 | raise ResamplingError(exceptions) |
513 | 520 | if one_shot: |
|
0 commit comments