-
Notifications
You must be signed in to change notification settings - Fork 220
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
to_dict on empty nested types #200
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -591,7 +591,6 @@ def __getattribute__(self, name: str) -> Any: | |
return value | ||
|
||
value = self._get_field_default(name) | ||
super().__setattr__(name, value) | ||
return value | ||
|
||
def __setattr__(self, attr: str, value: Any) -> None: | ||
|
@@ -875,6 +874,10 @@ def parse(self: T, data: bytes) -> T: | |
) | ||
|
||
current = getattr(self, field_name) | ||
|
||
if self.__raw_get(field_name) == PLACEHOLDER: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And maybe there should be a return on this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why should there be a return here? The for loop has to continue There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry I meant continue |
||
setattr(self, field_name, current) | ||
|
||
if meta.proto_type == TYPE_MAP: | ||
# Value represents a single key/value pair entry in the map. | ||
current[value.key] = value.value | ||
|
@@ -972,6 +975,8 @@ def to_dict( | |
) | ||
): | ||
output[cased_name] = value.to_dict(casing, include_default_values) | ||
elif self.__raw_get(field_name) != PLACEHOLDER: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar thing here, should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking about that but I saw line 540 doing the same comparison |
||
output[cased_name] = {} | ||
elif meta.proto_type == TYPE_MAP: | ||
for k in value: | ||
if hasattr(value[k], "to_dict"): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,7 +17,7 @@ class Foo(betterproto.Message): | |
assert betterproto.serialized_on_wire(foo.bar) is False | ||
|
||
# Serialized after setting something | ||
foo.bar.baz = 1 | ||
foo.bar = Bar(baz=1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Changing all this is a regression if this PR breaks this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I know that isn't great. My PR would break lazy initialization. If you want to keep that, I have to think of another implementation. But lazy initialization also isn't in the google implementation, so I thought it wouldn't be important. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well breaking things isn't good. I know how this can be fixed it's been discussed before but it involves every field having a serialized attribute or similar. |
||
assert betterproto.serialized_on_wire(foo.bar) is True | ||
|
||
# Still has it after setting the default value | ||
|
@@ -130,7 +130,7 @@ class Foo(betterproto.Message): | |
assert foo.bar == 0 | ||
assert betterproto.which_one_of(foo, "group1")[0] == "baz" | ||
|
||
foo.sub.val = 1 | ||
foo.sub = Sub(val=1) | ||
assert betterproto.serialized_on_wire(foo.sub) | ||
|
||
foo.abc = "test" | ||
|
@@ -348,7 +348,7 @@ def test_recursive_message_defaults(): | |
assert msg != RecursiveMessage( | ||
name="bob", intermediate=Intermediate(42), child=RecursiveMessage(name="jude") | ||
) | ||
msg.child.child.name = "jude" | ||
msg.child = RecursiveMessage(child=RecursiveMessage(name="jude")) | ||
assert msg == RecursiveMessage( | ||
name="bob", | ||
intermediate=Intermediate(42), | ||
|
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.
This should be moved below that if statement
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.
The line 879 uses the
current
attribute, so putting that below doesn't make any sense