8000 Right type for default values of enum fields · AdrienVannson/python-betterproto@fa000bd · GitHub
[go: up one dir, main page]

Skip to content

Commit fa000bd

Browse files
committed
Right type for default values of enum fields
1 parent 8fdea02 commit fa000bd

File tree

4 files changed

+44
-44
lines changed

4 files changed

+44
-44
lines changed

src/betterproto/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ def default_factory():
226226
# out at runtime. The generated dataclass variables are still typed correctly.
227227

228228

229-
def enum_field(number: int, group: Optional[str] = None, optional: bool = False, repeated: bool = False) -> Any:
230-
return dataclass_field(number, TYPE_ENUM, lambda: 0, group=group, optional=optional, repeated=repeated)
229+
def enum_field(number: int, enum_default_value: Callable[[], Enum], group: Optional[str] = None, optional: bool = False, repeated: bool = False) -> Any:
230+
return dataclass_field(number, TYPE_ENUM, enum_default_value, group=group, optional=optional, repeated=repeated)
231231

232232

233233
def bool_field(number: int, group: Optional[str] = None, optional: bool = False, repeated: bool = False) -> Any:

src/betterproto/lib/pydantic/google/protobuf/__init__.py

Lines changed: 28 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/betterproto/lib/std/google/protobuf/__init__.py

Lines changed: 13 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test_features.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class TestEnum(betterproto.Enum):
9696

9797
@dataclass
9898
class Foo(betterproto.Message):
99-
bar: TestEnum = betterproto.enum_field(1)
99+
bar: TestEnum = betterproto.enum_field(1, enum_default_value=lambda: TestEnum.try_value(0))
100100

101101
# JSON strings are supported, but ints should still be supported too.
102102
foo = Foo().from_dict({"bar": 1})

0 commit comments

Comments
 (0)
0