diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f81d85e..f68ebeba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ any parts of the framework not mentioned in the documentation should generally b * Avoid exception when trying to include skipped relationship * Don't swallow `filter[]` params when there are several +* Fix DeprecationWarning regarding collections.abc import in Python 3.7 ## [2.7.0] - 2019-01-14 diff --git a/rest_framework_json_api/compat.py b/rest_framework_json_api/compat.py new file mode 100644 index 00000000..577a91e0 --- /dev/null +++ b/rest_framework_json_api/compat.py @@ -0,0 +1,4 @@ +try: + import collections.abc as collections_abc # noqa: F401 +except ImportError: + import collections as collections_abc # noqa: F401 diff --git a/rest_framework_json_api/relations.py b/rest_framework_json_api/relations.py index 82b94cd5..942c61d9 100644 --- a/rest_framework_json_api/relations.py +++ b/rest_framework_json_api/relations.py @@ -1,4 +1,3 @@ -import collections import json from collections import OrderedDict @@ -14,6 +13,7 @@ from rest_framework.reverse import reverse from rest_framework.serializers import Serializer +from rest_framework_json_api.compat import collections_abc from rest_framework_json_api.exceptions import Conflict from rest_framework_json_api.utils import ( Hyperlink, @@ -388,7 +388,7 @@ def get_attribute(self, instance): return super(SerializerMethodResourceRelatedField, self).get_attribute(instance) def to_representation(self, value): - if isinstance(value, collections.Iterable): + if isinstance(value, collections_abc.Iterable): base = super(SerializerMethodResourceRelatedField, self) return [base.to_representation(x) for x in value] return super(SerializerMethodResourceRelatedField, self).to_representation(value) diff --git a/rest_framework_json_api/renderers.py b/rest_framework_json_api/renderers.py index d83fcdf0..0023399a 100644 --- a/rest_framework_json_api/renderers.py +++ b/rest_framework_json_api/renderers.py @@ -2,7 +2,7 @@ Renderers """ import copy -from collections import Iterable, OrderedDict, defaultdict +from collections import OrderedDict, defaultdict import inflection from django.db.models import Manager @@ -13,6 +13,7 @@ import rest_framework_json_api from rest_framework_json_api import utils +from rest_framework_json_api.compat import collections_abc from rest_framework_json_api.relations import HyperlinkedMixin, ResourceRelatedField, SkipDataMixin @@ -196,7 +197,7 @@ def extract_relationships(cls, fields, resource, resource_instance): relation_data = {} - if isinstance(resource.get(field_name), Iterable): + if isinstance(resource.get(field_name), collections_abc.Iterable): relation_data.update( { 'meta': {'count': len(resource.get(field_name))} diff --git a/rest_framework_json_api/views.py b/rest_framework_json_api/views.py index b692844b..4b4c6ce3 100644 --- a/rest_framework_json_api/views.py +++ b/rest_framework_json_api/views.py @@ -1,4 +1,3 @@ -from collections import Iterable from django.core.exceptions import ImproperlyConfigured from django.db.models import Model @@ -20,6 +19,7 @@ from rest_framework.reverse import reverse from rest_framework.serializers import Serializer, SkipField +from rest_framework_json_api.compat import collections_abc from rest_framework_json_api.exceptions import Conflict from rest_framework_json_api.serializers import ResourceIdentifierObjectSerializer from rest_framework_json_api.utils import ( @@ -127,7 +127,7 @@ def retrieve_related(self, request, *args, **kwargs): if instance is None: return Response(data=None) - if isinstance(instance, Iterable): + if isinstance(instance, collections_abc.Iterable): serializer_kwargs['many'] = True serializer = self.get_serializer(instance, **serializer_kwargs)