Skip to content

Commit

Permalink
Fix DeprecationWarning when accessing collections.abc classes via col…
Browse files Browse the repository at this point in the history
…lections (encode#6268)

* Use compat version of collections.abc.Mapping

Since the Mapping class will no longer be available to import directly
from the collections module in Python 3.8, we should use the
compatibility helper introduced in encode#6154 in the fields module.

* Alias and use compat version of collections.abc.MutableMapping

Since the MutableMapping class will no longer be available to import
directly from the collections module in Python 3.8, we should create an
alias for it in the compat module and use that instead.
  • Loading branch information
chornsby authored and Pierre Chiquet committed Mar 24, 2020
1 parent 9cd6bf9 commit 23fa8a6
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 9 deletions.
4 changes: 2 additions & 2 deletions rest_framework/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

try:
# Python 3
from collections.abc import Mapping # noqa
from collections.abc import Mapping, MutableMapping # noqa
except ImportError:
# Python 2.7
from collections import Mapping # noqa
from collections import Mapping, MutableMapping # noqa

try:
from django.urls import ( # noqa
Expand Down
7 changes: 3 additions & 4 deletions rest_framework/fields.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import unicode_literals

import collections
import copy
import datetime
import decimal
Expand Down Expand Up @@ -33,7 +32,7 @@

from rest_framework import ISO_8601
from rest_framework.compat import (
MaxLengthValidator, MaxValueValidator, MinLengthValidator,
Mapping, MaxLengthValidator, MaxValueValidator, MinLengthValidator,
MinValueValidator, ProhibitNullCharactersValidator, unicode_repr,
unicode_to_repr
)
Expand Down Expand Up @@ -96,7 +95,7 @@ def get_attribute(instance, attrs):
"""
for attr in attrs:
try:
if isinstance(instance, collections.Mapping):
if isinstance(instance, Mapping):
instance = instance[attr]
else:
instance = getattr(instance, attr)
Expand Down Expand Up @@ -1661,7 +1660,7 @@ def to_internal_value(self, data):
"""
if html.is_html_input(data):
data = html.parse_html_list(data, default=[])
if isinstance(data, type('')) or isinstance(data, collections.Mapping) or not hasattr(data, '__iter__'):
if isinstance(data, type('')) or isinstance(data, Mapping) or not hasattr(data, '__iter__'):
self.fail('not_a_list', input_type=type(data).__name__)
if not self.allow_empty and len(data) == 0:
self.fail('empty')
Expand Down
5 changes: 2 additions & 3 deletions rest_framework/utils/serializer_helpers.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from __future__ import unicode_literals

import collections
from collections import OrderedDict

from django.utils.encoding import force_text

from rest_framework.compat import unicode_to_repr
from rest_framework.compat import MutableMapping, unicode_to_repr
from rest_framework.utils import json


Expand Down Expand Up @@ -130,7 +129,7 @@ def as_form_field(self):
return self.__class__(self._field, values, self.errors, self._prefix)


class BindingDict(collections.MutableMapping):
class BindingDict(MutableMapping):
"""
This dict-like object is used to store fields on a serializer.
Expand Down

0 comments on commit 23fa8a6

Please sign in to comment.