8000 Mark messages as optional · AdrienVannson/python-betterproto@f5a5c8c · GitHub
[go: up one dir, main page]

Skip to content

Commit f5a5c8c

Browse files
committed
Mark messages as optional
1 parent 1ed8da0 commit f5a5c8c

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/betterproto/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,12 @@ def dataclass_field(
204204
repeated: bool = False,
205205
) -> dataclasses.Field:
206206
"""Creates a dataclass field with attached protobuf metadata."""
207-
if optional or group:
208-
def default_factory():
209-
return None
210-
elif repeated:
207+
if repeated:
211208
def default_factory():
212209
return []
210+
elif optional or group:
211+
def default_factory():
212+
return None
213213

214214
return dataclasses.field(
215215
default_factory=default_factory,

src/betterproto/plugin/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ def repeated(self) -> bool:
504504

505505
@property
506506
def optional(self) -> bool:
507-
return self.proto_obj.proto3_optional
507+
return self.proto_obj.proto3_optional or self.field_type == "message"
508508

509509
@property
510510
def field_type(self) -> str:

0 commit comments

Comments
 (0)
0