From 1847324edb489aeccd88b095932847d116d88661 Mon Sep 17 00:00:00 2001 From: Prashant Gupta Date: Fri, 18 Aug 2023 15:26:59 -0700 Subject: [PATCH 1/5] =?UTF-8?q?=F0=9F=90=9B=20fix=20issue=20where=20number?= =?UTF-8?q?ing=20is=20messed=20up?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Prashant Gupta --- py_to_proto/converter_base.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/py_to_proto/converter_base.py b/py_to_proto/converter_base.py index 02203ff..df3ed17 100644 --- a/py_to_proto/converter_base.py +++ b/py_to_proto/converter_base.py @@ -451,10 +451,7 @@ def _convert_message( ), { "oneof_index": len(nested_oneofs), - "number": self.get_field_number( - len(field_descriptors) + oneof_field_idx, - oneof_field_def, - ), + "number": field_number + oneof_field_idx, "name": oneof_field_name.lower(), }, ) From bddf9e1d3d5aa4408d94cb7001f2b1eb3a2c741f Mon Sep 17 00:00:00 2001 From: Prashant Gupta Date: Fri, 18 Aug 2023 15:47:41 -0700 Subject: [PATCH 2/5] =?UTF-8?q?=F0=9F=A7=AA=20add=20failing=20test?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Prashant Gupta --- tests/test_dataclass_to_proto.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/tests/test_dataclass_to_proto.py b/tests/test_dataclass_to_proto.py index a67c3d4..2be6aa2 100644 --- a/tests/test_dataclass_to_proto.py +++ b/tests/test_dataclass_to_proto.py @@ -576,6 +576,28 @@ class Foo: assert foostr_fld.containing_oneof is oneof_desc +def test_dataclass_to_proto_custom_field_numbers_not_ordered(temp_dpool): + """Make sure that custom fields can be added with Annotated types""" + + @dataclass + class Foo: + bar: Annotated[bool, FieldNumber(1)] + foo: Union[bool, str] + baz: Annotated[bool, FieldNumber(2)] + + desc = dataclass_to_proto("foo.bar", Foo, descriptor_pool=temp_dpool) + assert len(desc.oneofs) == 1 + oneof_desc = desc.oneofs_by_name["foo"] + foobool_fld = desc.fields_by_name["foo_bool"] + foostr_fld = desc.fields_by_name["foo_str"] + assert foobool_fld.number == 10 + assert foobool_fld.type == foobool_fld.TYPE_BOOL + assert foobool_fld.containing_oneof is oneof_desc + assert foostr_fld.number == 20 + assert foostr_fld.type == foostr_fld.TYPE_STRING + assert foostr_fld.containing_oneof is oneof_desc + + def test_dataclass_to_proto_optional_fields(temp_dpool): """Make sure that an optional field (with a default value) is handled with a true optional (local oneof) From a35f77966642fc7f9658ad6687c554d5a490f907 Mon Sep 17 00:00:00 2001 From: Prashant Gupta Date: Fri, 18 Aug 2023 15:48:11 -0700 Subject: [PATCH 3/5] =?UTF-8?q?=E2=8F=AA=20revert=20to=20main?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Prashant Gupta --- py_to_proto/converter_base.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/py_to_proto/converter_base.py b/py_to_proto/converter_base.py index df3ed17..e03f190 100644 --- a/py_to_proto/converter_base.py +++ b/py_to_proto/converter_base.py @@ -451,7 +451,9 @@ def _convert_message( ), { "oneof_index": len(nested_oneofs), - "number": field_number + oneof_field_idx, + "number": self.get_field_number( + len(field_descriptors) + oneof_field_idx, oneof_field_def + ), "name": oneof_field_name.lower(), }, ) From fc3df8f41646eba52ba5eb2e45bf9c8a8c0b56cd Mon Sep 17 00:00:00 2001 From: Prashant Gupta Date: Fri, 18 Aug 2023 15:48:44 -0700 Subject: [PATCH 4/5] =?UTF-8?q?=E2=8F=AA=20revert=20to=20main?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Prashant Gupta --- py_to_proto/converter_base.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/py_to_proto/converter_base.py b/py_to_proto/converter_base.py index e03f190..02203ff 100644 --- a/py_to_proto/converter_base.py +++ b/py_to_proto/converter_base.py @@ -452,7 +452,8 @@ def _convert_message( { "oneof_index": len(nested_oneofs), "number": self.get_field_number( - len(field_descriptors) + oneof_field_idx, oneof_field_def + len(field_descriptors) + oneof_field_idx, + oneof_field_def, ), "name": oneof_field_name.lower(), }, From c340dd6bb2f67098803892af0fa75b4f9302f4fc Mon Sep 17 00:00:00 2001 From: Prashant Gupta Date: Fri, 18 Aug 2023 15:49:22 -0700 Subject: [PATCH 5/5] =?UTF-8?q?=F0=9F=8E=A8=20add=20test=20description?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Prashant Gupta --- tests/test_dataclass_to_proto.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_dataclass_to_proto.py b/tests/test_dataclass_to_proto.py index 2be6aa2..059ac39 100644 --- a/tests/test_dataclass_to_proto.py +++ b/tests/test_dataclass_to_proto.py @@ -577,7 +577,8 @@ class Foo: def test_dataclass_to_proto_custom_field_numbers_not_ordered(temp_dpool): - """Make sure that custom fields can be added with Annotated types""" + """Make sure that custom fields can be added with Annotated types + and field numbers can be unordered""" @dataclass class Foo: