Skip to content

Commit ca80193

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 ca80193

File tree

7 files changed

+44
-17
lines changed

7 files changed

+44
-17
lines changed

graphql/execution/executor.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import collections
2+
3+
try:
4+
import collections.abc as collections_abc
5+
except ImportError:
6+
import collections as collections_abc # type: ignore
27
import functools
38
import logging
49
import sys
@@ -580,7 +585,7 @@ def complete_list_value(
580585
"""
581586
Complete a list value by completing each item in the list with the inner type
582587
"""
583-
assert isinstance(result, collections.Iterable), (
588+
assert isinstance(result, collections_abc.Iterable), (
584589
"User Error: expected iterable, but did not find one " + "for field {}.{}."
585590
).format(info.parent_type, info.field_name)
586591

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 # type: ignore
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: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import collections
2+
3+
try:
4+
import collections.abc as collections_abc
5+
except ImportError:
6+
import collections as collections_abc # type: ignore
27
import copy
38

49
from ..language import ast
@@ -234,7 +239,7 @@ def define_field_map(
234239
if callable(field_map):
235240
field_map = field_map()
236241

237-
assert isinstance(field_map, collections.Mapping) and len(field_map) > 0, (
242+
assert isinstance(field_map, collections_abc.Mapping) and len(field_map) > 0, (
238243
"{} fields must be a mapping (dict / OrderedDict) with field names as keys or a "
239244
"function which returns such a mapping."
240245
).format(type)
@@ -248,7 +253,7 @@ def define_field_map(
248253

249254
if field_args:
250255
assert isinstance(
251-
field_args, collections.Mapping
256+
field_args, collections_abc.Mapping
252257
), "{}.{} args must be a mapping (dict / OrderedDict) with argument names as keys.".format(
253258
type, field_name
254259
)
@@ -520,15 +525,15 @@ def serialize(self, value):
520525
if isinstance(value, PyEnum):
521526
# We handle PyEnum values
522527
value = value.value
523-
if isinstance(value, collections.Hashable):
528+
if isinstance(value, collections_abc.Hashable):
524529
enum_value = self._value_lookup.get(value)
525530
if enum_value:
526531
return enum_value.name
527532

528533
return None
529534

530535
def parse_value(self, value):
531-
if isinstance(value, collections.Hashable):
536+
if isinstance(value, collections_abc.Hashable):
532537
enum_value = self._name_lookup.get(value)
533538

534539
if enum_value:
@@ -555,7 +560,7 @@ def _name_lookup(self):
555560

556561
def define_enum_values(type, value_map):
557562
assert (
558-
isinstance(value_map, collections.Mapping) and len(value_map) > 0
563+
isinstance(value_map, collections_abc.Mapping) and len(value_map) > 0
559564
), "{} values must be a mapping (dict / OrderedDict) with value names as keys.".format(
560565
type
561566
)
@@ -661,7 +666,7 @@ def _define_field_map(self):
661666
else:
662667
fields = self._fields
663668

664-
assert isinstance(fields, collections.Mapping) and len(fields) > 0, (
669+
assert isinstance(fields, collections_abc.Mapping) and len(fields) > 0, (
665670
"{} fields must be a mapping (dict / OrderedDict) with field names as keys or a "
666671
"function which returns such a mapping."
667672
).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 # type: ignore
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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
from collections import OrderedDict, Sequence, defaultdict
1+
from collections import OrderedDict, defaultdict
2+
3+
try:
4+
from collections.abc import Sequence
5+
except ImportError:
6+
from collections import Sequence
27
from functools import reduce
38

49
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 # type: ignore
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)