Skip to content

Commit e7aa645

Browse files
committed
Assert the receiver has the correct type
For every path where we create a new receiver, we now assert we end up with the correct type, as the code proved to be a bit weak, and we sometime ended up with a `Receiver[Any]` without noticing. Signed-off-by: Leandro Lucarella <[email protected]>
1 parent f7465bb commit e7aa645

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/frequenz/sdk/config/_manager.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pathlib
99
from collections.abc import Mapping, Sequence
1010
from datetime import timedelta
11-
from typing import Any, Final, TypeGuard, overload
11+
from typing import Any, Final, TypeGuard, assert_type, overload
1212

1313
from frequenz.channels import Broadcast, Receiver
1414
from frequenz.channels.experimental import WithPrevious
@@ -340,9 +340,10 @@ def _is_valid(
340340

341341
match (key, schema):
342342
case (None, None):
343+
assert_type(receiver, Receiver[Mapping[str, Any]])
343344
return receiver
344345
case (None, type()):
345-
return receiver.map(
346+
recv_dataclass = receiver.map(
346347
lambda config: _load_config_with_logging(
347348
config,
348349
schema,
@@ -355,10 +356,14 @@ def _is_valid(
355356
**marshmallow_load_kwargs,
356357
)
357358
).filter(_is_valid)
359+
assert_type(recv_dataclass, Receiver[DataclassT])
360+
return recv_dataclass
358361
case (str(), None):
359-
return receiver.map(lambda config: _get_key(config, key))
362+
recv_map_or_none = receiver.map(lambda config: _get_key(config, key))
363+
assert_type(recv_map_or_none, Receiver[Mapping[str, Any] | None])
364+
return recv_map_or_none
360365
case (str(), type()):
361-
return receiver.map(
366+
recv_dataclass_or_none = receiver.map(
362367
lambda config: _load_config_with_logging(
363368
config,
364369
schema,
@@ -367,6 +372,8 @@ def _is_valid(
367372
**marshmallow_load_kwargs,
368373
)
369374
).filter(_is_valid_or_none)
375+
assert_type(recv_dataclass_or_none, Receiver[DataclassT | None])
376+
return recv_dataclass_or_none
370377
case unexpected:
371378
# We can't use `assert_never` here because `mypy` is
372379
# having trouble

0 commit comments

Comments
 (0)