From 6ad9bab0d164034242daa7ea30d4d3581b945b9c Mon Sep 17 00:00:00 2001 From: Daniel Kilimnik Date: Tue, 19 Jan 2021 11:44:54 +0100 Subject: [PATCH 1/4] to_dict on empty nested types (https://github.com/danielgtaylor/python-betterproto/issues/199) --- src/betterproto/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/betterproto/__init__.py b/src/betterproto/__init__.py index b26ec4bca..83fd83096 100644 --- a/src/betterproto/__init__.py +++ b/src/betterproto/__init__.py @@ -972,6 +972,8 @@ def to_dict( ) ): output[cased_name] = value.to_dict(casing, include_default_values) + else: + output[cased_name] = {} elif meta.proto_type == TYPE_MAP: for k in value: if hasattr(value[k], "to_dict"): From cef8f715086f65d004f7048aa3035374458d19ab Mon Sep 17 00:00:00 2001 From: DK99 Date: Wed, 20 Jan 2021 18:54:56 +0100 Subject: [PATCH 2/4] Removed lazy initialization --- src/betterproto/__init__.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/betterproto/__init__.py b/src/betterproto/__init__.py index 83fd83096..b64b4e7c9 100644 --- a/src/betterproto/__init__.py +++ b/src/betterproto/__init__.py @@ -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: @@ -875,6 +874,10 @@ def parse(self: T, data: bytes) -> T: ) current = getattr(self, field_name) + + if self.__raw_get(field_name) == PLACEHOLDER: + 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,7 +975,7 @@ def to_dict( ) ): output[cased_name] = value.to_dict(casing, include_default_values) - else: + elif self.__raw_get(field_name) != PLACEHOLDER: output[cased_name] = {} elif meta.proto_type == TYPE_MAP: for k in value: From 9b7dd26429f94962f0296ef5c9d036071d432d3e Mon Sep 17 00:00:00 2001 From: DK99 Date: Wed, 20 Jan 2021 18:55:23 +0100 Subject: [PATCH 3/4] removed lazy initialization from tests --- tests/test_features.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_features.py b/tests/test_features.py index ef33d8730..1f0025e31 100644 --- a/tests/test_features.py +++ b/tests/test_features.py @@ -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) 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), From d213abca03c90d0690e5e4d23894d51751478098 Mon Sep 17 00:00:00 2001 From: DK99 Date: Wed, 20 Jan 2021 18:57:08 +0100 Subject: [PATCH 4/4] Fixed formatting --- src/betterproto/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/betterproto/__init__.py b/src/betterproto/__init__.py index b64b4e7c9..8bd32b53b 100644 --- a/src/betterproto/__init__.py +++ b/src/betterproto/__init__.py @@ -589,7 +589,7 @@ def __getattribute__(self, name: str) -> Any: value = super().__getattribute__(name) if value is not PLACEHOLDER: return value - + value = self._get_field_default(name) return value