Skip to content

Commit db5cd72

Browse files
tirkarthisliverc
authored andcommitted
Fix deprecation warnings regarding collections.abc (#624)
1 parent 4c22087 commit db5cd72

File tree

5 files changed

+12
-6
lines changed

5 files changed

+12
-6
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ any parts of the framework not mentioned in the documentation should generally b
1414

1515
* Avoid exception when trying to include skipped relationship
1616
* Don't swallow `filter[]` params when there are several
17+
* Fix DeprecationWarning regarding collections.abc import in Python 3.7
1718

1819
## [2.7.0] - 2019-01-14
1920

rest_framework_json_api/compat.py

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
try:
2+
import collections.abc as collections_abc # noqa: F401
3+
except ImportError:
4+
import collections as collections_abc # noqa: F401

rest_framework_json_api/relations.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import collections
21
import json
32
from collections import OrderedDict
43

@@ -14,6 +13,7 @@
1413
from rest_framework.reverse import reverse
1514
from rest_framework.serializers import Serializer
1615

16+
from rest_framework_json_api.compat import collections_abc
1717
from rest_framework_json_api.exceptions import Conflict
1818
from rest_framework_json_api.utils import (
1919
Hyperlink,
@@ -388,7 +388,7 @@ def get_attribute(self, instance):
388388
return super(SerializerMethodResourceRelatedField, self).get_attribute(instance)
389389

390390
def to_representation(self, value):
391-
if isinstance(value, collections.Iterable):
391+
if isinstance(value, collections_abc.Iterable):
392392
base = super(SerializerMethodResourceRelatedField, self)
393393
return [base.to_representation(x) for x in value]
394394
return super(SerializerMethodResourceRelatedField, self).to_representation(value)

rest_framework_json_api/renderers.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
Renderers
33
"""
44
import copy
5-
from collections import Iterable, OrderedDict, defaultdict
5+
from collections import OrderedDict, defaultdict
66

77
import inflection
88
from django.db.models import Manager
@@ -13,6 +13,7 @@
1313

1414
import rest_framework_json_api
1515
from rest_framework_json_api import utils
16+
from rest_framework_json_api.compat import collections_abc
1617
from rest_framework_json_api.relations import HyperlinkedMixin, ResourceRelatedField, SkipDataMixin
1718

1819

@@ -196,7 +197,7 @@ def extract_relationships(cls, fields, resource, resource_instance):
196197

197198
relation_data = {}
198199

199-
if isinstance(resource.get(field_name), Iterable):
200+
if isinstance(resource.get(field_name), collections_abc.Iterable):
200201
relation_data.update(
201202
{
202203
'meta': {'count': len(resource.get(field_name))}

rest_framework_json_api/views.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from collections import Iterable
21

32
from django.core.exceptions import ImproperlyConfigured
43
from django.db.models import Model
@@ -20,6 +19,7 @@
2019
from rest_framework.reverse import reverse
2120
from rest_framework.serializers import Serializer, SkipField
2221

22+
from rest_framework_json_api.compat import collections_abc
2323
from rest_framework_json_api.exceptions import Conflict
2424
from rest_framework_json_api.serializers import ResourceIdentifierObjectSerializer
2525
from rest_framework_json_api.utils import (
@@ -127,7 +127,7 @@ def retrieve_related(self, request, *args, **kwargs):
127127
if instance is None:
128128
return Response(data=None)
129129

130-
if isinstance(instance, Iterable):
130+
if isinstance(instance, collections_abc.Iterable):
131131
serializer_kwargs['many'] = True
132132

133133
serializer = self.get_serializer(instance, **serializer_kwargs)

0 commit comments

Comments
 (0)