Skip to content

Commit 1ea11ff

Browse files
Fix negative enums (#134)
1 parent dfc0ade commit 1ea11ff

File tree

4 files changed

+8
-13
lines changed

4 files changed

+8
-13
lines changed

betterproto2/src/betterproto2/__init__.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -866,11 +866,15 @@ def _get_field_default(self, field_name: str) -> Any:
866866
def _postprocess_single(self, wire_type: int, meta: FieldMetadata, field_name: str, value: Any) -> Any:
867867
"""Adjusts values after parsing."""
868868
if wire_type == WIRE_VARINT:
869-
if meta.proto_type in (TYPE_INT32, TYPE_INT64):
869+
if meta.proto_type in (TYPE_INT32, TYPE_INT64, TYPE_ENUM):
870870
bits = 32 if meta.proto_type == TYPE_INT32 else 64
871871
value = value & ((1 << bits) - 1)
872872
signbit = 1 << (bits - 1)
873873
value = int((value ^ signbit) - signbit)
874+
875+
if meta.proto_type == TYPE_ENUM:
876+
# Convert enum ints to python enum instances
877+
value = self._betterproto.cls_by_field[field_name](value)
874878
elif meta.proto_type in (TYPE_UINT32, TYPE_UINT64):
875879
bits = 32 if meta.proto_type == TYPE_UINT32 else 64
876880
value = value & ((1 << bits) - 1)
@@ -881,9 +885,6 @@ def _postprocess_single(self, wire_type: int, meta: FieldMetadata, field_name: s
881885
elif meta.proto_type == TYPE_BOOL:
882886
# Booleans use a varint encoding, so convert it to true/false.
883887
value = value > 0
884-
elif meta.proto_type == TYPE_ENUM:
885-
# Convert enum ints to python enum instances
886-
value = self._betterproto.cls_by_field[field_name](value)
887888
elif wire_type in (WIRE_FIXED_32, WIRE_FIXED_64):
888889
fmt = _pack_fmt(meta.proto_type)
889890
value = struct.unpack(fmt, value)[0]

betterproto2/tests/inputs/enum/enum.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"ZERO",
55
"ONE",
66
"THREE",
7-
"FOUR"
7+
"FOUR",
8+
"MINUS_ONE"
89
]
910
}

betterproto2_compiler/src/betterproto2_compiler/templates/template.py.j2

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,6 @@ class {{ enum.py_name | add_to_all }}(betterproto2.Enum):
2424

2525
{% endfor %}
2626

27-
{% if output_file.settings.pydantic_dataclasses %}
28-
@classmethod
29-
def __get_pydantic_core_schema__(cls, _source_type, _handler):
30-
from pydantic_core import core_schema
31-
32-
return core_schema.int_schema(ge=0)
33-
{% endif %}
34-
3527
{% if enum.has_renamed_entries %}
3628
@classmethod
3729
def betterproto_value_to_renamed_proto_names(cls) -> dict[int, str]:

betterproto2_compiler/tests/inputs/enum/enum.proto

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ enum Choice {
1414
// TWO = 2;
1515
FOUR = 4;
1616
THREE = 3;
17+
MINUS_ONE = -1;
1718
}
1819

1920
// A "C" like enum with the enum name prefixed onto members, these should be stripped

0 commit comments

Comments
 (0)