Skip to content

Commit 6205b5e

Browse files
authored
Import stdlib ABCs from 'collections.abc' rather than 'collections'. (#6451)
On Python 2.7, fall back to 'collections'. Closes #6450.
1 parent 446070f commit 6205b5e

1 file changed

Lines changed: 11 additions & 7 deletions

File tree

packages/google-api-core/google/api_core/protobuf_helpers.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
"""Helpers for :mod:`protobuf`."""
1616

1717
import collections
18+
try:
19+
from collections import abc as collections_abc
20+
except ImportError: # Python 2.7
21+
import collections as collections_abc
1822
import copy
1923
import inspect
2024

@@ -161,7 +165,7 @@ def get(msg_or_dict, key, default=_SENTINEL):
161165
# If we get something else, complain.
162166
if isinstance(msg_or_dict, message.Message):
163167
answer = getattr(msg_or_dict, key, default)
164-
elif isinstance(msg_or_dict, collections.Mapping):
168+
elif isinstance(msg_or_dict, collections_abc.Mapping):
165169
answer = msg_or_dict.get(key, default)
166170
else:
167171
raise TypeError(
@@ -184,21 +188,21 @@ def _set_field_on_message(msg, key, value):
184188
"""Set helper for protobuf Messages."""
185189
# Attempt to set the value on the types of objects we know how to deal
186190
# with.
187-
if isinstance(value, (collections.MutableSequence, tuple)):
191+
if isinstance(value, (collections_abc.MutableSequence, tuple)):
188192
# Clear the existing repeated protobuf message of any elements
189193
# currently inside it.
190194
while getattr(msg, key):
191195
getattr(msg, key).pop()
192196

193197
# Write our new elements to the repeated field.
194198
for item in value:
195-
if isinstance(item, collections.Mapping):
199+
if isinstance(item, collections_abc.Mapping):
196200
getattr(msg, key).add(**item)
197201
else:
198202
# protobuf's RepeatedCompositeContainer doesn't support
199203
# append.
200204
getattr(msg, key).extend([item])
201-
elif isinstance(value, collections.Mapping):
205+
elif isinstance(value, collections_abc.Mapping):
202206
# Assign the dictionary values to the protobuf message.
203207
for item_key, item_value in value.items():
204208
set(getattr(msg, key), item_key, item_value)
@@ -222,7 +226,7 @@ def set(msg_or_dict, key, value):
222226
"""
223227
# Sanity check: Is our target object valid?
224228
if (not isinstance(msg_or_dict,
225-
(collections.MutableMapping, message.Message))):
229+
(collections_abc.MutableMapping, message.Message))):
226230
raise TypeError(
227231
'set() expected a dict or protobuf message, got {!r}.'.format(
228232
type(msg_or_dict)))
@@ -233,12 +237,12 @@ def set(msg_or_dict, key, value):
233237
# If a subkey exists, then get that object and call this method
234238
# recursively against it using the subkey.
235239
if subkey is not None:
236-
if isinstance(msg_or_dict, collections.MutableMapping):
240+
if isinstance(msg_or_dict, collections_abc.MutableMapping):
237241
msg_or_dict.setdefault(basekey, {})
238242
set(get(msg_or_dict, basekey), subkey, value)
239243
return
240244

241-
if isinstance(msg_or_dict, collections.MutableMapping):
245+
if isinstance(msg_or_dict, collections_abc.MutableMapping):
242246
msg_or_dict[key] = value
243247
else:
244248
_set_field_on_message(msg_or_dict, key, value)

0 commit comments

Comments
 (0)