Skip to content

[do not merge] Empty fields fix (based on v2.0.0b1) #1

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

Open
wants to merge 4 commits into
base: release-v2.0.0b1
Choose a base branch
from
Open
Changes from 3 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
24 changes: 18 additions & 6 deletions betterproto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -611,8 +611,16 @@ def __bytes__(self) -> bytes:
output += _serialize_single(meta.number, TYPE_BYTES, buf)
else:
for item in value:
output += _serialize_single(
meta.number, meta.proto_type, item, wraps=meta.wraps or ""
output += (
_serialize_single(
meta.number,
meta.proto_type,
item,
wraps=meta.wraps or "",
)
# if it's an empty message it still needs to be represented
# as an item in the repeated list
or b"\n\x00"
)
elif isinstance(value, dict):
for k, v in value.items():
Expand All @@ -636,8 +644,12 @@ def __bytes__(self) -> bytes:

@classmethod
def _type_hint(cls, field_name: str) -> Type:
module = inspect.getmodule(cls)
type_hints = get_type_hints(cls, vars(module))
global_vars = {}
for base in inspect.getmro(cls):
module = inspect.getmodule(base)
global_vars.update(vars(module))

type_hints = get_type_hints(cls, global_vars)
return type_hints[field_name]

@classmethod
Expand Down Expand Up @@ -831,9 +843,9 @@ def to_dict(
self._betterproto.cls_by_field[field_name]
) # type: ignore
if isinstance(v, list):
output[cased_name] = [enum_values[e].name for e in v]
output[cased_name] = [enum_values[e].value for e in v]
else:
output[cased_name] = enum_values[v].name
output[cased_name] = enum_values[v].value
else:
output[cased_name] = v
return output
Expand Down