@@ -532,10 +532,11 @@ def write_messages(
532532 if is_scalar (field ) and field .label != d .FieldDescriptorProto .LABEL_REPEATED :
533533 # Scalar non repeated fields are r/w, generate getter and setter if deprecated
534534 scl_field = scl + [d .DescriptorProto .FIELD_FIELD_NUMBER , idx ]
535+ deprecation_scl_field = scl_field + [d .FieldDescriptorProto .OPTIONS_FIELD_NUMBER ] + [d .FieldOptions .DEPRECATED_FIELD_NUMBER ]
535536 if field .options .deprecated :
536537 wl ("@property" )
537538 self ._write_deprecation_warning (
538- scl_field + [ d . FieldDescriptorProto . OPTIONS_FIELD_NUMBER ] + [ d . FieldOptions . DEPRECATED_FIELD_NUMBER ] ,
539+ deprecation_scl_field ,
539540 "This field has been marked as deprecated using proto field options." ,
540541 )
541542 body = " ..." if not self ._has_comments (scl_field ) else ""
@@ -546,7 +547,7 @@ def write_messages(
546547 wl ("" )
547548 wl (f"@{ field .name } .setter" )
548549 self ._write_deprecation_warning (
549- scl_field + [ d . FieldDescriptorProto . OPTIONS_FIELD_NUMBER ] + [ d . FieldOptions . DEPRECATED_FIELD_NUMBER ] ,
550+ deprecation_scl_field ,
550551 "This field has been marked as deprecated using proto field options." ,
551552 )
552553 body = " ..." if not self ._has_comments (scl_field ) else ""
@@ -869,7 +870,7 @@ def write_grpc_async_hacks(self) -> None:
869870 wl ("..." )
870871 wl ("" )
871872
872- def write_grpc_stub_methods (self , service : d .ServiceDescriptorProto , scl_prefix : SourceCodeLocation , * , is_async : bool , both : bool = False , ignore_override_errors : bool = False ) -> None :
873+ def write_grpc_stub_methods (self , service : d .ServiceDescriptorProto , scl_prefix : SourceCodeLocation , * , is_async : bool , both : bool = False , ignore_type_error : bool = False ) -> None :
873874 wl = self ._write_line
874875 methods = [(i , m ) for i , m in enumerate (service .method ) if m .name not in PYTHON_RESERVED ]
875876 if not methods :
@@ -880,20 +881,30 @@ def type_str(method: d.MethodDescriptorProto, is_async: bool) -> str:
880881
881882 for i , method in methods :
882883 scl = scl_prefix + [d .ServiceDescriptorProto .METHOD_FIELD_NUMBER , i ]
883- wl ("@property" )
884- if method .options .deprecated :
884+ is_deprecated = method .options .deprecated
885+ has_comments = self ._has_comments (scl )
886+
887+ # Generate type annotation once
888+ if both :
889+ type_annotation = f"{ self ._import ('typing' , 'Union' )} [{ type_str (method , is_async = False )} , { type_str (method , is_async = True )} ]"
890+ else :
891+ type_annotation = type_str (method , is_async = is_async )
892+
893+ if is_deprecated :
894+ wl ("@property" )
885895 self ._write_deprecation_warning (
886- scl + [d .MethodDescriptorProto .OPTIONS_FIELD_NUMBER ] + [ d .MethodOptions .DEPRECATED_FIELD_NUMBER ],
896+ scl + [d .MethodDescriptorProto .OPTIONS_FIELD_NUMBER , d .MethodOptions .DEPRECATED_FIELD_NUMBER ],
887897 "This method has been marked as deprecated using proto method options." ,
888898 )
889- if both :
890- wl ("def {}(self) -> {}[{}, {}]:{}" , method .name , self ._import ("typing" , "Union" ), type_str (method , is_async = False ), type_str (method , is_async = True ), " ..." if not self ._has_comments (scl ) else " " )
899+ wl (f"def { method .name } (self) -> { type_annotation } :{ ' ...' if not has_comments else '' } { ' # type: ignore[override]' if ignore_type_error else '' } " )
900+
901+ if has_comments :
902+ with self ._indent ():
903+ if not self ._write_comments (scl ):
904+ wl ("..." )
891905 else :
892- wl ("def {}(self) -> {}:{}{}" , method .name , type_str (method , is_async = is_async ), " ..." if not self ._has_comments (scl ) else "" , "" if not ignore_override_errors else " # type: ignore[override]" )
893- if self ._has_comments (scl ):
894- with self ._indent ():
895- if not self ._write_comments (scl ):
896- wl ("..." )
906+ wl (f"{ method .name } : { type_annotation } { ' # type: ignore[assignment]' if ignore_type_error else '' } " )
907+ self ._write_comments (scl )
897908
898909 def write_grpc_methods (self , service : d .ServiceDescriptorProto , scl_prefix : SourceCodeLocation ) -> None :
899910 wl = self ._write_line
@@ -985,7 +996,7 @@ def write_grpc_services(
985996 if self ._write_comments (scl ):
986997 wl ("" )
987998 wl ("def __init__(self, channel: {}) -> None: ..." , self ._import ("grpc.aio" , "Channel" ))
988- self .write_grpc_stub_methods (service , scl , is_async = True , ignore_override_errors = True )
999+ self .write_grpc_stub_methods (service , scl , is_async = True , ignore_type_error = True )
9891000 wl ("" )
9901001
9911002 # The service definition interface
0 commit comments