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

to_dict on empty nested types #200

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/betterproto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -589,9 +589,8 @@ def __getattribute__(self, name: str) -> Any:
value = super().__getattribute__(name)
if value is not PLACEHOLDER:
return value

value = self._get_field_default(name)
super().__setattr__(name, value)
return value

def __setattr__(self, attr: str, value: Any) -> None:
Expand Down Expand Up @@ -875,6 +874,10 @@ def parse(self: T, data: bytes) -> T:
)

current = getattr(self, field_name)
Copy link
Collaborator

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

Copy link
Author

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


if self.__raw_get(field_name) == PLACEHOLDER:
Copy link
Collaborator

Choose a reason for hiding this comment

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

This should use is not ==

Copy link
Collaborator

Choose a reason for hiding this comment

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

And maybe there should be a return on this

Copy link
Author

Choose a reason for hiding this comment

The 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

Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -972,6 +975,8 @@ def to_dict(
)
):
output[cased_name] = value.to_dict(casing, include_default_values)
elif self.__raw_get(field_name) != PLACEHOLDER:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Similar thing here, should be is not

Copy link
Author

Choose a reason for hiding this comment

The 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"):
Expand Down