Skip to content

Fix deprecation warnings regarding collections.abc #624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions rest_framework_json_api/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
try:
import collections.abc as collections_abc # noqa: F401
except ImportError:
import collections as collections_abc # noqa: F401
4 changes: 2 additions & 2 deletions rest_framework_json_api/relations.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import collections
import json
from collections import OrderedDict

Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions rest_framework_json_api/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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


Expand Down Expand Up @@ -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))}
Expand Down
4 changes: 2 additions & 2 deletions rest_framework_json_api/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from collections import Iterable

from django.core.exceptions import ImproperlyConfigured
from django.db.models import Model
Expand All @@ -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 (
Expand Down Expand Up @@ -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)
Expand Down