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

Replace Merge and MergeNamed with merge() #238

Merged
merged 12 commits into from
Nov 16, 2023

Conversation

llucax
Copy link
Contributor

@llucax llucax commented Nov 9, 2023

  • Remove MergeNamed
  • Add a merge() wrapper function that replaces Merge
  • Rename Merge to _Merge to make it private more explicitly
  • Rename _Merge variable args to receivers
  • Improve string representation of merge receiver
  • Raise an exception is merge() is called with less than two receivers

Fixes #236, fixes #237.

We already have 2 ways to join multiple receivers, `Merge` and `select`.
`MergeNamed` is a mixture of both, so it is redundant.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
This is a more convenient way to use `Merge`, users normally only care
about merging some channels, so having a class just makes the syntax
more weird for most use cases that will be `async for msg in
merge(...)`.

`Merge` is not exposed to the user any more.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
This is just to make it more consistent with the internal attribute.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Since `merge()` uses the `_Merge` class, when printing what's returned
by `merge()` the string representation of `_Merge` is used. This is not
very useful since it doesn't show the name that the user wrote in the
code.

This commit adds a `name` argument to `_Merge` and uses it to create the
string representation of the receiver. If no name is provided, the class
name is used as before.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
@llucax llucax requested a review from a team as a code owner November 9, 2023 18:28
@github-actions github-actions bot added part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests part:tooling Affects the development tooling (CI, deployment, dependency management, etc.) part:channels Affects channels implementation part:synchronization Affects the synchronization of multiple sources (`select`, `merge`) part:core Affects the core types (`Sender`, `Receiver`, exceptions, etc.) labels Nov 9, 2023
@llucax llucax self-assigned this Nov 9, 2023
@llucax llucax requested a review from shsms November 9, 2023 18:29
@llucax llucax added the scope:breaking-change Breaking change, users will need to update their code label Nov 9, 2023
Copy link
Contributor

@daniel-zullo-frequenz daniel-zullo-frequenz left a comment

Choose a reason for hiding this comment

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

I only found a few cosmetics in the documentation to check for, otherwise LGTM

src/frequenz/channels/__init__.py Outdated Show resolved Hide resolved
src/frequenz/channels/_merge.py Outdated Show resolved Hide resolved
src/frequenz/channels/_merge.py Outdated Show resolved Hide resolved
Copy link
Contributor

@shsms shsms left a comment

Choose a reason for hiding this comment

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

Sorry about the late review, just got one comment.

src/frequenz/channels/_merge.py Outdated Show resolved Hide resolved
@llucax
Copy link
Contributor Author

llucax commented Nov 15, 2023

Updated. Sorry @shsms, I forgot to create fixup/new commits for all the changes and amended one one the old commits (to only raise when no receivers are passed). The diff for that amend is this one.

All the other changes are in new commits.

@shsms
Copy link
Contributor

shsms commented Nov 15, 2023

Thanks @llucax, I have no further comments, will approve once you fixup.

llucax and others added 3 commits November 16, 2023 11:03
Calling `merge()` with no receivers makes returns a stopped receiver,
which can be unexpected if it was done accidentally, as the user will
probably expect the merge receiver to block while receiving at least
once.

By raising an exception we make sure that users express their intentions
explicitly instead, having to write something like:

```python
receivers: list[Receiver[T]]
...
if receivers:
    async for m in merge(*receivers):
        ...
```

In this case it is clear the user is having the no-receivers case into
account and will handle it properly.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Apply suggestions made by @daniel-zullo-frequenz.

Co-authored-by: daniel-zullo-frequenz <120166726+daniel-zullo-frequenz@users.noreply.github.com>
Signed-off-by: Leandro Lucarella <luca@llucax.com>
In case `merge()` is called with one receiver there is no need to create
a `_Merge` object, we can just return the provided receiver instead.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Just to be consistent to how docstrings are normally written and
organized.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
The current tests for merge are really integration tests, as it uses a
channel, so we better rename it and mark it as such, as a failure in
this test could be actually a failure in `Anycast` instead.

Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
Signed-off-by: Leandro Lucarella <luca-frequenz@llucax.com>
@llucax
Copy link
Contributor Author

llucax commented Nov 16, 2023

Rebased to apply the fixups and enabled auto-merge.

@llucax llucax added this pull request to the merge queue Nov 16, 2023
Merged via the queue into frequenz-floss:v1.x.x with commit 9266229 Nov 16, 2023
14 checks passed
@llucax llucax deleted the revamp-merge branch November 16, 2023 10:15
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:docs Affects the documentation part:synchronization Affects the synchronization of multiple sources (`select`, `merge`) part:tests Affects the unit, integration and performance (benchmarks) tests part:tooling Affects the development tooling (CI, deployment, dependency management, etc.) scope:breaking-change Breaking change, users will need to update their code
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Expose Merge as a merge() function Remove MergeNamed
4 participants