Skip to content

Commit 905c272

Browse files
committed
Fix Python 3.7 deprecation warnings
Importing ABCs directly from the `collections` module is deprecated in Python 3.7.
1 parent 9202021 commit 905c272

File tree

7 files changed

+41
-17
lines changed

7 files changed

+41
-17
lines changed

graphql/execution/executor.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import collections
2+
try:
3+
import collections.abc as collections_abc
4+
except ImportError:
5+
import collections as collections_abc
26
import functools
37
import logging
48
import sys
@@ -580,7 +584,7 @@ def complete_list_value(
580584
"""
581585
Complete a list value by completing each item in the list with the inner type
582586
"""
583-
assert isinstance(result, collections.Iterable), (
587+
assert isinstance(result, collections_abc.Iterable), (
584588
"User Error: expected iterable, but did not find one " + "for field {}.{}."
585589
).format(info.parent_type, info.field_name)
586590

graphql/execution/values.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import collections
1+
try:
2+
import collections.abc as collections_abc
3+
except ImportError:
4+
import collections as collections_abc
25
import json
36

47
from six import string_types
@@ -160,7 +163,7 @@ def coerce_value(type, value):
160163
if isinstance(type, GraphQLList):
161164
item_type = type.of_type
162165
if not isinstance(value, string_types) and isinstance(
163-
value, collections.Iterable
166+
value, collections_abc.Iterable
164167
):
165168
return [coerce_value(item_type, item) for item in value]
166169
else:

graphql/type/definition.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import collections
2+
try:
3+
import collections.abc as collections_abc
4+
except ImportError:
5+
import collections as collections_abc
26
import copy
37

48
from ..language import ast
@@ -234,7 +238,7 @@ def define_field_map(
234238
if callable(field_map):
235239
field_map = field_map()
236240

237-
assert isinstance(field_map, collections.Mapping) and len(field_map) > 0, (
241+
assert isinstance(field_map, collections_abc.Mapping) and len(field_map) > 0, (
238242
"{} fields must be a mapping (dict / OrderedDict) with field names as keys or a "
239243
"function which returns such a mapping."
240244
).format(type)
@@ -248,7 +252,7 @@ def define_field_map(
248252

249253
if field_args:
250254
assert isinstance(
251-
field_args, collections.Mapping
255+
field_args, collections_abc.Mapping
252256
), "{}.{} args must be a mapping (dict / OrderedDict) with argument names as keys.".format(
253257
type, field_name
254258
)
@@ -520,15 +524,15 @@ def serialize(self, value):
520524
if isinstance(value, PyEnum):
521525
# We handle PyEnum values
522526
value = value.value
523-
if isinstance(value, collections.Hashable):
527+
if isinstance(value, collections_abc.Hashable):
524528
enum_value = self._value_lookup.get(value)
525529
if enum_value:
526530
return enum_value.name
527531

528532
return None
529533

530534
def parse_value(self, value):
531-
if isinstance(value, collections.Hashable):
535+
if isinstance(value, collections_abc.Hashable):
532536
enum_value = self._name_lookup.get(value)
533537

534538
if enum_value:
@@ -555,7 +559,7 @@ def _name_lookup(self):
555559

556560
def define_enum_values(type, value_map):
557561
assert (
558-
isinstance(value_map, collections.Mapping) and len(value_map) > 0
562+
isinstance(value_map, collections_abc.Mapping) and len(value_map) > 0
559563
), "{} values must be a mapping (dict / OrderedDict) with value names as keys.".format(
560564
type
561565
)
@@ -661,7 +665,7 @@ def _define_field_map(self):
661665
else:
662666
fields = self._fields
663667

664-
assert isinstance(fields, collections.Mapping) and len(fields) > 0, (
668+
assert isinstance(fields, collections_abc.Mapping) and len(fields) > 0, (
665669
"{} fields must be a mapping (dict / OrderedDict) with field names as keys or a "
666670
"function which returns such a mapping."
667671
).format(self)

graphql/type/directives.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import collections
1+
try:
2+
import collections.abc as collections_abc
3+
except ImportError:
4+
import collections as collections_abc
25

36
from ..pyutils.ordereddict import OrderedDict
47
from ..utils.assert_valid_name import assert_valid_name
@@ -43,7 +46,7 @@ def __init__(self, name, description=None, args=None, locations=None):
4346
assert name, "Directive must be named."
4447
assert_valid_name(name)
4548
assert isinstance(
46-
locations, collections.Iterable
49+
locations, collections_abc.Iterable
4750
), "Must provide locations for directive."
4851

4952
self.name = name
@@ -52,7 +55,7 @@ def __init__(self, name, description=None, args=None, locations=None):
5255

5356
if args:
5457
assert isinstance(
55-
args, collections.Mapping
58+
args, collections_abc.Mapping
5659
), "{} args must be a dict with argument names as keys.".format(name)
5760
for arg_name, _arg in args.items():
5861
assert_valid_name(arg_name)

graphql/type/schema.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
from collections import Iterable
1+
try:
2+
from collections.abc import Iterable
3+
except ImportError:
4+
from collections import Iterable
25

36
from .definition import GraphQLObjectType
47
from .directives import GraphQLDirective, specified_directives

graphql/type/typemap.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
from collections import OrderedDict, Sequence, defaultdict
1+
from collections import OrderedDict, defaultdict
2+
try:
3+
from collections.abc import Sequence
4+
except ImportError:
5+
from collections import Sequence
26
from functools import reduce
37

48
from ..utils.type_comparators import is_equal_type, is_type_sub_type_of

graphql/utils/is_valid_value.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
Implementation of isValidJSValue from graphql.s
33
"""
44

5-
import collections
5+
try:
6+
import collections.abc as collections_abc
7+
except ImportError:
8+
import collections as collections_abc
69
import json
710

811
from six import string_types
@@ -38,7 +41,7 @@ def is_valid_value(value, type):
3841
if isinstance(type, GraphQLList):
3942
item_type = type.of_type
4043
if not isinstance(value, string_types) and isinstance(
41-
value, collections.Iterable
44+
value, collections_abc.Iterable
4245
):
4346
errors = []
4447
for i, item in enumerate(value):
@@ -52,7 +55,7 @@ def is_valid_value(value, type):
5255
return is_valid_value(value, item_type)
5356

5457
if isinstance(type, GraphQLInputObjectType):
55-
if not isinstance(value, collections.Mapping):
58+
if not isinstance(value, collections_abc.Mapping):
5659
return [u'Expected "{}", found not an object.'.format(type)]
5760

5861
fields = type.fields

0 commit comments

Comments
 (0)