1212
1313"""Helpers for :mod:`protobuf`."""
1414
15- import collections
1615import copy
1716import inspect
1817
18+ from collections .abc import Mapping
19+ from collections .abc import MutableMapping
20+ from collections .abc import MutableSequence
21+ from collections import OrderedDict
22+
1923from google .protobuf import field_mask_pb2
2024from google .protobuf import message
2125from google .protobuf import wrappers_pb2
@@ -82,7 +86,7 @@ def get_messages(module):
8286 Message class names as keys, and the Message subclasses themselves
8387 as values.
8488 """
85- answer = collections . OrderedDict ()
89+ answer = OrderedDict ()
8690 for name in dir (module ):
8791 candidate = getattr (module , name )
8892 if (inspect .isclass (candidate ) and
@@ -143,7 +147,7 @@ def get(msg_or_dict, key, default=_SENTINEL):
143147 # If we get something else, complain.
144148 if isinstance (msg_or_dict , message .Message ):
145149 answer = getattr (msg_or_dict , key , default )
146- elif isinstance (msg_or_dict , collections . Mapping ):
150+ elif isinstance (msg_or_dict , Mapping ):
147151 answer = msg_or_dict .get (key , default )
148152 else :
149153 raise TypeError (
@@ -166,21 +170,21 @@ def _set_field_on_message(msg, key, value):
166170 """Set helper for protobuf Messages."""
167171 # Attempt to set the value on the types of objects we know how to deal
168172 # with.
169- if isinstance (value , (collections . MutableSequence , tuple )):
173+ if isinstance (value , (MutableSequence , tuple )):
170174 # Clear the existing repeated protobuf message of any elements
171175 # currently inside it.
172176 while getattr (msg , key ):
173177 getattr (msg , key ).pop ()
174178
175179 # Write our new elements to the repeated field.
176180 for item in value :
177- if isinstance (item , collections . Mapping ):
181+ if isinstance (item , Mapping ):
178182 getattr (msg , key ).add (** item )
179183 else :
180184 # protobuf's RepeatedCompositeContainer doesn't support
181185 # append.
182186 getattr (msg , key ).extend ([item ])
183- elif isinstance (value , collections . Mapping ):
187+ elif isinstance (value , Mapping ):
184188 # Assign the dictionary values to the protobuf message.
185189 for item_key , item_value in value .items ():
186190 set (getattr (msg , key ), item_key , item_value )
@@ -202,7 +206,7 @@ def set(msg_or_dict, key, value):
202206 """
203207 # Sanity check: Is our target object valid?
204208 if (not isinstance (msg_or_dict ,
205- (collections . MutableMapping , message .Message ))):
209+ (MutableMapping , message .Message ))):
206210 raise TypeError (
207211 'set() expected a dict or protobuf message, got {!r}.' .format (
208212 type (msg_or_dict )))
@@ -213,12 +217,12 @@ def set(msg_or_dict, key, value):
213217 # If a subkey exists, then get that object and call this method
214218 # recursively against it using the subkey.
215219 if subkey is not None :
216- if isinstance (msg_or_dict , collections . MutableMapping ):
220+ if isinstance (msg_or_dict , MutableMapping ):
217221 msg_or_dict .setdefault (basekey , {})
218222 set (get (msg_or_dict , basekey ), subkey , value )
219223 return
220224
221- if isinstance (msg_or_dict , collections . MutableMapping ):
225+ if isinstance (msg_or_dict , MutableMapping ):
222226 msg_or_dict [key ] = value
223227 else :
224228 _set_field_on_message (msg_or_dict , key , value )
0 commit comments