From bcf64e57af56efb7df134eb882957f3f814eb270 Mon Sep 17 00:00:00 2001 From: Asif Saif Uddin Date: Sun, 10 Mar 2019 13:02:19 +0600 Subject: [PATCH] remove more python2 compat codes --- rest_framework/compat.py | 11 +++-------- rest_framework/fields.py | 12 ++++++------ rest_framework/relations.py | 2 +- rest_framework/renderers.py | 2 +- rest_framework/routers.py | 4 ++-- rest_framework/settings.py | 2 +- rest_framework/test.py | 2 +- rest_framework/throttling.py | 2 +- rest_framework/validators.py | 6 +++--- rest_framework/versioning.py | 2 +- rest_framework/viewsets.py | 2 +- tests/authentication/migrations/0001_initial.py | 3 --- tests/authentication/models.py | 3 --- tests/authentication/test_authentication.py | 2 +- tests/generic_relations/models.py | 4 ---- 15 files changed, 22 insertions(+), 37 deletions(-) diff --git a/rest_framework/compat.py b/rest_framework/compat.py index f4b54c3fc..fbb6a2863 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -10,12 +10,7 @@ from django.core import validators from django.utils import six from django.views.generic import View -try: - # Python 3 - from collections.abc import Mapping, MutableMapping # noqa -except ImportError: - # Python 2.7 - from collections import Mapping, MutableMapping # noqa +from collections.abc import Mapping, MutableMapping # noqa try: from django.urls import ( # noqa @@ -292,7 +287,7 @@ else: INDENT_SEPARATORS = (b',', b': ') -class CustomValidatorMessage(object): +class CustomValidatorMessage: """ We need to avoid evaluation of `lazy` translated `message` in `django.core.validators.BaseValidator.__init__`. https://github.com/django/django/blob/75ed5900321d170debef4ac452b8b3cf8a1c2384/django/core/validators.py#L297 @@ -302,7 +297,7 @@ class CustomValidatorMessage(object): def __init__(self, *args, **kwargs): self.message = kwargs.pop('message', self.message) - super(CustomValidatorMessage, self).__init__(*args, **kwargs) + super().__init__(*args, **kwargs) class MinValueValidator(CustomValidatorMessage, validators.MinValueValidator): diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 7bbb2aeef..1b3fe4230 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -183,18 +183,18 @@ def iter_options(grouped_choices, cutoff=None, cutoff_text=None): """ Helper function for options and option groups in templates. """ - class StartOptionGroup(object): + class StartOptionGroup: start_option_group = True end_option_group = False def __init__(self, label): self.label = label - class EndOptionGroup(object): + class EndOptionGroup: start_option_group = False end_option_group = True - class Option(object): + class Option: start_option_group = False end_option_group = False @@ -249,7 +249,7 @@ def get_error_detail(exc_info): } -class CreateOnlyDefault(object): +class CreateOnlyDefault: """ This class may be used to provide default values that are only used for create operations, but that do not return any value for update @@ -276,7 +276,7 @@ class CreateOnlyDefault(object): ) -class CurrentUserDefault(object): +class CurrentUserDefault: def set_context(self, serializer_field): self.user = serializer_field.context['request'].user @@ -303,7 +303,7 @@ MISSING_ERROR_MESSAGE = ( ) -class Field(object): +class Field: _creation_counter = 0 default_error_messages = { diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 614a453f9..4c7f9ed5d 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -65,7 +65,7 @@ class Hyperlink(six.text_type): is_hyperlink = True -class PKOnlyObject(object): +class PKOnlyObject: """ This is a mock object, used for when we only need the pk of the object instance, but still want to return an object with a .pk attribute, diff --git a/rest_framework/renderers.py b/rest_framework/renderers.py index 4d88ed717..15b4c6d00 100644 --- a/rest_framework/renderers.py +++ b/rest_framework/renderers.py @@ -39,7 +39,7 @@ def zero_as_none(value): return None if value == 0 else value -class BaseRenderer(object): +class BaseRenderer: """ All renderers should extend this class, setting the `media_type` and `format` attributes, and override the `.render()` method. diff --git a/rest_framework/routers.py b/rest_framework/routers.py index 8926b1000..50918f941 100644 --- a/rest_framework/routers.py +++ b/rest_framework/routers.py @@ -38,7 +38,7 @@ Route = namedtuple('Route', ['url', 'mapping', 'name', 'detail', 'initkwargs']) DynamicRoute = namedtuple('DynamicRoute', ['url', 'name', 'detail', 'initkwargs']) -class DynamicDetailRoute(object): +class DynamicDetailRoute: def __new__(cls, url, name, initkwargs): warnings.warn( "`DynamicDetailRoute` is deprecated and will be removed in 3.10 " @@ -49,7 +49,7 @@ class DynamicDetailRoute(object): return DynamicRoute(url, name, True, initkwargs) -class DynamicListRoute(object): +class DynamicListRoute: def __new__(cls, url, name, initkwargs): warnings.warn( "`DynamicListRoute` is deprecated and will be removed in 3.10 in " diff --git a/rest_framework/settings.py b/rest_framework/settings.py index f04a6b57f..d31cf7022 100644 --- a/rest_framework/settings.py +++ b/rest_framework/settings.py @@ -186,7 +186,7 @@ def import_from_string(val, setting_name): raise ImportError(msg) -class APISettings(object): +class APISettings: """ A settings object, that allows API settings to be accessed as properties. For example: diff --git a/rest_framework/test.py b/rest_framework/test.py index b319a4fc5..d7e9ffd1f 100644 --- a/rest_framework/test.py +++ b/rest_framework/test.py @@ -30,7 +30,7 @@ if requests is not None: def get_all(self, key, default): return self.getheaders(key) - class MockOriginalResponse(object): + class MockOriginalResponse: def __init__(self, headers): self.msg = HeaderDict(headers) self.closed = False diff --git a/rest_framework/throttling.py b/rest_framework/throttling.py index 3bb14eacc..6c497edc5 100644 --- a/rest_framework/throttling.py +++ b/rest_framework/throttling.py @@ -10,7 +10,7 @@ from django.core.exceptions import ImproperlyConfigured from rest_framework.settings import api_settings -class BaseThrottle(object): +class BaseThrottle: """ Rate throttling of requests. """ diff --git a/rest_framework/validators.py b/rest_framework/validators.py index df8ac8bcd..1976f0bc5 100644 --- a/rest_framework/validators.py +++ b/rest_framework/validators.py @@ -32,7 +32,7 @@ def qs_filter(queryset, **kwargs): return queryset.none() -class UniqueValidator(object): +class UniqueValidator: """ Validator that corresponds to `unique=True` on a model field. @@ -87,7 +87,7 @@ class UniqueValidator(object): )) -class UniqueTogetherValidator(object): +class UniqueTogetherValidator: """ Validator that corresponds to `unique_together = (...)` on a model class. @@ -176,7 +176,7 @@ class UniqueTogetherValidator(object): )) -class BaseUniqueForValidator(object): +class BaseUniqueForValidator: message = None missing_message = _('This field is required.') diff --git a/rest_framework/versioning.py b/rest_framework/versioning.py index d7401f1cd..d776df8ce 100644 --- a/rest_framework/versioning.py +++ b/rest_framework/versioning.py @@ -10,7 +10,7 @@ from rest_framework.templatetags.rest_framework import replace_query_param from rest_framework.utils.mediatypes import _MediaType -class BaseVersioning(object): +class BaseVersioning: default_version = api_settings.DEFAULT_VERSION allowed_versions = api_settings.ALLOWED_VERSIONS version_param = api_settings.VERSION_PARAM diff --git a/rest_framework/viewsets.py b/rest_framework/viewsets.py index fa99e6e3d..68d661139 100644 --- a/rest_framework/viewsets.py +++ b/rest_framework/viewsets.py @@ -33,7 +33,7 @@ def _is_extra_action(attr): return hasattr(attr, 'mapping') -class ViewSetMixin(object): +class ViewSetMixin: """ This is the magic. diff --git a/tests/authentication/migrations/0001_initial.py b/tests/authentication/migrations/0001_initial.py index cfc887240..548b3576b 100644 --- a/tests/authentication/migrations/0001_initial.py +++ b/tests/authentication/migrations/0001_initial.py @@ -1,6 +1,3 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - from django.conf import settings from django.db import migrations, models diff --git a/tests/authentication/models.py b/tests/authentication/models.py index b8d1fd5a6..1a721de4d 100644 --- a/tests/authentication/models.py +++ b/tests/authentication/models.py @@ -1,6 +1,3 @@ -# coding: utf-8 -from __future__ import unicode_literals - from django.conf import settings from django.db import models diff --git a/tests/authentication/test_authentication.py b/tests/authentication/test_authentication.py index de5b2fadf..8e7ccb992 100644 --- a/tests/authentication/test_authentication.py +++ b/tests/authentication/test_authentication.py @@ -249,7 +249,7 @@ class SessionAuthTests(TestCase): assert response.status_code == status.HTTP_403_FORBIDDEN -class BaseTokenAuthTests(object): +class BaseTokenAuthTests: """Token authentication""" model = None path = None diff --git a/tests/generic_relations/models.py b/tests/generic_relations/models.py index 55bc243cb..d0e2e9b55 100644 --- a/tests/generic_relations/models.py +++ b/tests/generic_relations/models.py @@ -5,10 +5,8 @@ from django.contrib.contenttypes.fields import ( ) from django.contrib.contenttypes.models import ContentType from django.db import models -from django.utils.encoding import python_2_unicode_compatible -@python_2_unicode_compatible class Tag(models.Model): """ Tags have a descriptive slug, and are attached to an arbitrary object. @@ -22,7 +20,6 @@ class Tag(models.Model): return self.tag -@python_2_unicode_compatible class Bookmark(models.Model): """ A URL bookmark that may have multiple tags attached. @@ -34,7 +31,6 @@ class Bookmark(models.Model): return 'Bookmark: %s' % self.url -@python_2_unicode_compatible class Note(models.Model): """ A textual note that may have multiple tags attached.