Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support broadening or narrowing of types in Receivers and Senders #262

Merged
merged 4 commits into from
Jan 10, 2024

Conversation

shsms
Copy link
Contributor

@shsms shsms commented Jan 9, 2024

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()

@shsms shsms requested a review from a team as a code owner January 9, 2024 15:22
@github-actions github-actions bot added the part:tests Affects the unit, integration and performance (benchmarks) tests label Jan 9, 2024
This allows broadening of the message type, such that receivers that
stream narrow types can be typed as receivers streaming broader types.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
This allows narrowing of the message type, such that senders for
sending broad types can be typed as senders for narrower types.

Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
Signed-off-by: Sahas Subramanian <sahas.subramanian@proton.me>
@github-actions github-actions bot added the part:docs Affects the documentation label Jan 9, 2024
@shsms shsms added part:channels Affects channels implementation and removed part:docs Affects the documentation labels Jan 9, 2024
Copy link
Contributor

@Marenz Marenz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@llucax
Copy link
Contributor

llucax commented Jan 10, 2024

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
Copy link
Contributor

Marenz commented Jan 10, 2024

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

@llucax
Copy link
Contributor

llucax commented Jan 10, 2024

@shsms shsms added this pull request to the merge queue Jan 10, 2024
@llucax llucax added the part:core Affects the core types (`Sender`, `Receiver`, exceptions, etc.) label Jan 10, 2024
Merged via the queue into frequenz-floss:v1.x.x with commit 2267927 Jan 10, 2024
17 checks passed
@shsms shsms deleted the type-variance branch January 10, 2024 09:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
part:channels Affects channels implementation part:core Affects the core types (`Sender`, `Receiver`, exceptions, etc.) part:tests Affects the unit, integration and performance (benchmarks) tests
Projects
Development

Successfully merging this pull request may close these issues.

3 participants