Skip to content

Commit 07c5c96

Browse files
chornsbycarltongibson
authored andcommitted
Fix DeprecationWarning when accessing collections.abc classes via collections (#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 #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.
1 parent 286cf57 commit 07c5c96

File tree

3 files changed

+7
-9
lines changed

3 files changed

+7
-9
lines changed

rest_framework/compat.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@
1212

1313
try:
1414
# Python 3
15-
from collections.abc import Mapping # noqa
15+
from collections.abc import Mapping, MutableMapping # noqa
1616
except ImportError:
1717
# Python 2.7
18-
from collections import Mapping # noqa
18+
from collections import Mapping, MutableMapping # noqa
1919

2020
try:
2121
from django.urls import ( # noqa

rest_framework/fields.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from __future__ import unicode_literals
22

3-
import collections
43
import copy
54
import datetime
65
import decimal
@@ -33,7 +32,7 @@
3332

3433
from rest_framework import ISO_8601
3534
from rest_framework.compat import (
36-
MaxLengthValidator, MaxValueValidator, MinLengthValidator,
35+
Mapping, MaxLengthValidator, MaxValueValidator, MinLengthValidator,
3736
MinValueValidator, ProhibitNullCharactersValidator, unicode_repr,
3837
unicode_to_repr
3938
)
@@ -96,7 +95,7 @@ def get_attribute(instance, attrs):
9695
"""
9796
for attr in attrs:
9897
try:
99-
if isinstance(instance, collections.Mapping):
98+
if isinstance(instance, Mapping):
10099
instance = instance[attr]
101100
else:
102101
instance = getattr(instance, attr)
@@ -1661,7 +1660,7 @@ def to_internal_value(self, data):
16611660
"""
16621661
if html.is_html_input(data):
16631662
data = html.parse_html_list(data, default=[])
1664-
if isinstance(data, type('')) or isinstance(data, collections.Mapping) or not hasattr(data, '__iter__'):
1663+
if isinstance(data, type('')) or isinstance(data, Mapping) or not hasattr(data, '__iter__'):
16651664
self.fail('not_a_list', input_type=type(data).__name__)
16661665
if not self.allow_empty and len(data) == 0:
16671666
self.fail('empty')

rest_framework/utils/serializer_helpers.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
from __future__ import unicode_literals
22

3-
import collections
43
from collections import OrderedDict
54

65
from django.utils.encoding import force_text
76

8-
from rest_framework.compat import unicode_to_repr
7+
from rest_framework.compat import MutableMapping, unicode_to_repr
98
from rest_framework.utils import json
109

1110

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

132131

133-
class BindingDict(collections.MutableMapping):
132+
class BindingDict(MutableMapping):
134133
"""
135134
This dict-like object is used to store fields on a serializer.
136135

0 commit comments

Comments
 (0)