@@ -317,22 +317,12 @@ def get_field_string(self) -> str:
317
317
def betterproto_field_args (self ) -> list [str ]:
318
318
args = []
319
319
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 } " )
336
326
337
327
if self .optional :
338
328
args .append ("optional=True" )
@@ -382,7 +372,12 @@ def proto_name(self) -> str:
382
372
return self .proto_obj .name
383
373
384
374
@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 :
386
381
"""String representation of Python type."""
387
382
if self .proto_obj .type in PROTO_FLOAT_TYPES :
388
383
return "float"
@@ -401,11 +396,20 @@ def py_type(self) -> str:
401
396
imports = self .output_file .imports_end ,
402
397
source_type = self .proto_obj .type_name ,
403
398
request = self .output_file .parent_request ,
399
+ wrap = wrap ,
404
400
settings = self .output_file .settings ,
405
401
)
406
402
else :
407
403
raise NotImplementedError (f"Unknown type { self .proto_obj .type } " )
408
404
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
+
409
413
@property
410
414
def annotation (self ) -> str :
411
415
py_type = self .py_type
0 commit comments