Skip to content

Commit 9d06e43

Browse files
jdufresnecarltongibson
authored andcommitted
Replace type('') with six.text_type (encode#6482)
As all source files import unicode_literals, type('') is always equivalent to six.text_type (str on Python 3 and unicode on Python 2). Removes the need to call type(), is more explicit, and will be easier to catch places to change for when it is time to eventually drop Python 2.
1 parent dfc277c commit 9d06e43

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

rest_framework/fields.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,7 +1486,7 @@ def get_value(self, dictionary):
14861486
return dictionary.get(self.field_name, empty)
14871487

14881488
def to_internal_value(self, data):
1489-
if isinstance(data, type('')) or not hasattr(data, '__iter__'):
1489+
if isinstance(data, six.text_type) or not hasattr(data, '__iter__'):
14901490
self.fail('not_a_list', input_type=type(data).__name__)
14911491
if not self.allow_empty and len(data) == 0:
14921492
self.fail('empty')
@@ -1660,7 +1660,7 @@ def to_internal_value(self, data):
16601660
"""
16611661
if html.is_html_input(data):
16621662
data = html.parse_html_list(data, default=[])
1663-
if isinstance(data, (type(''), Mapping)) or not hasattr(data, '__iter__'):
1663+
if isinstance(data, (six.text_type, Mapping)) or not hasattr(data, '__iter__'):
16641664
self.fail('not_a_list', input_type=type(data).__name__)
16651665
if not self.allow_empty and len(data) == 0:
16661666
self.fail('empty')

rest_framework/relations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -518,7 +518,7 @@ def get_value(self, dictionary):
518518
return dictionary.get(self.field_name, empty)
519519

520520
def to_internal_value(self, data):
521-
if isinstance(data, type('')) or not hasattr(data, '__iter__'):
521+
if isinstance(data, six.text_type) or not hasattr(data, '__iter__'):
522522
self.fail('not_a_list', input_type=type(data).__name__)
523523
if not self.allow_empty and len(data) == 0:
524524
self.fail('empty')

tests/test_pagination.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from django.core.paginator import Paginator as DjangoPaginator
66
from django.db import models
77
from django.test import TestCase
8+
from django.utils import six
89

910
from rest_framework import (
1011
exceptions, filters, generics, pagination, serializers, status
@@ -207,7 +208,7 @@ def test_no_page_number(self):
207208
]
208209
}
209210
assert self.pagination.display_page_controls
210-
assert isinstance(self.pagination.to_html(), type(''))
211+
assert isinstance(self.pagination.to_html(), six.text_type)
211212

212213
def test_second_page(self):
213214
request = Request(factory.get('/', {'page': 2}))
@@ -313,7 +314,7 @@ def test_no_page_number(self):
313314
]
314315
}
315316
assert not self.pagination.display_page_controls
316-
assert isinstance(self.pagination.to_html(), type(''))
317+
assert isinstance(self.pagination.to_html(), six.text_type)
317318

318319
def test_invalid_page(self):
319320
request = Request(factory.get('/', {'page': 'invalid'}))
@@ -368,7 +369,7 @@ def test_no_offset(self):
368369
]
369370
}
370371
assert self.pagination.display_page_controls
371-
assert isinstance(self.pagination.to_html(), type(''))
372+
assert isinstance(self.pagination.to_html(), six.text_type)
372373

373374
def test_pagination_not_applied_if_limit_or_default_limit_not_set(self):
374375
class MockPagination(pagination.LimitOffsetPagination):
@@ -631,7 +632,7 @@ def test_cursor_pagination(self):
631632
assert current == [1, 1, 1, 1, 1]
632633
assert next == [1, 2, 3, 4, 4]
633634

634-
assert isinstance(self.pagination.to_html(), type(''))
635+
assert isinstance(self.pagination.to_html(), six.text_type)
635636

636637
def test_cursor_pagination_with_page_size(self):
637638
(previous, current, next, previous_url, next_url) = self.get_pages('/?page_size=20')

tests/test_validation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from django.core.validators import MaxValueValidator, RegexValidator
66
from django.db import models
77
from django.test import TestCase
8+
from django.utils import six
89

910
from rest_framework import generics, serializers, status
1011
from rest_framework.test import APIRequestFactory
@@ -111,7 +112,7 @@ def test_serializer_errors_has_only_invalid_data_error(self):
111112
assert not serializer.is_valid()
112113
assert serializer.errors == {
113114
'non_field_errors': [
114-
'Invalid data. Expected a dictionary, but got %s.' % type('').__name__
115+
'Invalid data. Expected a dictionary, but got %s.' % six.text_type.__name__
115116
]
116117
}
117118

0 commit comments

Comments
 (0)