|
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 |
18 | 17 |
|
19 | 18 | from frequenz.channels.timer import Timer, TriggerAllMissed, _to_microseconds
|
20 | 19 | from frequenz.quantities import Quantity
|
@@ -495,25 +494,24 @@ async def resample(self, *, one_shot: bool = False) -> None:
|
495 | 494 | self._config.resampling_period,
|
496 | 495 | )
|
497 | 496 |
|
| 497 | + # We need to make a copy here because we need to match the results to the |
| 498 | + # current resamplers, and since we await here, new resamplers could be added |
| 499 | + # or removed from the dict while we awaiting the resampling, which would |
| 500 | + # cause the results to be out of sync. |
| 501 | + resampler_sources = list(self._resamplers) |
498 | 502 | results = await asyncio.gather(
|
499 | 503 | *[r.resample(self._window_end) for r in self._resamplers.values()],
|
500 | 504 | return_exceptions=True,
|
501 | 505 | )
|
502 | 506 |
|
503 | 507 | self._window_end += self._config.resampling_period
|
504 |
| - # We need the cast because mypy is not able to infer that this can only |
505 |
| - # contain Exception | CancelledError because of the condition in the list |
506 |
| - # comprehension below. |
507 |
| - exceptions = cast( |
508 |
| - dict[Source, Exception | asyncio.CancelledError], |
509 |
| - { |
510 |
| - source: results[i] |
511 |
| - for i, source in enumerate(self._resamplers) |
512 |
| - # CancelledError inherits from BaseException, but we don't want |
513 |
| - # to catch *all* BaseExceptions here. |
514 |
| - if isinstance(results[i], (Exception, asyncio.CancelledError)) |
515 |
| - }, |
516 |
| - ) |
| 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 | + } |
517 | 515 | if exceptions:
|
518 | 516 | raise ResamplingError(exceptions)
|
519 | 517 | if one_shot:
|
|
0 commit comments