frequenz-floss / frequenz-channels-python

Channel implementations for Python
https://frequenz-floss.github.io/frequenz-channels-python/
MIT License
7 stars 8 forks source link

Support broadening or narrowing of types in `Receiver`s and `Sender`s #262

Closed shsms closed 8 months ago

shsms commented 9 months ago

This allows Receivers to reduce message types to be broader than actual, and senders to accept narrower types than required.

    @dataclass
    class Broader:
        """A broad class."""

        value: int

    class Actual(Broader):
        """Actual class."""

    class Narrower(Actual):
        """A narrower class."""

    chan = Broadcast[Actual](name="input-chan")

    sender: Sender[Narrower] = chan.new_sender()
    receiver: Receiver[Broader] = chan.new_receiver()
llucax commented 8 months ago

I wonder what's the use case for this. This is the case I was thinking of when adding the check_type option to the channel registry with type-checking, but I didn't find any use case. In any case, I think we could merge this, as I guess even if the SDK doesn't have a use case it is a valid relaxation, but I really wonder why you would want to not use the exact same types for sending and receiving.

Marenz commented 8 months ago

Multiple senders from different sources could maybe use different specialized types for the same sender?

llucax commented 8 months ago

OK, I see now this came because of this: