Skip to content

Implement Message.__bool__ #138

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

Closed
wants to merge 22 commits into from

Conversation

Gobot1234
Copy link
Collaborator

@Gobot1234 Gobot1234 commented Aug 14, 2020

This allows you to check if a message has had a non-default value set for it.

>>> bool(MyMessage())
False
>>> bool(MyMessage(my_value='a value'))
True
>>> bool(MyMessage(my_value=''))
False

Proposed use case

def parse_body(self, body: MyMessage):
    for item in body.items:
        if item.type == 'some cool value':  
            # do stuff
    if body:
        acknowledge_body()

nat-n
nat-n previously approved these changes Aug 16, 2020
Copy link
Collaborator

@nat-n nat-n left a comment

Choose a reason for hiding this comment

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

I see how this can be convenient, and I guess this is idiomatic if we think of the message as being like a collection, which are typically falsey when empty in python.

Though I feel it's still a little bit of a weird thing to do conceptually, and as an API feature it's quite awkward to backtrack on, so I'm going to approve it with low confidence, and wait for a second opinion before merging.

Co-authored-by: nat <n@natn.me>
@Gobot1234
Copy link
Collaborator Author

As things go I think it would be better if you could detect if any value was passed to init but I wasn't too sure how that could be implemented with dataclasses as, following on from the collections note, bool([""]) would evaluate to True, which I think would make a bit more sense.

@cetanu
Copy link
Collaborator

cetanu commented Aug 17, 2020

So the message is true if any of the fields are populated with a truthy value?
I'm okay with it, but I'm curious what the exact use-case is

@Gobot1234
Copy link
Collaborator Author

If #130 was merged however this could be done

@nat-n
Copy link
Collaborator

nat-n commented Aug 18, 2020

I should have said this earlier, but an API change like this should really have a couple tests.

boukeversteegh and others added 17 commits August 24, 2020 13:56
…ive (danielgtaylor#102)

Fixes danielgtaylor#93 to_dict returns wrong enum fields when numbering is not consecutive
- replace some usages of `==` with `is`
- use available constants instead of magic strings for type names

Co-authored-by: nat <nat.noordanus@gmail.com>
…ture to represent parsed data (danielgtaylor#121)

Refactor plugin to parse input into data-class based hierarchical structure
* re-implement README gRPC client example to be a self-contained script
 - fix a syntax error
 - fix a usage error

* asyncio.run() was added in 3.7
 - this lib targets >= 3.6

* Apply suggestions from code review

Optimized imports, store RPC call result before printing

Co-authored-by: Arun Babu Neelicattu <arun.neelicattu@gmail.com>

* add entry-point check to example

Co-authored-by: Arun Babu Neelicattu <arun.neelicattu@gmail.com>
danielgtaylor#110)

* Serialize default values in oneofs when calling to_dict() or to_json()

This change is consistent with the official protobuf implementation. If
a default value is set when using a oneof, and then a message is
translated from message -> JSON -> message, the default value is kept in
tact. Also, if no default value is set, they remain null.

* Some cleanup + testing for nested messages with oneofs

* Cleanup oneof_enum test cases, they should be fixed

This _should_ address:
danielgtaylor#63

* Include default value oneof fields when serializing to bytes

This will cause oneof fields with default values to explicitly be sent
to clients. Note that does not mean that all fields are serialized and
sent to clients, just those that _could_ be null and are not.

* Remove assignment when populating a sub-message within a proto

Also, move setattr out one indentation level

* Properly transform proto with empty string in oneof to bytes

Also, updated tests to ensure that which_one_of picks up the set field

* Formatting betterproto/__init__.py

* Adding test cases demonstrating equivalent behaviour with google impl

* Removing a temporary file I made locally

* Adding some clarifying comments

* Fixing tests for python38
* Fix static type checking in grpclib client
* Fix python3.6 compatibility issue with dataclasses
- This update improves support for windows & removes the direct dependency on poetry
Use the standard library keyword module instead of a hard coded list and applying it to enum keys as well.
@Gobot1234
Copy link
Collaborator Author

I think I'm gonna have to close and reopen this.

@Gobot1234 Gobot1234 closed this Aug 24, 2020
@Gobot1234 Gobot1234 deleted the messagebool branch August 24, 2020 22:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants