-
Notifications
You must be signed in to change notification settings - Fork 8
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
Handle type guards properly in Receiver.filter()
#332
Conversation
src/frequenz/channels/_receiver.py
Outdated
# We need to use Any here because otherwise _Filter would have to deal with two | ||
# different signatures. We can create two filter classes, one for regular functions | ||
# and one for type guards, but then there is no way to tell at runtime which | ||
# function is a type guard and which isn't to instantiate the correct class. | ||
# Using Any here has no impact though, as thanks to the overloads, only the | ||
# overloaded types will be accepted. | ||
def filter( | ||
self, filter_function: Callable[[ReceiverMessageT_co], Any], / | ||
) -> Receiver[Any]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to change _Filter
because TypeGuard
is just another name for bool, with some additional semantic meaning. The type system knows assigning a TypeGuard
value to a bool
is fine.
# We need to use Any here because otherwise _Filter would have to deal with two | |
# different signatures. We can create two filter classes, one for regular functions | |
# and one for type guards, but then there is no way to tell at runtime which | |
# function is a type guard and which isn't to instantiate the correct class. | |
# Using Any here has no impact though, as thanks to the overloads, only the | |
# overloaded types will be accepted. | |
def filter( | |
self, filter_function: Callable[[ReceiverMessageT_co], Any], / | |
) -> Receiver[Any]: | |
def filter( | |
self, | |
filter_function: ( | |
Callable[[ReceiverMessageT_co], bool] | |
| Callable[[ReceiverMessageT_co], TypeGuard[FilteredMessageT_co]] | |
), | |
/, | |
) -> Receiver[ReceiverMessageT_co] | Receiver[FilteredMessageT_co]: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting, I think what I tried was: filter_function: Callable[[ReceiverMessageT_co], bool | TypeGuard[FilteredMessageT_co]]
and that didn't work.
Updated. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, this is great!
tests/test_broadcast.py
Outdated
assert message == 8 | ||
match message: | ||
case int(): | ||
assert message == 8 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is it necessary to have this check twice? (in line 267 and 270 ? assert message == 8
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it could just be a pass
inside the case
, true. I just needed to put something :D
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just changed the test to make sure the case int()
is executed. mypy
should verify this too thannks to the assert_never
, but just in case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Now it is clear for me why it was necessary :)
Updated again. |
Now the `Receiver` type returned by `Receiver.filter()` will have the narrowed type when a `TypeGuard` is used. Signed-off-by: Leandro Lucarella <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎉
Now the
Receiver
type returned byReceiver.filter()
will have the narrowed type when aTypeGuard
is used.