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

🐛 fix issue where numbering is messed up #57

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Changes from all 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
23 changes: 23 additions & 0 deletions tests/test_dataclass_to_proto.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,29 @@ 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
and field numbers can be unordered"""

@dataclass
class Foo:
bar: Annotated[bool, FieldNumber(1)]
foo: Union[bool, str]
baz: Annotated[bool, FieldNumber(2)]
Copy link
Member Author

Choose a reason for hiding this comment

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

This fails with

[Couldn't build proto file into descriptor pool: duplicate field number (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)
Expand Down
Loading