2121import dataclasses
2222import sys
2323from itertools import chain
24- from typing import Callable , List , Mapping , Sequence , Set , Tuple
24+ from typing import Callable , Dict , FrozenSet , Mapping , Sequence , Set , Tuple
2525
26- from google .longrunning import operations_pb2
26+ from google .longrunning import operations_pb2 # type: ignore
2727from google .protobuf import descriptor_pb2
2828
2929from gapic .generator import options
@@ -100,21 +100,21 @@ def module_name(self) -> str:
100100 return to_snake_case (self .name .split ('/' )[- 1 ][:- len ('.proto' )])
101101
102102 @cached_property
103- def names (self ) -> Set [str ]:
103+ def names (self ) -> FrozenSet [str ]:
104104 """Return a set of names used by this proto.
105105
106106 This is used for detecting naming collisions in the module names
107107 used for imports.
108108 """
109109 # Add names of all enums, messages, and fields.
110- answer = {e .name for e in self .all_enums .values ()}
110+ answer : Set [ str ] = {e .name for e in self .all_enums .values ()}
111111 for message in self .all_messages .values ():
112112 answer = answer .union ({f .name for f in message .fields .values ()})
113113 answer .add (message .name )
114114
115115 # Identify any import module names where the same module name is used
116116 # from distinct packages.
117- modules = {}
117+ modules : Dict [ str , Set [ str ]] = {}
118118 for t in chain (* [m .field_types for m in self .all_messages .values ()]):
119119 modules .setdefault (t .ident .module , set ())
120120 modules [t .ident .module ].add (t .ident .package )
@@ -175,7 +175,7 @@ class API:
175175 """
176176 naming : api_naming .Naming
177177 all_protos : Mapping [str , Proto ]
178- subpackage_view : Tuple [str ] = dataclasses .field (default_factory = tuple )
178+ subpackage_view : Tuple [str , ... ] = dataclasses .field (default_factory = tuple )
179179
180180 @classmethod
181181 def build (cls ,
@@ -202,7 +202,7 @@ def build(cls,
202202
203203 # Iterate over each FileDescriptorProto and fill out a Proto
204204 # object describing it, and save these to the instance.
205- protos = {}
205+ protos : Dict [ str , Proto ] = {}
206206 for fd in file_descriptors :
207207 protos [fd .name ] = _ProtoBuilder (
208208 file_descriptor = fd ,
@@ -256,7 +256,7 @@ def subpackages(self) -> Mapping[str, 'API']:
256256 Each value in the mapping is another API object, but the ``protos``
257257 property only shows protos belonging to the subpackage.
258258 """
259- answer = collections .OrderedDict ()
259+ answer : Dict [ str , API ] = collections .OrderedDict ()
260260
261261 # Get the actual subpackages we have.
262262 #
@@ -294,9 +294,9 @@ def __init__(self, file_descriptor: descriptor_pb2.FileDescriptorProto,
294294 file_to_generate : bool ,
295295 naming : api_naming .Naming ,
296296 prior_protos : Mapping [str , Proto ] = None ):
297- self .proto_messages = {}
298- self .proto_enums = {}
299- self .proto_services = {}
297+ self .proto_messages : Dict [ str , wrappers . MessageType ] = {}
298+ self .proto_enums : Dict [ str , wrappers . EnumType ] = {}
299+ self .proto_services : Dict [ str , wrappers . Service ] = {}
300300 self .file_descriptor = file_descriptor
301301 self .file_to_generate = file_to_generate
302302 self .prior_protos = prior_protos or {}
@@ -307,7 +307,8 @@ def __init__(self, file_descriptor: descriptor_pb2.FileDescriptorProto,
307307 # the "path", which is a sequence of integers described in more
308308 # detail below; this code simply shifts from a list to a dict,
309309 # with tuples of paths as the dictionary keys.
310- self .docs = {}
310+ self .docs : Dict [Tuple [int , ...],
311+ descriptor_pb2 .SourceCodeInfo .Location ] = {}
311312 for location in file_descriptor .source_code_info .location :
312313 self .docs [tuple (location .path )] = location
313314
@@ -414,8 +415,9 @@ def api_messages(self) -> Mapping[str, wrappers.MessageType]:
414415 * [p .all_messages for p in self .prior_protos .values ()],
415416 )
416417
417- def _load_children (self , children : Sequence , loader : Callable , * ,
418- address : metadata .Address , path : Tuple [int ]) -> Mapping :
418+ def _load_children (self ,
419+ children : Sequence , loader : Callable , * ,
420+ address : metadata .Address , path : Tuple [int , ...]) -> Mapping :
419421 """Return wrapped versions of arbitrary children from a Descriptor.
420422
421423 Args:
@@ -445,9 +447,10 @@ def _load_children(self, children: Sequence, loader: Callable, *,
445447 answer [wrapped .name ] = wrapped
446448 return answer
447449
448- def _get_fields (self , field_pbs : List [descriptor_pb2 .FieldDescriptorProto ],
449- address : metadata .Address , path : Tuple [int ],
450- ) -> Mapping [str , wrappers .Field ]:
450+ def _get_fields (self ,
451+ field_pbs : Sequence [descriptor_pb2 .FieldDescriptorProto ],
452+ address : metadata .Address , path : Tuple [int , ...],
453+ ) -> Dict [str , wrappers .Field ]:
451454 """Return a dictionary of wrapped fields for the given message.
452455
453456 Args:
@@ -472,7 +475,7 @@ def _get_fields(self, field_pbs: List[descriptor_pb2.FieldDescriptorProto],
472475 # message wrapper is not yet created, because it needs this object
473476 # first) and this will be None. This case is addressed in the
474477 # `_load_message` method.
475- answer = collections .OrderedDict ()
478+ answer : Dict [ str , wrappers . Field ] = collections .OrderedDict ()
476479 for field_pb , i in zip (field_pbs , range (0 , sys .maxsize )):
477480 answer [field_pb .name ] = wrappers .Field (
478481 field_pb = field_pb ,
@@ -487,9 +490,10 @@ def _get_fields(self, field_pbs: List[descriptor_pb2.FieldDescriptorProto],
487490 # Done; return the answer.
488491 return answer
489492
490- def _get_methods (self , methods : List [descriptor_pb2 .MethodDescriptorProto ],
491- address : metadata .Address , path : Tuple [int ],
492- ) -> Mapping [str , wrappers .Method ]:
493+ def _get_methods (self ,
494+ methods : Sequence [descriptor_pb2 .MethodDescriptorProto ],
495+ address : metadata .Address , path : Tuple [int , ...],
496+ ) -> Mapping [str , wrappers .Method ]:
493497 """Return a dictionary of wrapped methods for the given service.
494498
495499 Args:
@@ -505,7 +509,7 @@ def _get_methods(self, methods: List[descriptor_pb2.MethodDescriptorProto],
505509 :class:`~.wrappers.Method` objects.
506510 """
507511 # Iterate over the methods and collect them into a dictionary.
508- answer = collections .OrderedDict ()
512+ answer : Dict [ str , wrappers . Method ] = collections .OrderedDict ()
509513 for meth_pb , i in zip (methods , range (0 , sys .maxsize )):
510514 lro = None
511515
0 commit comments