Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Commit 1cbbba6

Browse files
committed
Simplify code
1 parent faac70e commit 1cbbba6

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/betterproto2_compiler/plugin/models.py

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -317,22 +317,12 @@ def get_field_string(self) -> str:
317317
def betterproto_field_args(self) -> list[str]:
318318
args = []
319319

320-
if self.field_type == FieldDescriptorProtoType.TYPE_MESSAGE:
321-
type_package, type_name = parse_source_type_name(self.proto_obj.type_name, self.output_file.parent_request)
322-
323-
if (type_package, type_name) in WRAPPED_TYPES:
324-
unwrap_type = get_type_reference(
325-
package=self.output_file.package,
326-
imports=self.output_file.imports_end,
327-
source_type=self.proto_obj.type_name,
328-
request=self.output_file.parent_request,
329-
settings=self.output_file.settings,
330-
wrap=False,
331-
)
332-
333-
# Without the lambda function, the type is evaluated right away, which fails since the corresponding
334-
# import is placed at the end of the file to avoid circular imports.
335-
args.append(f"unwrap=lambda: {unwrap_type}")
320+
if self.field_type == FieldDescriptorProtoType.TYPE_MESSAGE and self.is_wrapped:
321+
unwrap_type = self.unwrapped_py_type
322+
323+
# Without the lambda function, the type is evaluated right away, which fails since the corresponding
324+
# import is placed at the end of the file to avoid circular imports.
325+
args.append(f"unwrap=lambda: {unwrap_type}")
336326

337327
if self.optional:
338328
args.append("optional=True")
@@ -382,7 +372,12 @@ def proto_name(self) -> str:
382372
return self.proto_obj.name
383373

384374
@property
385-
def py_type(self) -> str:
375+
def is_wrapped(self) -> bool:
376+
type_package, type_name = parse_source_type_name(self.proto_obj.type_name, self.output_file.parent_request)
377+
378+
return (type_package, type_name) in WRAPPED_TYPES
379+
380+
def _py_type(self, wrap: bool) -> str:
386381
"""String representation of Python type."""
387382
if self.proto_obj.type in PROTO_FLOAT_TYPES:
388383
return "float"
@@ -401,11 +396,20 @@ def py_type(self) -> str:
401396
imports=self.output_file.imports_end,
402397
source_type=self.proto_obj.type_name,
403398
request=self.output_file.parent_request,
399+
wrap=wrap,
404400
settings=self.output_file.settings,
405401
)
406402
else:
407403
raise NotImplementedError(f"Unknown type {self.proto_obj.type}")
408404

405+
@property
406+
def py_type(self) -> str:
407+
return self._py_type(wrap=True)
408+
409+
@property
410+
def unwrapped_py_type(self) -> str:
411+
return self._py_type(wrap=False)
412+
409413
@property
410414
def annotation(self) -> str:
411415
py_type = self.py_type

0 commit comments

Comments
 (0)