File tree 3 files changed +14
-2
lines changed
3 files changed +14
-2
lines changed Original file line number Diff line number Diff line change @@ -1439,6 +1439,8 @@ def to_dict(
1439
1439
elif field_is_repeated :
1440
1440
# Convert each item.
1441
1441
cls = self ._betterproto .cls_by_field [field_name ]
1442
+ if not value :
1443
+ value = []
1442
1444
if cls == datetime :
1443
1445
value = [_Timestamp .timestamp_to_json (i ) for i in value ]
1444
1446
elif cls == timedelta :
Original file line number Diff line number Diff line change 29
29
reference to `A` to `B`'s `fields` attribute.
30
30
"""
31
31
32
-
33
32
import builtins
34
33
import re
35
34
import textwrap
36
35
from dataclasses import (
37
36
dataclass ,
38
37
field ,
39
38
)
39
+
40
+
41
+ try :
42
+ from typing import Literal
43
+ except ImportError :
44
+ from typing_extensions import Literal
45
+
40
46
from typing import (
41
47
Dict ,
42
48
Iterable ,
@@ -250,6 +256,7 @@ class OutputTemplate:
250
256
services : List ["ServiceCompiler" ] = field (default_factory = list )
251
257
imports_type_checking_only : Set [str ] = field (default_factory = set )
252
258
pydantic_dataclasses : bool = False
259
+ use_optionals : Optional [Literal ["all" ]] = None
253
260
output : bool = True
254
261
255
262
@property
@@ -483,7 +490,7 @@ def repeated(self) -> bool:
483
490
484
491
@property
485
492
def optional (self ) -> bool :
486
- return self .proto_obj .proto3_optional
493
+ return self .proto_obj .proto3_optional or self . output_file . use_optionals == "all"
487
494
488
495
@property
489
496
def mutable (self ) -> bool :
Original file line number Diff line number Diff line change @@ -98,6 +98,9 @@ def generate_code(request: CodeGeneratorRequest) -> CodeGeneratorResponse:
98
98
output_package_name
99
99
].pydantic_dataclasses = True
100
100
101
+ if "useOptionals=all" in plugin_options :
102
+ request_data .output_packages [output_package_name ].use_optionals = "all"
103
+
101
104
# Read Messages and Enums
102
105
# We need to read Messages before Services in so that we can
103
106
# get the references to input/output messages for each service
You can’t perform that action at this time.
0 commit comments