Skip to content

Commit

Permalink
Update metadata instead of repace in _field helper
Browse files Browse the repository at this point in the history
  • Loading branch information
daglof authored and eigenein committed May 2, 2023
1 parent 70ad76f commit 3b5ff4f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pure_protobuf/dataclasses_.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,13 @@ def _field(number: int, *args, packed=True, isoneof=False, **kwargs) -> Any:
Convenience function to assign field numbers.
Calls the standard ``dataclasses.field`` function with the metadata assigned.
"""
metadata = {"number": number, "packed": packed, "isoneof": isoneof}
proto_metadata = {"number": number, "packed": packed, "isoneof": isoneof}

return dataclasses.field(*args, metadata=metadata, **kwargs)
metadata = kwargs.pop("metadata", None)
if metadata is not None:
proto_metadata.update(metadata)

return dataclasses.field(*args, metadata=proto_metadata, **kwargs)


def field(number: int, *args, packed=True, **kwargs) -> Any:
Expand Down
6 changes: 6 additions & 0 deletions tests/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,9 @@ class Box:

def test_recursive_dataclass():
assert cast(Message, Box(value=1, box=Box(value=2))).dumps() == b"\x08\x02\x12\x02\x08\x04"


def test_other_metadata():
dataclass_field = field(1, packed=False, metadata={"foo": True})

assert set(["number", "packed", "isoneof", "foo"]) == set(dataclass_field.metadata.keys())

0 comments on commit 3b5ff4f

Please sign in to comment.