Skip to content

Commit f99f625

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

File tree

6 files changed

+39
-19
lines changed

6 files changed

+39
-19
lines changed

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)

bigquery/google/cloud/bigquery/client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,11 @@
1616

1717
from __future__ import absolute_import
1818

19-
import collections
19+
try:
20+
from collections import abc as collections_abc
21+
except ImportError: # Python 2.7
22+
import collections as collections_abc
23+
2024
import functools
2125
import gzip
2226
import os
@@ -1232,7 +1236,7 @@ def copy_table(
12321236
destination = TableReference.from_string(
12331237
destination, default_project=self.project)
12341238

1235-
if not isinstance(sources, collections.Sequence):
1239+
if not isinstance(sources, collections_abc.Sequence):
12361240
sources = [sources]
12371241

12381242
copy_job = job.CopyJob(

bigquery/google/cloud/bigquery/dbapi/_helpers.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import collections
15+
try:
16+
from collections import abc as collections_abc
17+
except ImportError: # Python 2.7
18+
import collections as collections_abc
19+
1620
import datetime
1721
import decimal
1822
import numbers
@@ -105,7 +109,7 @@ def to_query_parameters(parameters):
105109
if parameters is None:
106110
return []
107111

108-
if isinstance(parameters, collections.Mapping):
112+
if isinstance(parameters, collections_abc.Mapping):
109113
return to_query_parameters_dict(parameters)
110114

111115
return to_query_parameters_list(parameters)

bigquery/google/cloud/bigquery/dbapi/cursor.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
import collections
1818

19+
try:
20+
from collections import abc as collections_abc
21+
except ImportError: # Python 2.7
22+
import collections as collections_abc
23+
1924
import six
2025

2126
from google.cloud.bigquery import job
@@ -335,7 +340,7 @@ def _format_operation(operation, parameters=None):
335340
if parameters is None:
336341
return operation
337342

338-
if isinstance(parameters, collections.Mapping):
343+
if isinstance(parameters, collections_abc.Mapping):
339344
return _format_operation_dict(operation, parameters)
340345

341346
return _format_operation_list(operation, parameters)

core/google/cloud/iam.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
"""
1919

2020
import collections
21+
try:
22+
from collections import abc as collections_abc
23+
except ImportError: # Python 2.7
24+
import collections as collections_abc
2125
import warnings
2226

2327
# Generic IAM roles
@@ -35,7 +39,7 @@
3539
Assigning to '{}' is deprecated. Replace with 'policy[{}] = members."""
3640

3741

38-
class Policy(collections.MutableMapping):
42+
class Policy(collections_abc.MutableMapping):
3943
"""IAM Policy
4044
4145
See
@@ -239,4 +243,4 @@ def to_api_repr(self):
239243
return resource
240244

241245

242-
collections.MutableMapping.register(Policy)
246+
collections_abc.MutableMapping.register(Policy)

firestore/google/cloud/firestore_v1beta1/_helpers.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@
1414

1515
"""Common helpers shared across Google Cloud Firestore modules."""
1616

17-
1817
try:
19-
from collections import abc
20-
except ImportError: # python 2.7
21-
import collections as abc
18+
from collections import abc as collections_abc
19+
except ImportError: # Python 2.7
20+
import collections as collections_abc
2221

2322
import datetime
2423
import re
@@ -749,7 +748,7 @@ def get_nested_value(field_path, data):
749748

750749
nested_data = data
751750
for index, field_name in enumerate(field_names):
752-
if isinstance(nested_data, abc.Mapping):
751+
if isinstance(nested_data, collections_abc.Mapping):
753752
if field_name in nested_data:
754753
nested_data = nested_data[field_name]
755754
else:

0 commit comments

Comments
 (0)