From 83c9136c9013c1ae5d484739b9648c2cd4d8469b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Padilla?= Date: Thu, 18 Jun 2015 09:38:29 -0400 Subject: [PATCH 1/3] Cleanup import following PEP 8 style guide --- rest_framework/authentication.py | 7 +++- rest_framework/authtoken/admin.py | 1 + rest_framework/authtoken/models.py | 4 +- rest_framework/authtoken/views.py | 3 +- rest_framework/compat.py | 11 ++++-- rest_framework/decorators.py | 7 +++- rest_framework/exceptions.py | 5 ++- rest_framework/fields.py | 38 ++++++++++--------- rest_framework/filters.py | 15 +++++--- rest_framework/generics.py | 4 +- rest_framework/metadata.py | 3 +- rest_framework/mixins.py | 1 + rest_framework/negotiation.py | 7 +++- rest_framework/pagination.py | 13 ++++--- rest_framework/parsers.py | 24 +++++++----- rest_framework/permissions.py | 3 ++ rest_framework/relations.py | 16 +++++--- rest_framework/renderers.py | 22 ++++++----- rest_framework/request.py | 12 +++--- rest_framework/response.py | 3 +- rest_framework/reverse.py | 4 +- rest_framework/routers.py | 8 ++-- rest_framework/serializers.py | 22 ++++++----- rest_framework/settings.py | 7 +++- rest_framework/templatetags/rest_framework.py | 12 ++++-- rest_framework/test.py | 14 ++++--- rest_framework/throttling.py | 5 ++- rest_framework/urlpatterns.py | 2 + rest_framework/urls.py | 1 + rest_framework/utils/breadcrumbs.py | 1 + rest_framework/utils/encoders.py | 19 ++++++---- rest_framework/utils/field_mapping.py | 6 ++- rest_framework/utils/formatting.py | 5 ++- rest_framework/utils/html.py | 1 + rest_framework/utils/mediatypes.py | 2 + rest_framework/utils/model_meta.py | 12 +++--- rest_framework/utils/representation.py | 11 ++++-- rest_framework/utils/serializer_helpers.py | 2 + rest_framework/validators.py | 2 + rest_framework/versioning.py | 9 +++-- rest_framework/views.py | 27 +++++++------ rest_framework/viewsets.py | 2 + setup.py | 4 +- tests/models.py | 1 + 44 files changed, 236 insertions(+), 142 deletions(-) diff --git a/rest_framework/authentication.py b/rest_framework/authentication.py index 598c72788..74c6b342b 100644 --- a/rest_framework/authentication.py +++ b/rest_framework/authentication.py @@ -2,13 +2,16 @@ Provides various authentication policies. """ from __future__ import unicode_literals + import base64 + from django.contrib.auth import authenticate from django.middleware.csrf import CsrfViewMiddleware from django.utils.translation import ugettext_lazy as _ -from rest_framework import exceptions, HTTP_HEADER_ENCODING -from rest_framework.authtoken.models import Token + from rest_framework.compat import get_user_model +from rest_framework.authtoken.models import Token +from rest_framework import exceptions, HTTP_HEADER_ENCODING def get_authorization_header(request): diff --git a/rest_framework/authtoken/admin.py b/rest_framework/authtoken/admin.py index ec28eb1ca..1a507249b 100644 --- a/rest_framework/authtoken/admin.py +++ b/rest_framework/authtoken/admin.py @@ -1,4 +1,5 @@ from django.contrib import admin + from rest_framework.authtoken.models import Token diff --git a/rest_framework/authtoken/models.py b/rest_framework/authtoken/models.py index a1a9315fa..f3e01a4b7 100644 --- a/rest_framework/authtoken/models.py +++ b/rest_framework/authtoken/models.py @@ -1,8 +1,8 @@ -import binascii import os +import binascii -from django.conf import settings from django.db import models +from django.conf import settings from django.utils.encoding import python_2_unicode_compatible diff --git a/rest_framework/authtoken/views.py b/rest_framework/authtoken/views.py index 66bbc49b7..26df9e11c 100644 --- a/rest_framework/authtoken/views.py +++ b/rest_framework/authtoken/views.py @@ -1,6 +1,5 @@ +from rest_framework import parsers, renderers from rest_framework.views import APIView -from rest_framework import parsers -from rest_framework import renderers from rest_framework.response import Response from rest_framework.authtoken.models import Token from rest_framework.authtoken.serializers import AuthTokenSerializer diff --git a/rest_framework/compat.py b/rest_framework/compat.py index e7a73adda..28e72cfe8 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -5,14 +5,17 @@ versions of django/python, and compatibility wrappers around optional packages. # flake8: noqa from __future__ import unicode_literals -from django.core.exceptions import ImproperlyConfigured + +import inspect + +import django +from django.utils import six from django.conf import settings from django.db import connection, transaction from django.utils.encoding import force_text +from django.core.exceptions import ImproperlyConfigured from django.utils.six.moves.urllib.parse import urlparse as _urlparse -from django.utils import six -import django -import inspect + try: import importlib except ImportError: diff --git a/rest_framework/decorators.py b/rest_framework/decorators.py index 21de1acf4..4c8bda39c 100644 --- a/rest_framework/decorators.py +++ b/rest_framework/decorators.py @@ -7,10 +7,13 @@ based views, as well as the `@detail_route` and `@list_route` decorators, which used to annotate methods on viewsets that should be included by routers. """ from __future__ import unicode_literals -from django.utils import six -from rest_framework.views import APIView + import types +from django.utils import six + +from rest_framework.views import APIView + def api_view(http_method_names=None): diff --git a/rest_framework/exceptions.py b/rest_framework/exceptions.py index f954c13e5..9a467f987 100644 --- a/rest_framework/exceptions.py +++ b/rest_framework/exceptions.py @@ -5,11 +5,14 @@ In addition Django's built in 403 and 404 exceptions are handled. (`django.http.Http404` and `django.core.exceptions.PermissionDenied`) """ from __future__ import unicode_literals + +import math + from django.utils import six from django.utils.encoding import force_text from django.utils.translation import ugettext_lazy as _, ungettext + from rest_framework import status -import math def _force_text_recursive(data): diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 275c7a9a7..3794329b7 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -1,30 +1,34 @@ from __future__ import unicode_literals + +import re +import copy +import uuid +import decimal +import inspect +import datetime +import collections + from django.conf import settings -from django.core.exceptions import ObjectDoesNotExist -from django.core.exceptions import ValidationError as DjangoValidationError -from django.core.validators import RegexValidator, ip_address_validators -from django.forms import ImageField as DjangoImageField from django.utils import six, timezone -from django.utils.dateparse import parse_date, parse_datetime, parse_time -from django.utils.encoding import is_protected_type, smart_text -from django.utils.translation import ugettext_lazy as _ from django.utils.ipv6 import clean_ipv6_address +from django.utils.translation import ugettext_lazy as _ +from django.forms import ImageField as DjangoImageField +from django.utils.encoding import is_protected_type, smart_text +from django.core.validators import RegexValidator, ip_address_validators +from django.utils.dateparse import parse_date, parse_datetime, parse_time +from django.core.exceptions import ( + ObjectDoesNotExist, ValidationError as DjangoValidationError +) + from rest_framework import ISO_8601 +from rest_framework.settings import api_settings +from rest_framework.exceptions import ValidationError +from rest_framework.utils import html, representation, humanize_datetime from rest_framework.compat import ( EmailValidator, MinValueValidator, MaxValueValidator, MinLengthValidator, MaxLengthValidator, URLValidator, OrderedDict, unicode_repr, unicode_to_repr, parse_duration, duration_string, ) -from rest_framework.exceptions import ValidationError -from rest_framework.settings import api_settings -from rest_framework.utils import html, representation, humanize_datetime -import collections -import copy -import datetime -import decimal -import inspect -import re -import uuid class empty: diff --git a/rest_framework/filters.py b/rest_framework/filters.py index 9a84efa23..7976d06da 100644 --- a/rest_framework/filters.py +++ b/rest_framework/filters.py @@ -4,13 +4,16 @@ returned by list views. """ from __future__ import unicode_literals -from django.core.exceptions import ImproperlyConfigured -from django.db import models -from django.utils import six -from rest_framework.compat import django_filters, guardian, get_model_name -from rest_framework.settings import api_settings -from functools import reduce import operator +from functools import reduce + +from django.utils import six +from django.db import models +from django.core.exceptions import ImproperlyConfigured + +from rest_framework.settings import api_settings +from rest_framework.compat import django_filters, guardian, get_model_name + FilterSet = django_filters and django_filters.FilterSet or None diff --git a/rest_framework/generics.py b/rest_framework/generics.py index 61dcb84a4..20eb120cc 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -2,9 +2,11 @@ Generic views that provide commonly needed behaviour. """ from __future__ import unicode_literals -from django.db.models.query import QuerySet + from django.http import Http404 +from django.db.models.query import QuerySet from django.shortcuts import get_object_or_404 as _get_object_or_404 + from rest_framework import views, mixins from rest_framework.settings import api_settings diff --git a/rest_framework/metadata.py b/rest_framework/metadata.py index 45f5b766d..df2c9d553 100644 --- a/rest_framework/metadata.py +++ b/rest_framework/metadata.py @@ -8,9 +8,10 @@ to return this information in a more standardized way. """ from __future__ import unicode_literals -from django.core.exceptions import PermissionDenied from django.http import Http404 from django.utils.encoding import force_text +from django.core.exceptions import PermissionDenied + from rest_framework import exceptions, serializers from rest_framework.compat import OrderedDict from rest_framework.request import clone_request diff --git a/rest_framework/mixins.py b/rest_framework/mixins.py index c34cfcee1..1104aa29c 100644 --- a/rest_framework/mixins.py +++ b/rest_framework/mixins.py @@ -5,6 +5,7 @@ We don't bind behaviour to http method handlers yet, which allows mixin classes to be composed in interesting ways. """ from __future__ import unicode_literals + from rest_framework import status from rest_framework.response import Response from rest_framework.settings import api_settings diff --git a/rest_framework/negotiation.py b/rest_framework/negotiation.py index 663ec4c8a..0ec1936c7 100644 --- a/rest_framework/negotiation.py +++ b/rest_framework/negotiation.py @@ -3,11 +3,14 @@ Content negotiation deals with selecting an appropriate renderer given the incoming request. Typically this will be based on the request's Accept header. """ from __future__ import unicode_literals + from django.http import Http404 + from rest_framework import HTTP_HEADER_ENCODING, exceptions from rest_framework.settings import api_settings -from rest_framework.utils.mediatypes import order_by_precedence, media_type_matches -from rest_framework.utils.mediatypes import _MediaType +from rest_framework.utils.mediatypes import ( + _MediaType, order_by_precedence, media_type_matches +) class BaseContentNegotiation(object): diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index 785768308..8e45e9e6b 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -4,21 +4,24 @@ Pagination serializers determine the structure of the output that should be used for paginated responses. """ from __future__ import unicode_literals + +import warnings from base64 import b64encode, b64decode from collections import namedtuple -from django.core.paginator import InvalidPage, Paginator as DjangoPaginator -from django.template import Context, loader + from django.utils import six -from django.utils.six.moves.urllib import parse as urlparse +from django.template import Context, loader from django.utils.translation import ugettext_lazy as _ +from django.utils.six.moves.urllib import parse as urlparse +from django.core.paginator import InvalidPage, Paginator as DjangoPaginator + +from rest_framework.response import Response from rest_framework.compat import OrderedDict from rest_framework.exceptions import NotFound -from rest_framework.response import Response from rest_framework.settings import api_settings from rest_framework.utils.urls import ( replace_query_param, remove_query_param ) -import warnings def _positive_int(integer_string, strict=False, cutoff=None): diff --git a/rest_framework/parsers.py b/rest_framework/parsers.py index 437d13392..3cc820857 100644 --- a/rest_framework/parsers.py +++ b/rest_framework/parsers.py @@ -6,18 +6,22 @@ on the request, such as form content or json encoded data. """ from __future__ import unicode_literals -from django.conf import settings -from django.core.files.uploadhandler import StopFutureHandlers -from django.http import QueryDict -from django.http.multipartparser import MultiPartParser as DjangoMultiPartParser -from django.http.multipartparser import MultiPartParserError, parse_header, ChunkIter -from django.utils import six -from django.utils.six.moves.urllib import parse as urlparse -from django.utils.encoding import force_text -from rest_framework.exceptions import ParseError -from rest_framework import renderers import json +from django.utils import six +from django.conf import settings +from django.http import QueryDict +from django.utils.encoding import force_text +from django.utils.six.moves.urllib import parse as urlparse +from django.core.files.uploadhandler import StopFutureHandlers +from django.http.multipartparser import ( + MultiPartParserError, parse_header, ChunkIter, + MultiPartParser as DjangoMultiPartParser +) + +from rest_framework import renderers +from rest_framework.exceptions import ParseError + class DataAndFiles(object): def __init__(self, data, files): diff --git a/rest_framework/permissions.py b/rest_framework/permissions.py index 6ded6bbb4..84df78148 100644 --- a/rest_framework/permissions.py +++ b/rest_framework/permissions.py @@ -2,9 +2,12 @@ Provides a set of pluggable permission policies. """ from __future__ import unicode_literals + from django.http import Http404 + from rest_framework.compat import get_model_name + SAFE_METHODS = ('GET', 'HEAD', 'OPTIONS') diff --git a/rest_framework/relations.py b/rest_framework/relations.py index e428c7d0a..36e9cf71f 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -1,16 +1,20 @@ # coding: utf-8 from __future__ import unicode_literals -from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured -from django.core.urlresolvers import get_script_prefix, resolve, NoReverseMatch, Resolver404 -from django.db.models.query import QuerySet + from django.utils import six +from django.db.models.query import QuerySet from django.utils.encoding import smart_text -from django.utils.six.moves.urllib import parse as urlparse from django.utils.translation import ugettext_lazy as _ +from django.utils.six.moves.urllib import parse as urlparse +from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured +from django.core.urlresolvers import ( + get_script_prefix, resolve, NoReverseMatch, Resolver404 +) + +from rest_framework.utils import html +from rest_framework.reverse import reverse from rest_framework.compat import OrderedDict from rest_framework.fields import get_attribute, empty, Field -from rest_framework.reverse import reverse -from rest_framework.utils import html class PKOnlyObject(object): diff --git a/rest_framework/renderers.py b/rest_framework/renderers.py index 6c7cdf537..5fca5c12f 100644 --- a/rest_framework/renderers.py +++ b/rest_framework/renderers.py @@ -9,22 +9,26 @@ REST framework also provides an HTML renderer the renders the browsable API. from __future__ import unicode_literals import json + import django from django import forms -from django.core.exceptions import ImproperlyConfigured -from django.core.paginator import Page -from django.http.multipartparser import parse_header -from django.template import Context, RequestContext, loader, Template -from django.test.client import encode_multipart from django.utils import six -from rest_framework import exceptions, serializers, status, VERSION -from rest_framework.compat import SHORT_SEPARATORS, LONG_SEPARATORS, INDENT_SEPARATORS +from django.core.paginator import Page +from django.test.client import encode_multipart +from django.http.multipartparser import parse_header +from django.core.exceptions import ImproperlyConfigured +from django.template import Context, RequestContext, loader, Template + +from rest_framework.utils import encoders from rest_framework.exceptions import ParseError from rest_framework.settings import api_settings -from rest_framework.request import is_form_media_type, override_method -from rest_framework.utils import encoders from rest_framework.utils.breadcrumbs import get_breadcrumbs from rest_framework.utils.field_mapping import ClassLookupDict +from rest_framework import exceptions, serializers, status, VERSION +from rest_framework.request import is_form_media_type, override_method +from rest_framework.compat import ( + SHORT_SEPARATORS, LONG_SEPARATORS, INDENT_SEPARATORS +) def zero_as_none(value): diff --git a/rest_framework/request.py b/rest_framework/request.py index 174a9de56..614b73abf 100644 --- a/rest_framework/request.py +++ b/rest_framework/request.py @@ -9,16 +9,18 @@ The wrapped request then offers a richer API, in particular : - form overloading of HTTP method, content type and content """ from __future__ import unicode_literals + +import sys +import warnings + +from django.utils import six from django.conf import settings from django.http import QueryDict from django.http.multipartparser import parse_header -from django.utils import six from django.utils.datastructures import MultiValueDict -from rest_framework import HTTP_HEADER_ENCODING -from rest_framework import exceptions + +from rest_framework import exceptions, HTTP_HEADER_ENCODING from rest_framework.settings import api_settings -import sys -import warnings def is_form_media_type(media_type): diff --git a/rest_framework/response.py b/rest_framework/response.py index 9319e708e..e7f4cd3d1 100644 --- a/rest_framework/response.py +++ b/rest_framework/response.py @@ -5,9 +5,10 @@ it is initialized with unrendered data, instead of a pre-rendered string. The appropriate renderer is called during Django's template response rendering. """ from __future__ import unicode_literals + +from django.utils import six from django.utils.six.moves.http_client import responses from django.template.response import SimpleTemplateResponse -from django.utils import six class Response(SimpleTemplateResponse): diff --git a/rest_framework/reverse.py b/rest_framework/reverse.py index e6d3f5634..c13b4819a 100644 --- a/rest_framework/reverse.py +++ b/rest_framework/reverse.py @@ -2,10 +2,10 @@ Provide urlresolver functions that return fully qualified URLs or view names """ from __future__ import unicode_literals -from django.core.urlresolvers import reverse as django_reverse -from django.core.urlresolvers import NoReverseMatch + from django.utils import six from django.utils.functional import lazy +from django.core.urlresolvers import NoReverseMatch, reverse as django_reverse def reverse(viewname, args=None, kwargs=None, request=None, format=None, **extra): diff --git a/rest_framework/routers.py b/rest_framework/routers.py index 2792795a4..f92cca004 100644 --- a/rest_framework/routers.py +++ b/rest_framework/routers.py @@ -17,13 +17,15 @@ from __future__ import unicode_literals import itertools from collections import namedtuple + from django.conf.urls import url -from django.core.exceptions import ImproperlyConfigured from django.core.urlresolvers import NoReverseMatch +from django.core.exceptions import ImproperlyConfigured + from rest_framework import views -from rest_framework.compat import get_resolver_match, OrderedDict -from rest_framework.response import Response from rest_framework.reverse import reverse +from rest_framework.response import Response +from rest_framework.compat import get_resolver_match, OrderedDict from rest_framework.urlpatterns import format_suffix_patterns diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index 9e4fff449..d4a26750d 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -11,29 +11,31 @@ python primitives. response content is handled by parsers and renderers. """ from __future__ import unicode_literals + +import warnings + from django.db import models -from django.db.models.fields import FieldDoesNotExist, Field as DjangoModelField from django.utils.functional import cached_property from django.utils.translation import ugettext_lazy as _ -from rest_framework.compat import ( - postgres_fields, - unicode_to_repr, - DurationField as ModelDurationField, +from django.db.models.fields import ( + FieldDoesNotExist, Field as DjangoModelField ) + from rest_framework.utils import model_meta -from rest_framework.utils.field_mapping import ( - get_url_kwargs, get_field_kwargs, - get_relation_kwargs, get_nested_relation_kwargs, - ClassLookupDict +from rest_framework.compat import ( + postgres_fields, unicode_to_repr, DurationField as ModelDurationField, ) from rest_framework.utils.serializer_helpers import ( ReturnDict, ReturnList, BoundField, NestedBoundField, BindingDict ) +from rest_framework.utils.field_mapping import ( + get_url_kwargs, get_field_kwargs, get_relation_kwargs, + get_nested_relation_kwargs, ClassLookupDict +) from rest_framework.validators import ( UniqueForDateValidator, UniqueForMonthValidator, UniqueForYearValidator, UniqueTogetherValidator ) -import warnings # Note: We do the following so that users of the framework can use this style: diff --git a/rest_framework/settings.py b/rest_framework/settings.py index a3e9f5902..515e4aecb 100644 --- a/rest_framework/settings.py +++ b/rest_framework/settings.py @@ -18,12 +18,15 @@ REST framework settings, checking for user settings first, then falling back to the defaults. """ from __future__ import unicode_literals -from django.test.signals import setting_changed -from django.conf import settings + from django.utils import six +from django.conf import settings +from django.test.signals import setting_changed + from rest_framework import ISO_8601 from rest_framework.compat import importlib + USER_SETTINGS = getattr(settings, 'REST_FRAMEWORK', None) DEFAULTS = { diff --git a/rest_framework/templatetags/rest_framework.py b/rest_framework/templatetags/rest_framework.py index bf0dc7b8f..7e9110d8f 100644 --- a/rest_framework/templatetags/rest_framework.py +++ b/rest_framework/templatetags/rest_framework.py @@ -1,14 +1,18 @@ from __future__ import unicode_literals, absolute_import + +import re + from django import template -from django.core.urlresolvers import reverse, NoReverseMatch from django.utils import six -from django.utils.encoding import iri_to_uri, force_text from django.utils.html import escape -from django.utils.safestring import SafeData, mark_safe from django.utils.html import smart_urlquote +from django.utils.safestring import SafeData, mark_safe +from django.utils.encoding import iri_to_uri, force_text +from django.core.urlresolvers import reverse, NoReverseMatch + from rest_framework.renderers import HTMLFormRenderer from rest_framework.utils.urls import replace_query_param -import re + register = template.Library() diff --git a/rest_framework/test.py b/rest_framework/test.py index a83d082ab..60c697814 100644 --- a/rest_framework/test.py +++ b/rest_framework/test.py @@ -3,16 +3,18 @@ # Note that we import as `DjangoRequestFactory` and `DjangoClient` in order # to make it harder for the user to import the wrong thing without realizing. from __future__ import unicode_literals + import django -from django.conf import settings -from django.test.client import Client as DjangoClient -from django.test.client import ClientHandler -from django.test import testcases from django.utils import six +from django.conf import settings +from django.test import testcases from django.utils.http import urlencode +from django.test.client import ClientHandler, Client as DjangoClient + from rest_framework.settings import api_settings -from rest_framework.compat import RequestFactory as DjangoRequestFactory -from rest_framework.compat import force_bytes_or_smart_bytes +from rest_framework.compat import ( + force_bytes_or_smart_bytes, RequestFactory as DjangoRequestFactory +) def force_authenticate(request, user=None, token=None): diff --git a/rest_framework/throttling.py b/rest_framework/throttling.py index 261fc2463..abb353469 100644 --- a/rest_framework/throttling.py +++ b/rest_framework/throttling.py @@ -2,10 +2,13 @@ Provides various throttling policies. """ from __future__ import unicode_literals + +import time + from django.core.cache import cache as default_cache from django.core.exceptions import ImproperlyConfigured + from rest_framework.settings import api_settings -import time class BaseThrottle(object): diff --git a/rest_framework/urlpatterns.py b/rest_framework/urlpatterns.py index 2e4369f71..7b1516265 100644 --- a/rest_framework/urlpatterns.py +++ b/rest_framework/urlpatterns.py @@ -1,6 +1,8 @@ from __future__ import unicode_literals + from django.conf.urls import url, include from django.core.urlresolvers import RegexURLResolver + from rest_framework.settings import api_settings diff --git a/rest_framework/urls.py b/rest_framework/urls.py index ee37eb203..111524233 100644 --- a/rest_framework/urls.py +++ b/rest_framework/urls.py @@ -13,6 +13,7 @@ The urls must be namespaced as 'rest_framework', and you should make sure your authentication settings include `SessionAuthentication`. """ from __future__ import unicode_literals + from django.conf.urls import url from django.contrib.auth import views diff --git a/rest_framework/utils/breadcrumbs.py b/rest_framework/utils/breadcrumbs.py index e6690d170..af786810f 100644 --- a/rest_framework/utils/breadcrumbs.py +++ b/rest_framework/utils/breadcrumbs.py @@ -1,4 +1,5 @@ from __future__ import unicode_literals + from django.core.urlresolvers import resolve, get_script_prefix diff --git a/rest_framework/utils/encoders.py b/rest_framework/utils/encoders.py index 2160d18b6..62bbfaff9 100644 --- a/rest_framework/utils/encoders.py +++ b/rest_framework/utils/encoders.py @@ -2,15 +2,18 @@ Helper classes for parsers. """ from __future__ import unicode_literals -from django.db.models.query import QuerySet -from django.utils import six, timezone -from django.utils.encoding import force_text -from django.utils.functional import Promise -from rest_framework.compat import total_seconds -import datetime -import decimal -import json + import uuid +import json +import decimal +import datetime + +from django.utils import six, timezone +from django.db.models.query import QuerySet +from django.utils.functional import Promise +from django.utils.encoding import force_text + +from rest_framework.compat import total_seconds class JSONEncoder(json.JSONEncoder): diff --git a/rest_framework/utils/field_mapping.py b/rest_framework/utils/field_mapping.py index f20628be0..8fcb8c332 100644 --- a/rest_framework/utils/field_mapping.py +++ b/rest_framework/utils/field_mapping.py @@ -2,12 +2,14 @@ Helper functions for mapping model fields to a dictionary of default keyword arguments that should be used for their equivelent serializer fields. """ -from django.core import validators +import inspect + from django.db import models +from django.core import validators from django.utils.text import capfirst + from rest_framework.compat import clean_manytomany_helptext from rest_framework.validators import UniqueValidator -import inspect NUMERIC_FIELD_TYPES = ( diff --git a/rest_framework/utils/formatting.py b/rest_framework/utils/formatting.py index 8b6f005e1..5ad974e7c 100644 --- a/rest_framework/utils/formatting.py +++ b/rest_framework/utils/formatting.py @@ -2,10 +2,13 @@ Utility functions to return a formatted name and description for a given view. """ from __future__ import unicode_literals + +import re + from django.utils.html import escape from django.utils.safestring import mark_safe + from rest_framework.compat import apply_markdown, force_text -import re def remove_trailing_string(content, trailing): diff --git a/rest_framework/utils/html.py b/rest_framework/utils/html.py index d773952dc..341f7bcd1 100644 --- a/rest_framework/utils/html.py +++ b/rest_framework/utils/html.py @@ -2,6 +2,7 @@ Helpers for dealing with HTML input. """ import re + from django.utils.datastructures import MultiValueDict diff --git a/rest_framework/utils/mediatypes.py b/rest_framework/utils/mediatypes.py index de2931c28..def4b9641 100644 --- a/rest_framework/utils/mediatypes.py +++ b/rest_framework/utils/mediatypes.py @@ -4,8 +4,10 @@ Handling of media types, as found in HTTP Content-Type and Accept headers. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.7 """ from __future__ import unicode_literals + from django.http.multipartparser import parse_header from django.utils.encoding import python_2_unicode_compatible + from rest_framework import HTTP_HEADER_ENCODING diff --git a/rest_framework/utils/model_meta.py b/rest_framework/utils/model_meta.py index 19f751b04..9fe2e7069 100644 --- a/rest_framework/utils/model_meta.py +++ b/rest_framework/utils/model_meta.py @@ -5,12 +5,14 @@ relationships and their associated metadata. Usage: `get_field_info(model)` returns a `FieldInfo` instance. """ -from collections import namedtuple -from django.core.exceptions import ImproperlyConfigured -from django.db import models -from django.utils import six -from rest_framework.compat import OrderedDict import inspect +from collections import namedtuple + +from django.utils import six +from django.db import models +from django.core.exceptions import ImproperlyConfigured + +from rest_framework.compat import OrderedDict FieldInfo = namedtuple('FieldResult', [ diff --git a/rest_framework/utils/representation.py b/rest_framework/utils/representation.py index 1bfc64c1f..1bd579f62 100644 --- a/rest_framework/utils/representation.py +++ b/rest_framework/utils/representation.py @@ -3,12 +3,15 @@ Helper functions for creating user-friendly representations of serializer classes and serializer fields. """ from __future__ import unicode_literals -from django.db import models -from django.utils.encoding import force_text -from django.utils.functional import Promise -from rest_framework.compat import unicode_repr + import re +from django.db import models +from django.utils.functional import Promise +from django.utils.encoding import force_text + +from rest_framework.compat import unicode_repr + def manager_repr(value): model = value.model diff --git a/rest_framework/utils/serializer_helpers.py b/rest_framework/utils/serializer_helpers.py index 87bb3ac08..b751a3588 100644 --- a/rest_framework/utils/serializer_helpers.py +++ b/rest_framework/utils/serializer_helpers.py @@ -1,5 +1,7 @@ from __future__ import unicode_literals + import collections + from rest_framework.compat import OrderedDict, unicode_to_repr diff --git a/rest_framework/validators.py b/rest_framework/validators.py index 6ae80b897..a1771a92e 100644 --- a/rest_framework/validators.py +++ b/rest_framework/validators.py @@ -7,7 +7,9 @@ object creation, and makes it possible to switch between using the implicit `ModelSerializer` class and an equivalent explicit `Serializer` class. """ from __future__ import unicode_literals + from django.utils.translation import ugettext_lazy as _ + from rest_framework.compat import unicode_to_repr from rest_framework.exceptions import ValidationError from rest_framework.utils.representation import smart_repr diff --git a/rest_framework/versioning.py b/rest_framework/versioning.py index 27c2d8791..4ee6e2546 100644 --- a/rest_framework/versioning.py +++ b/rest_framework/versioning.py @@ -1,13 +1,16 @@ # coding: utf-8 from __future__ import unicode_literals + +import re + from django.utils.translation import ugettext_lazy as _ + from rest_framework import exceptions -from rest_framework.compat import unicode_http_header from rest_framework.reverse import _reverse from rest_framework.settings import api_settings -from rest_framework.templatetags.rest_framework import replace_query_param +from rest_framework.compat import unicode_http_header from rest_framework.utils.mediatypes import _MediaType -import re +from rest_framework.templatetags.rest_framework import replace_query_param class BaseVersioning(object): diff --git a/rest_framework/views.py b/rest_framework/views.py index 55fb8979b..2230c6dc6 100644 --- a/rest_framework/views.py +++ b/rest_framework/views.py @@ -2,21 +2,24 @@ Provides an APIView class that is the base of all views in REST framework. """ from __future__ import unicode_literals -from django.core.exceptions import PermissionDenied -from django.http import Http404 -from django.utils import six -from django.utils.encoding import smart_text -from django.utils.translation import ugettext_lazy as _ -from django.views.decorators.csrf import csrf_exempt -from rest_framework import status, exceptions -from rest_framework.compat import HttpResponseBase, View, set_rollback -from rest_framework.request import Request -from rest_framework.response import Response -from rest_framework.settings import api_settings -from rest_framework.utils import formatting + import inspect import warnings +from django.utils import six +from django.http import Http404 +from django.utils.encoding import smart_text +from django.core.exceptions import PermissionDenied +from django.views.decorators.csrf import csrf_exempt +from django.utils.translation import ugettext_lazy as _ + +from rest_framework import status, exceptions +from rest_framework.request import Request +from rest_framework.utils import formatting +from rest_framework.response import Response +from rest_framework.settings import api_settings +from rest_framework.compat import HttpResponseBase, View, set_rollback + def get_view_name(view_cls, suffix=None): """ diff --git a/rest_framework/viewsets.py b/rest_framework/viewsets.py index 88c763da4..e9fa41fbc 100644 --- a/rest_framework/viewsets.py +++ b/rest_framework/viewsets.py @@ -19,8 +19,10 @@ automatically. from __future__ import unicode_literals from functools import update_wrapper + from django.utils.decorators import classonlymethod from django.views.decorators.csrf import csrf_exempt + from rest_framework import views, generics, mixins diff --git a/setup.py b/setup.py index 4cdcfa86e..ca802774d 100755 --- a/setup.py +++ b/setup.py @@ -1,11 +1,9 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- - -from setuptools import setup -from setuptools.command.test import test as TestCommand import re import os import sys +from setuptools import setup def get_version(package): diff --git a/tests/models.py b/tests/models.py index 456b0a0bb..c265182b7 100644 --- a/tests/models.py +++ b/tests/models.py @@ -1,4 +1,5 @@ from __future__ import unicode_literals + from django.db import models from django.utils.translation import ugettext_lazy as _ From 7351a3f6ca7028868a79a6f2dae36ccb2fc7dd65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Padilla?= Date: Thu, 25 Jun 2015 16:55:51 -0400 Subject: [PATCH 2/3] Sort imports with isort --- rest_framework/authentication.py | 4 +-- .../authtoken/migrations/0001_initial.py | 2 +- rest_framework/authtoken/models.py | 5 ++- rest_framework/authtoken/views.py | 4 +-- rest_framework/compat.py | 8 ++--- rest_framework/exceptions.py | 3 +- rest_framework/fields.py | 33 +++++++++---------- rest_framework/filters.py | 7 ++-- rest_framework/generics.py | 4 +-- rest_framework/metadata.py | 2 +- rest_framework/negotiation.py | 2 +- rest_framework/pagination.py | 15 ++++----- rest_framework/parsers.py | 13 ++++---- rest_framework/permissions.py | 1 - rest_framework/relations.py | 20 +++++------ rest_framework/renderers.py | 22 ++++++------- rest_framework/request.py | 4 +-- rest_framework/response.py | 2 +- rest_framework/reverse.py | 3 +- rest_framework/routers.py | 7 ++-- rest_framework/serializers.py | 24 ++++++-------- rest_framework/settings.py | 3 +- rest_framework/templatetags/rest_framework.py | 10 +++--- rest_framework/test.py | 10 +++--- rest_framework/urlpatterns.py | 2 +- rest_framework/urls.py | 1 - rest_framework/utils/breadcrumbs.py | 2 +- rest_framework/utils/encoders.py | 10 +++--- rest_framework/utils/field_mapping.py | 3 +- rest_framework/utils/model_meta.py | 5 ++- rest_framework/utils/representation.py | 2 +- rest_framework/versioning.py | 4 +-- rest_framework/views.py | 14 ++++---- rest_framework/viewsets.py | 2 +- setup.py | 3 +- tests/browsable_api/auth_urls.py | 4 +-- tests/browsable_api/no_auth_urls.py | 1 + tests/browsable_api/test_browsable_api.py | 1 + tests/browsable_api/views.py | 5 ++- tests/description.py | 1 - tests/test_atomic_requests.py | 4 +-- tests/test_authentication.py | 25 +++++++------- tests/test_decorators.py | 16 ++++----- tests/test_description.py | 9 +++-- tests/test_fields.py | 12 ++++--- tests/test_filters.py | 9 +++-- tests/test_generics.py | 7 ++-- tests/test_htmlrenderer.py | 8 +++-- tests/test_metadata.py | 10 ++++-- tests/test_middleware.py | 2 +- tests/test_model_serializer.py | 11 +++++-- tests/test_multitable_inheritance.py | 2 ++ tests/test_negotiation.py | 11 ++++--- tests/test_pagination.py | 12 ++++--- tests/test_parsers.py | 4 ++- tests/test_permissions.py | 16 ++++++--- tests/test_relations.py | 9 +++-- tests/test_relations_generic.py | 6 +++- tests/test_relations_hyperlink.py | 6 ++-- tests/test_relations_pk.py | 6 ++-- tests/test_relations_slug.py | 5 ++- tests/test_renderers.py | 21 ++++++------ tests/test_request.py | 23 +++++++------ tests/test_response.py | 20 +++++------ tests/test_reverse.py | 2 ++ tests/test_routers.py | 13 +++++--- tests/test_serializer.py | 10 ++++-- tests/test_serializer_bulk_update.py | 2 ++ tests/test_serializer_lists.py | 3 +- tests/test_settings.py | 2 ++ tests/test_status.py | 5 ++- tests/test_templatetags.py | 9 +++-- tests/test_testing.py | 9 +++-- tests/test_throttling.py | 10 ++++-- tests/test_urlpatterns.py | 6 ++-- tests/test_utils.py | 11 ++++--- tests/test_validation.py | 7 ++-- tests/test_validators.py | 4 ++- tests/test_versioning.py | 11 ++++--- tests/test_views.py | 4 ++- tests/test_viewsets.py | 2 +- tests/test_write_only_fields.py | 1 + 82 files changed, 355 insertions(+), 278 deletions(-) diff --git a/rest_framework/authentication.py b/rest_framework/authentication.py index 74c6b342b..088eac4ca 100644 --- a/rest_framework/authentication.py +++ b/rest_framework/authentication.py @@ -9,9 +9,9 @@ from django.contrib.auth import authenticate from django.middleware.csrf import CsrfViewMiddleware from django.utils.translation import ugettext_lazy as _ -from rest_framework.compat import get_user_model +from rest_framework import HTTP_HEADER_ENCODING, exceptions from rest_framework.authtoken.models import Token -from rest_framework import exceptions, HTTP_HEADER_ENCODING +from rest_framework.compat import get_user_model def get_authorization_header(request): diff --git a/rest_framework/authtoken/migrations/0001_initial.py b/rest_framework/authtoken/migrations/0001_initial.py index 769f62029..0eb9fbcbe 100644 --- a/rest_framework/authtoken/migrations/0001_initial.py +++ b/rest_framework/authtoken/migrations/0001_initial.py @@ -1,8 +1,8 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals -from django.db import models, migrations from django.conf import settings +from django.db import migrations, models class Migration(migrations.Migration): diff --git a/rest_framework/authtoken/models.py b/rest_framework/authtoken/models.py index f3e01a4b7..b329ee65f 100644 --- a/rest_framework/authtoken/models.py +++ b/rest_framework/authtoken/models.py @@ -1,11 +1,10 @@ -import os import binascii +import os -from django.db import models from django.conf import settings +from django.db import models from django.utils.encoding import python_2_unicode_compatible - # Prior to Django 1.5, the AUTH_USER_MODEL setting does not exist. # Note that we don't perform this code in the compat module due to # bug report #1297 diff --git a/rest_framework/authtoken/views.py b/rest_framework/authtoken/views.py index 26df9e11c..743684057 100644 --- a/rest_framework/authtoken/views.py +++ b/rest_framework/authtoken/views.py @@ -1,8 +1,8 @@ from rest_framework import parsers, renderers -from rest_framework.views import APIView -from rest_framework.response import Response from rest_framework.authtoken.models import Token from rest_framework.authtoken.serializers import AuthTokenSerializer +from rest_framework.response import Response +from rest_framework.views import APIView class ObtainAuthToken(APIView): diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 28e72cfe8..d11c68093 100644 --- a/rest_framework/compat.py +++ b/rest_framework/compat.py @@ -9,11 +9,12 @@ from __future__ import unicode_literals import inspect import django -from django.utils import six from django.conf import settings -from django.db import connection, transaction -from django.utils.encoding import force_text from django.core.exceptions import ImproperlyConfigured +from django.db import connection, transaction +from django.test.client import FakePayload +from django.utils import six +from django.utils.encoding import force_text from django.utils.six.moves.urllib.parse import urlparse as _urlparse try: @@ -202,7 +203,6 @@ if 'patch' not in View.http_method_names: View.http_method_names = View.http_method_names + ['patch'] -from django.test.client import FakePayload try: # In 1.5 the test client uses force_bytes diff --git a/rest_framework/exceptions.py b/rest_framework/exceptions.py index 9a467f987..f587d10fd 100644 --- a/rest_framework/exceptions.py +++ b/rest_framework/exceptions.py @@ -10,7 +10,8 @@ import math from django.utils import six from django.utils.encoding import force_text -from django.utils.translation import ugettext_lazy as _, ungettext +from django.utils.translation import ugettext_lazy as _ +from django.utils.translation import ungettext from rest_framework import status diff --git a/rest_framework/fields.py b/rest_framework/fields.py index 3794329b7..7960295bd 100644 --- a/rest_framework/fields.py +++ b/rest_framework/fields.py @@ -1,34 +1,33 @@ from __future__ import unicode_literals -import re +import collections import copy -import uuid +import datetime import decimal import inspect -import datetime -import collections +import re +import uuid from django.conf import settings +from django.core.exceptions import ValidationError as DjangoValidationError +from django.core.exceptions import ObjectDoesNotExist +from django.core.validators import RegexValidator, ip_address_validators +from django.forms import ImageField as DjangoImageField from django.utils import six, timezone +from django.utils.dateparse import parse_date, parse_datetime, parse_time +from django.utils.encoding import is_protected_type, smart_text from django.utils.ipv6 import clean_ipv6_address from django.utils.translation import ugettext_lazy as _ -from django.forms import ImageField as DjangoImageField -from django.utils.encoding import is_protected_type, smart_text -from django.core.validators import RegexValidator, ip_address_validators -from django.utils.dateparse import parse_date, parse_datetime, parse_time -from django.core.exceptions import ( - ObjectDoesNotExist, ValidationError as DjangoValidationError -) from rest_framework import ISO_8601 -from rest_framework.settings import api_settings -from rest_framework.exceptions import ValidationError -from rest_framework.utils import html, representation, humanize_datetime from rest_framework.compat import ( - EmailValidator, MinValueValidator, MaxValueValidator, - MinLengthValidator, MaxLengthValidator, URLValidator, OrderedDict, - unicode_repr, unicode_to_repr, parse_duration, duration_string, + EmailValidator, MaxLengthValidator, MaxValueValidator, MinLengthValidator, + MinValueValidator, OrderedDict, URLValidator, duration_string, + parse_duration, unicode_repr, unicode_to_repr ) +from rest_framework.exceptions import ValidationError +from rest_framework.settings import api_settings +from rest_framework.utils import html, humanize_datetime, representation class empty: diff --git a/rest_framework/filters.py b/rest_framework/filters.py index 7976d06da..294d1b22b 100644 --- a/rest_framework/filters.py +++ b/rest_framework/filters.py @@ -7,13 +7,12 @@ from __future__ import unicode_literals import operator from functools import reduce -from django.utils import six -from django.db import models from django.core.exceptions import ImproperlyConfigured +from django.db import models +from django.utils import six +from rest_framework.compat import django_filters, get_model_name, guardian from rest_framework.settings import api_settings -from rest_framework.compat import django_filters, guardian, get_model_name - FilterSet = django_filters and django_filters.FilterSet or None diff --git a/rest_framework/generics.py b/rest_framework/generics.py index 20eb120cc..88438e8c4 100644 --- a/rest_framework/generics.py +++ b/rest_framework/generics.py @@ -3,11 +3,11 @@ Generic views that provide commonly needed behaviour. """ from __future__ import unicode_literals -from django.http import Http404 from django.db.models.query import QuerySet +from django.http import Http404 from django.shortcuts import get_object_or_404 as _get_object_or_404 -from rest_framework import views, mixins +from rest_framework import mixins, views from rest_framework.settings import api_settings diff --git a/rest_framework/metadata.py b/rest_framework/metadata.py index df2c9d553..3c7b3d2ab 100644 --- a/rest_framework/metadata.py +++ b/rest_framework/metadata.py @@ -8,9 +8,9 @@ to return this information in a more standardized way. """ from __future__ import unicode_literals +from django.core.exceptions import PermissionDenied from django.http import Http404 from django.utils.encoding import force_text -from django.core.exceptions import PermissionDenied from rest_framework import exceptions, serializers from rest_framework.compat import OrderedDict diff --git a/rest_framework/negotiation.py b/rest_framework/negotiation.py index 0ec1936c7..b9263c447 100644 --- a/rest_framework/negotiation.py +++ b/rest_framework/negotiation.py @@ -9,7 +9,7 @@ from django.http import Http404 from rest_framework import HTTP_HEADER_ENCODING, exceptions from rest_framework.settings import api_settings from rest_framework.utils.mediatypes import ( - _MediaType, order_by_precedence, media_type_matches + _MediaType, media_type_matches, order_by_precedence ) diff --git a/rest_framework/pagination.py b/rest_framework/pagination.py index 8e45e9e6b..bea10d036 100644 --- a/rest_framework/pagination.py +++ b/rest_framework/pagination.py @@ -6,22 +6,21 @@ be used for paginated responses. from __future__ import unicode_literals import warnings -from base64 import b64encode, b64decode +from base64 import b64decode, b64encode from collections import namedtuple -from django.utils import six +from django.core.paginator import Paginator as DjangoPaginator +from django.core.paginator import InvalidPage from django.template import Context, loader -from django.utils.translation import ugettext_lazy as _ +from django.utils import six from django.utils.six.moves.urllib import parse as urlparse -from django.core.paginator import InvalidPage, Paginator as DjangoPaginator +from django.utils.translation import ugettext_lazy as _ -from rest_framework.response import Response from rest_framework.compat import OrderedDict from rest_framework.exceptions import NotFound +from rest_framework.response import Response from rest_framework.settings import api_settings -from rest_framework.utils.urls import ( - replace_query_param, remove_query_param -) +from rest_framework.utils.urls import remove_query_param, replace_query_param def _positive_int(integer_string, strict=False, cutoff=None): diff --git a/rest_framework/parsers.py b/rest_framework/parsers.py index 3cc820857..2dc808f1a 100644 --- a/rest_framework/parsers.py +++ b/rest_framework/parsers.py @@ -8,16 +8,17 @@ from __future__ import unicode_literals import json -from django.utils import six from django.conf import settings +from django.core.files.uploadhandler import StopFutureHandlers from django.http import QueryDict +from django.http.multipartparser import \ + MultiPartParser as DjangoMultiPartParser +from django.http.multipartparser import ( + ChunkIter, MultiPartParserError, parse_header +) +from django.utils import six from django.utils.encoding import force_text from django.utils.six.moves.urllib import parse as urlparse -from django.core.files.uploadhandler import StopFutureHandlers -from django.http.multipartparser import ( - MultiPartParserError, parse_header, ChunkIter, - MultiPartParser as DjangoMultiPartParser -) from rest_framework import renderers from rest_framework.exceptions import ParseError diff --git a/rest_framework/permissions.py b/rest_framework/permissions.py index 84df78148..628538903 100644 --- a/rest_framework/permissions.py +++ b/rest_framework/permissions.py @@ -7,7 +7,6 @@ from django.http import Http404 from rest_framework.compat import get_model_name - SAFE_METHODS = ('GET', 'HEAD', 'OPTIONS') diff --git a/rest_framework/relations.py b/rest_framework/relations.py index 36e9cf71f..ca39dbade 100644 --- a/rest_framework/relations.py +++ b/rest_framework/relations.py @@ -1,20 +1,20 @@ # coding: utf-8 from __future__ import unicode_literals -from django.utils import six -from django.db.models.query import QuerySet -from django.utils.encoding import smart_text -from django.utils.translation import ugettext_lazy as _ -from django.utils.six.moves.urllib import parse as urlparse -from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured +from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist from django.core.urlresolvers import ( - get_script_prefix, resolve, NoReverseMatch, Resolver404 + NoReverseMatch, Resolver404, get_script_prefix, resolve ) +from django.db.models.query import QuerySet +from django.utils import six +from django.utils.encoding import smart_text +from django.utils.six.moves.urllib import parse as urlparse +from django.utils.translation import ugettext_lazy as _ -from rest_framework.utils import html -from rest_framework.reverse import reverse from rest_framework.compat import OrderedDict -from rest_framework.fields import get_attribute, empty, Field +from rest_framework.fields import Field, empty, get_attribute +from rest_framework.reverse import reverse +from rest_framework.utils import html class PKOnlyObject(object): diff --git a/rest_framework/renderers.py b/rest_framework/renderers.py index 5fca5c12f..6e1370b72 100644 --- a/rest_framework/renderers.py +++ b/rest_framework/renderers.py @@ -12,23 +12,23 @@ import json import django from django import forms -from django.utils import six -from django.core.paginator import Page -from django.test.client import encode_multipart -from django.http.multipartparser import parse_header from django.core.exceptions import ImproperlyConfigured -from django.template import Context, RequestContext, loader, Template +from django.core.paginator import Page +from django.http.multipartparser import parse_header +from django.template import Context, RequestContext, Template, loader +from django.test.client import encode_multipart +from django.utils import six -from rest_framework.utils import encoders +from rest_framework import VERSION, exceptions, serializers, status +from rest_framework.compat import ( + INDENT_SEPARATORS, LONG_SEPARATORS, SHORT_SEPARATORS +) from rest_framework.exceptions import ParseError +from rest_framework.request import is_form_media_type, override_method from rest_framework.settings import api_settings +from rest_framework.utils import encoders from rest_framework.utils.breadcrumbs import get_breadcrumbs from rest_framework.utils.field_mapping import ClassLookupDict -from rest_framework import exceptions, serializers, status, VERSION -from rest_framework.request import is_form_media_type, override_method -from rest_framework.compat import ( - SHORT_SEPARATORS, LONG_SEPARATORS, INDENT_SEPARATORS -) def zero_as_none(value): diff --git a/rest_framework/request.py b/rest_framework/request.py index 614b73abf..568ba1b50 100644 --- a/rest_framework/request.py +++ b/rest_framework/request.py @@ -13,13 +13,13 @@ from __future__ import unicode_literals import sys import warnings -from django.utils import six from django.conf import settings from django.http import QueryDict from django.http.multipartparser import parse_header +from django.utils import six from django.utils.datastructures import MultiValueDict -from rest_framework import exceptions, HTTP_HEADER_ENCODING +from rest_framework import HTTP_HEADER_ENCODING, exceptions from rest_framework.settings import api_settings diff --git a/rest_framework/response.py b/rest_framework/response.py index e7f4cd3d1..47d947655 100644 --- a/rest_framework/response.py +++ b/rest_framework/response.py @@ -6,9 +6,9 @@ The appropriate renderer is called during Django's template response rendering. """ from __future__ import unicode_literals +from django.template.response import SimpleTemplateResponse from django.utils import six from django.utils.six.moves.http_client import responses -from django.template.response import SimpleTemplateResponse class Response(SimpleTemplateResponse): diff --git a/rest_framework/reverse.py b/rest_framework/reverse.py index c13b4819a..dae4b8212 100644 --- a/rest_framework/reverse.py +++ b/rest_framework/reverse.py @@ -3,9 +3,10 @@ Provide urlresolver functions that return fully qualified URLs or view names """ from __future__ import unicode_literals +from django.core.urlresolvers import reverse as django_reverse +from django.core.urlresolvers import NoReverseMatch from django.utils import six from django.utils.functional import lazy -from django.core.urlresolvers import NoReverseMatch, reverse as django_reverse def reverse(viewname, args=None, kwargs=None, request=None, format=None, **extra): diff --git a/rest_framework/routers.py b/rest_framework/routers.py index f92cca004..380f3bb4f 100644 --- a/rest_framework/routers.py +++ b/rest_framework/routers.py @@ -19,16 +19,15 @@ import itertools from collections import namedtuple from django.conf.urls import url -from django.core.urlresolvers import NoReverseMatch from django.core.exceptions import ImproperlyConfigured +from django.core.urlresolvers import NoReverseMatch from rest_framework import views -from rest_framework.reverse import reverse +from rest_framework.compat import OrderedDict, get_resolver_match from rest_framework.response import Response -from rest_framework.compat import get_resolver_match, OrderedDict +from rest_framework.reverse import reverse from rest_framework.urlpatterns import format_suffix_patterns - Route = namedtuple('Route', ['url', 'mapping', 'name', 'initkwargs']) DynamicDetailRoute = namedtuple('DynamicDetailRoute', ['url', 'name', 'initkwargs']) DynamicListRoute = namedtuple('DynamicListRoute', ['url', 'name', 'initkwargs']) diff --git a/rest_framework/serializers.py b/rest_framework/serializers.py index d4a26750d..3a452bcc1 100644 --- a/rest_framework/serializers.py +++ b/rest_framework/serializers.py @@ -15,29 +15,26 @@ from __future__ import unicode_literals import warnings from django.db import models +from django.db.models.fields import Field as DjangoModelField +from django.db.models.fields import FieldDoesNotExist from django.utils.functional import cached_property from django.utils.translation import ugettext_lazy as _ -from django.db.models.fields import ( - FieldDoesNotExist, Field as DjangoModelField -) +from rest_framework.compat import DurationField as ModelDurationField +from rest_framework.compat import postgres_fields, unicode_to_repr from rest_framework.utils import model_meta -from rest_framework.compat import ( - postgres_fields, unicode_to_repr, DurationField as ModelDurationField, +from rest_framework.utils.field_mapping import ( + ClassLookupDict, get_field_kwargs, get_nested_relation_kwargs, + get_relation_kwargs, get_url_kwargs ) from rest_framework.utils.serializer_helpers import ( - ReturnDict, ReturnList, BoundField, NestedBoundField, BindingDict -) -from rest_framework.utils.field_mapping import ( - get_url_kwargs, get_field_kwargs, get_relation_kwargs, - get_nested_relation_kwargs, ClassLookupDict + BindingDict, BoundField, NestedBoundField, ReturnDict, ReturnList ) from rest_framework.validators import ( UniqueForDateValidator, UniqueForMonthValidator, UniqueForYearValidator, UniqueTogetherValidator ) - # Note: We do the following so that users of the framework can use this style: # # example_field = serializers.CharField(...) @@ -45,9 +42,8 @@ from rest_framework.validators import ( # This helps keep the separation between model fields, form fields, and # serializer fields more explicit. -from rest_framework.relations import * # NOQA -from rest_framework.fields import * # NOQA - +from rest_framework.fields import * # NOQA # isort:skip +from rest_framework.relations import * # NOQA # isort:skip # We assume that 'validators' are intended for the child serializer, # rather than the parent serializer. diff --git a/rest_framework/settings.py b/rest_framework/settings.py index 515e4aecb..4c46a4831 100644 --- a/rest_framework/settings.py +++ b/rest_framework/settings.py @@ -19,14 +19,13 @@ back to the defaults. """ from __future__ import unicode_literals -from django.utils import six from django.conf import settings from django.test.signals import setting_changed +from django.utils import six from rest_framework import ISO_8601 from rest_framework.compat import importlib - USER_SETTINGS = getattr(settings, 'REST_FRAMEWORK', None) DEFAULTS = { diff --git a/rest_framework/templatetags/rest_framework.py b/rest_framework/templatetags/rest_framework.py index 7e9110d8f..3018489fb 100644 --- a/rest_framework/templatetags/rest_framework.py +++ b/rest_framework/templatetags/rest_framework.py @@ -1,19 +1,17 @@ -from __future__ import unicode_literals, absolute_import +from __future__ import absolute_import, unicode_literals import re from django import template +from django.core.urlresolvers import NoReverseMatch, reverse from django.utils import six -from django.utils.html import escape -from django.utils.html import smart_urlquote +from django.utils.encoding import force_text, iri_to_uri +from django.utils.html import escape, smart_urlquote from django.utils.safestring import SafeData, mark_safe -from django.utils.encoding import iri_to_uri, force_text -from django.core.urlresolvers import reverse, NoReverseMatch from rest_framework.renderers import HTMLFormRenderer from rest_framework.utils.urls import replace_query_param - register = template.Library() # Regex for adding classes to html snippets diff --git a/rest_framework/test.py b/rest_framework/test.py index 60c697814..92ac09f58 100644 --- a/rest_framework/test.py +++ b/rest_framework/test.py @@ -5,16 +5,16 @@ from __future__ import unicode_literals import django -from django.utils import six from django.conf import settings from django.test import testcases +from django.test.client import Client as DjangoClient +from django.test.client import ClientHandler +from django.utils import six from django.utils.http import urlencode -from django.test.client import ClientHandler, Client as DjangoClient +from rest_framework.compat import RequestFactory as DjangoRequestFactory +from rest_framework.compat import force_bytes_or_smart_bytes from rest_framework.settings import api_settings -from rest_framework.compat import ( - force_bytes_or_smart_bytes, RequestFactory as DjangoRequestFactory -) def force_authenticate(request, user=None, token=None): diff --git a/rest_framework/urlpatterns.py b/rest_framework/urlpatterns.py index 7b1516265..5347b3bc1 100644 --- a/rest_framework/urlpatterns.py +++ b/rest_framework/urlpatterns.py @@ -1,6 +1,6 @@ from __future__ import unicode_literals -from django.conf.urls import url, include +from django.conf.urls import include, url from django.core.urlresolvers import RegexURLResolver from rest_framework.settings import api_settings diff --git a/rest_framework/urls.py b/rest_framework/urls.py index 111524233..d88ee8dbd 100644 --- a/rest_framework/urls.py +++ b/rest_framework/urls.py @@ -17,7 +17,6 @@ from __future__ import unicode_literals from django.conf.urls import url from django.contrib.auth import views - template_name = {'template_name': 'rest_framework/login.html'} urlpatterns = [ diff --git a/rest_framework/utils/breadcrumbs.py b/rest_framework/utils/breadcrumbs.py index af786810f..65daea08f 100644 --- a/rest_framework/utils/breadcrumbs.py +++ b/rest_framework/utils/breadcrumbs.py @@ -1,6 +1,6 @@ from __future__ import unicode_literals -from django.core.urlresolvers import resolve, get_script_prefix +from django.core.urlresolvers import get_script_prefix, resolve def get_breadcrumbs(url): diff --git a/rest_framework/utils/encoders.py b/rest_framework/utils/encoders.py index 62bbfaff9..949c99eda 100644 --- a/rest_framework/utils/encoders.py +++ b/rest_framework/utils/encoders.py @@ -3,15 +3,15 @@ Helper classes for parsers. """ from __future__ import unicode_literals -import uuid -import json -import decimal import datetime +import decimal +import json +import uuid -from django.utils import six, timezone from django.db.models.query import QuerySet -from django.utils.functional import Promise +from django.utils import six, timezone from django.utils.encoding import force_text +from django.utils.functional import Promise from rest_framework.compat import total_seconds diff --git a/rest_framework/utils/field_mapping.py b/rest_framework/utils/field_mapping.py index 8fcb8c332..38c433155 100644 --- a/rest_framework/utils/field_mapping.py +++ b/rest_framework/utils/field_mapping.py @@ -4,14 +4,13 @@ keyword arguments that should be used for their equivelent serializer fields. """ import inspect -from django.db import models from django.core import validators +from django.db import models from django.utils.text import capfirst from rest_framework.compat import clean_manytomany_helptext from rest_framework.validators import UniqueValidator - NUMERIC_FIELD_TYPES = ( models.IntegerField, models.FloatField, models.DecimalField ) diff --git a/rest_framework/utils/model_meta.py b/rest_framework/utils/model_meta.py index 9fe2e7069..3bcd9049c 100644 --- a/rest_framework/utils/model_meta.py +++ b/rest_framework/utils/model_meta.py @@ -8,13 +8,12 @@ Usage: `get_field_info(model)` returns a `FieldInfo` instance. import inspect from collections import namedtuple -from django.utils import six -from django.db import models from django.core.exceptions import ImproperlyConfigured +from django.db import models +from django.utils import six from rest_framework.compat import OrderedDict - FieldInfo = namedtuple('FieldResult', [ 'pk', # Model field instance 'fields', # Dict of field name -> model field instance diff --git a/rest_framework/utils/representation.py b/rest_framework/utils/representation.py index 1bd579f62..920751719 100644 --- a/rest_framework/utils/representation.py +++ b/rest_framework/utils/representation.py @@ -7,8 +7,8 @@ from __future__ import unicode_literals import re from django.db import models -from django.utils.functional import Promise from django.utils.encoding import force_text +from django.utils.functional import Promise from rest_framework.compat import unicode_repr diff --git a/rest_framework/versioning.py b/rest_framework/versioning.py index 4ee6e2546..7b1422c92 100644 --- a/rest_framework/versioning.py +++ b/rest_framework/versioning.py @@ -6,11 +6,11 @@ import re from django.utils.translation import ugettext_lazy as _ from rest_framework import exceptions +from rest_framework.compat import unicode_http_header from rest_framework.reverse import _reverse from rest_framework.settings import api_settings -from rest_framework.compat import unicode_http_header -from rest_framework.utils.mediatypes import _MediaType from rest_framework.templatetags.rest_framework import replace_query_param +from rest_framework.utils.mediatypes import _MediaType class BaseVersioning(object): diff --git a/rest_framework/views.py b/rest_framework/views.py index 2230c6dc6..a709c2f6b 100644 --- a/rest_framework/views.py +++ b/rest_framework/views.py @@ -6,19 +6,19 @@ from __future__ import unicode_literals import inspect import warnings -from django.utils import six -from django.http import Http404 -from django.utils.encoding import smart_text from django.core.exceptions import PermissionDenied -from django.views.decorators.csrf import csrf_exempt +from django.http import Http404 +from django.utils import six +from django.utils.encoding import smart_text from django.utils.translation import ugettext_lazy as _ +from django.views.decorators.csrf import csrf_exempt -from rest_framework import status, exceptions +from rest_framework import exceptions, status +from rest_framework.compat import HttpResponseBase, View, set_rollback from rest_framework.request import Request -from rest_framework.utils import formatting from rest_framework.response import Response from rest_framework.settings import api_settings -from rest_framework.compat import HttpResponseBase, View, set_rollback +from rest_framework.utils import formatting def get_view_name(view_cls, suffix=None): diff --git a/rest_framework/viewsets.py b/rest_framework/viewsets.py index e9fa41fbc..e1c5e17f6 100644 --- a/rest_framework/viewsets.py +++ b/rest_framework/viewsets.py @@ -23,7 +23,7 @@ from functools import update_wrapper from django.utils.decorators import classonlymethod from django.views.decorators.csrf import csrf_exempt -from rest_framework import views, generics, mixins +from rest_framework import generics, mixins, views class ViewSetMixin(object): diff --git a/setup.py b/setup.py index ca802774d..2dfcbc5bd 100755 --- a/setup.py +++ b/setup.py @@ -1,8 +1,9 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -import re import os +import re import sys + from setuptools import setup diff --git a/tests/browsable_api/auth_urls.py b/tests/browsable_api/auth_urls.py index aa3958b0b..0e9379717 100644 --- a/tests/browsable_api/auth_urls.py +++ b/tests/browsable_api/auth_urls.py @@ -1,9 +1,9 @@ from __future__ import unicode_literals -from django.conf.urls import url, include + +from django.conf.urls import include, url from .views import MockView - urlpatterns = [ url(r'^$', MockView.as_view()), url(r'^auth/', include('rest_framework.urls', namespace='rest_framework')), diff --git a/tests/browsable_api/no_auth_urls.py b/tests/browsable_api/no_auth_urls.py index ec273f91c..5fc95c727 100644 --- a/tests/browsable_api/no_auth_urls.py +++ b/tests/browsable_api/no_auth_urls.py @@ -1,6 +1,7 @@ from __future__ import unicode_literals from django.conf.urls import url + from .views import MockView urlpatterns = [ diff --git a/tests/browsable_api/test_browsable_api.py b/tests/browsable_api/test_browsable_api.py index 5f2647838..a9e14f677 100644 --- a/tests/browsable_api/test_browsable_api.py +++ b/tests/browsable_api/test_browsable_api.py @@ -1,4 +1,5 @@ from __future__ import unicode_literals + from django.contrib.auth.models import User from django.test import TestCase diff --git a/tests/browsable_api/views.py b/tests/browsable_api/views.py index 000f4e804..cd020cd32 100644 --- a/tests/browsable_api/views.py +++ b/tests/browsable_api/views.py @@ -1,9 +1,8 @@ from __future__ import unicode_literals -from rest_framework.views import APIView -from rest_framework import authentication -from rest_framework import renderers +from rest_framework import authentication, renderers from rest_framework.response import Response +from rest_framework.views import APIView class MockView(APIView): diff --git a/tests/description.py b/tests/description.py index b46d7f54d..55a55a9f6 100644 --- a/tests/description.py +++ b/tests/description.py @@ -8,7 +8,6 @@ from rest_framework.views import APIView - # test strings snatched from http://www.columbia.edu/~fdc/utf8/, # http://winrus.com/utf8-jap.htm and memory UTF8_TEST_DOCSTRING = ( diff --git a/tests/test_atomic_requests.py b/tests/test_atomic_requests.py index b74f2179e..d0d088f52 100644 --- a/tests/test_atomic_requests.py +++ b/tests/test_atomic_requests.py @@ -2,10 +2,11 @@ from __future__ import unicode_literals from django.conf.urls import patterns, url from django.db import connection, connections, transaction -from django.test import TestCase, TransactionTestCase from django.http import Http404 +from django.test import TestCase, TransactionTestCase from django.utils.decorators import method_decorator from django.utils.unittest import skipUnless + from rest_framework import status from rest_framework.exceptions import APIException from rest_framework.response import Response @@ -13,7 +14,6 @@ from rest_framework.test import APIRequestFactory from rest_framework.views import APIView from tests.models import BasicModel - factory = APIRequestFactory() diff --git a/tests/test_authentication.py b/tests/test_authentication.py index f5d91da3d..91434124e 100644 --- a/tests/test_authentication.py +++ b/tests/test_authentication.py @@ -1,27 +1,26 @@ # coding: utf-8 from __future__ import unicode_literals -from django.conf.urls import url, include + +import base64 + +from django.conf.urls import include, url from django.contrib.auth.models import User from django.http import HttpResponse from django.test import TestCase from django.utils import six -from rest_framework import HTTP_HEADER_ENCODING -from rest_framework import exceptions -from rest_framework import permissions -from rest_framework import renderers -from rest_framework.response import Response -from rest_framework import status + +from rest_framework import ( + HTTP_HEADER_ENCODING, exceptions, permissions, renderers, status +) from rest_framework.authentication import ( - BaseAuthentication, - TokenAuthentication, - BasicAuthentication, - SessionAuthentication, + BaseAuthentication, BasicAuthentication, SessionAuthentication, + TokenAuthentication ) from rest_framework.authtoken.models import Token -from rest_framework.test import APIRequestFactory, APIClient +from rest_framework.response import Response +from rest_framework.test import APIClient, APIRequestFactory from rest_framework.views import APIView -import base64 factory = APIRequestFactory() diff --git a/tests/test_decorators.py b/tests/test_decorators.py index 195f0ba3e..46e4a6ad7 100644 --- a/tests/test_decorators.py +++ b/tests/test_decorators.py @@ -1,22 +1,20 @@ from __future__ import unicode_literals + from django.test import TestCase + from rest_framework import status from rest_framework.authentication import BasicAuthentication +from rest_framework.decorators import ( + api_view, authentication_classes, parser_classes, permission_classes, + renderer_classes, throttle_classes +) from rest_framework.parsers import JSONParser from rest_framework.permissions import IsAuthenticated -from rest_framework.response import Response from rest_framework.renderers import JSONRenderer +from rest_framework.response import Response from rest_framework.test import APIRequestFactory from rest_framework.throttling import UserRateThrottle from rest_framework.views import APIView -from rest_framework.decorators import ( - api_view, - renderer_classes, - parser_classes, - authentication_classes, - throttle_classes, - permission_classes, -) class DecoratorTestCase(TestCase): diff --git a/tests/test_description.py b/tests/test_description.py index 78ce2350b..d0ca40762 100644 --- a/tests/test_description.py +++ b/tests/test_description.py @@ -1,12 +1,17 @@ # -- coding: utf-8 -- from __future__ import unicode_literals + from django.test import TestCase from django.utils.encoding import python_2_unicode_compatible, smart_text + from rest_framework.compat import apply_markdown from rest_framework.views import APIView -from .description import ViewWithNonASCIICharactersInDocstring -from .description import UTF8_TEST_DOCSTRING + +from .description import ( + UTF8_TEST_DOCSTRING, ViewWithNonASCIICharactersInDocstring +) + # We check that docstrings get nicely un-indented. DESCRIPTION = """an example docstring diff --git a/tests/test_fields.py b/tests/test_fields.py index 1c4228d3a..76e6d9d60 100644 --- a/tests/test_fields.py +++ b/tests/test_fields.py @@ -1,11 +1,13 @@ -from decimal import Decimal -from django.utils import timezone -from rest_framework import serializers -import rest_framework import datetime +import uuid +from decimal import Decimal + import django import pytest -import uuid +from django.utils import timezone + +import rest_framework +from rest_framework import serializers # Tests for field keyword arguments and core functionality. diff --git a/tests/test_filters.py b/tests/test_filters.py index c06160f9c..9db685c28 100644 --- a/tests/test_filters.py +++ b/tests/test_filters.py @@ -1,19 +1,22 @@ from __future__ import unicode_literals + import datetime from decimal import Decimal -from django.db import models + from django.conf.urls import url from django.core.urlresolvers import reverse +from django.db import models from django.test import TestCase from django.test.utils import override_settings from django.utils import unittest from django.utils.dateparse import parse_date from django.utils.six.moves import reload_module -from rest_framework import generics, serializers, status, filters + +from rest_framework import filters, generics, serializers, status from rest_framework.compat import django_filters from rest_framework.test import APIRequestFactory -from .models import BaseFilterableItem, FilterableItem, BasicModel +from .models import BaseFilterableItem, BasicModel, FilterableItem factory = APIRequestFactory() diff --git a/tests/test_generics.py b/tests/test_generics.py index 88e792cea..0647119bf 100644 --- a/tests/test_generics.py +++ b/tests/test_generics.py @@ -1,13 +1,16 @@ from __future__ import unicode_literals + import django from django.db import models from django.shortcuts import get_object_or_404 from django.test import TestCase from django.utils import six + from rest_framework import generics, renderers, serializers, status from rest_framework.test import APIRequestFactory -from tests.models import BasicModel, RESTFrameworkModel -from tests.models import ForeignKeySource, ForeignKeyTarget +from tests.models import ( + BasicModel, ForeignKeySource, ForeignKeyTarget, RESTFrameworkModel +) factory = APIRequestFactory() diff --git a/tests/test_htmlrenderer.py b/tests/test_htmlrenderer.py index 1a4a62b25..01ef46d35 100644 --- a/tests/test_htmlrenderer.py +++ b/tests/test_htmlrenderer.py @@ -1,15 +1,17 @@ from __future__ import unicode_literals -from django.core.exceptions import PermissionDenied + +import django.template.loader from django.conf.urls import url +from django.core.exceptions import PermissionDenied from django.http import Http404 +from django.template import Template, TemplateDoesNotExist from django.test import TestCase -from django.template import TemplateDoesNotExist, Template from django.utils import six + from rest_framework import status from rest_framework.decorators import api_view, renderer_classes from rest_framework.renderers import TemplateHTMLRenderer from rest_framework.response import Response -import django.template.loader @api_view(('GET',)) diff --git a/tests/test_metadata.py b/tests/test_metadata.py index 04f4284d9..6510a7070 100644 --- a/tests/test_metadata.py +++ b/tests/test_metadata.py @@ -1,10 +1,14 @@ from __future__ import unicode_literals + +from django.core.validators import MaxValueValidator, MinValueValidator from django.db import models from django.test import TestCase -from django.core.validators import MinValueValidator, MaxValueValidator -from rest_framework import exceptions, metadata, serializers, status, views, versioning -from rest_framework.request import Request + +from rest_framework import ( + exceptions, metadata, serializers, status, versioning, views +) from rest_framework.renderers import BrowsableAPIRenderer +from rest_framework.request import Request from rest_framework.test import APIRequestFactory request = Request(APIRequestFactory().options('/')) diff --git a/tests/test_middleware.py b/tests/test_middleware.py index 36688be4d..49124da61 100644 --- a/tests/test_middleware.py +++ b/tests/test_middleware.py @@ -1,12 +1,12 @@ from django.conf.urls import url from django.contrib.auth.models import User + from rest_framework.authentication import TokenAuthentication from rest_framework.authtoken.models import Token from rest_framework.test import APITestCase from rest_framework.views import APIView - urlpatterns = [ url(r'^$', APIView.as_view(authentication_classes=(TokenAuthentication,))), ] diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index 35fa0e252..28173b687 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -6,15 +6,20 @@ These tests deal with ensuring that we correctly map the model fields onto an appropriate set of serializer fields for each case. """ from __future__ import unicode_literals + import django +import pytest from django.core.exceptions import ImproperlyConfigured -from django.core.validators import MaxValueValidator, MinValueValidator, MinLengthValidator +from django.core.validators import ( + MaxValueValidator, MinLengthValidator, MinValueValidator +) from django.db import models from django.test import TestCase from django.utils import six -import pytest + from rest_framework import serializers -from rest_framework.compat import unicode_repr, DurationField as ModelDurationField +from rest_framework.compat import DurationField as ModelDurationField +from rest_framework.compat import unicode_repr def dedent(blocktext): diff --git a/tests/test_multitable_inheritance.py b/tests/test_multitable_inheritance.py index 15627e1dd..340d4966a 100644 --- a/tests/test_multitable_inheritance.py +++ b/tests/test_multitable_inheritance.py @@ -1,6 +1,8 @@ from __future__ import unicode_literals + from django.db import models from django.test import TestCase + from rest_framework import serializers from tests.models import RESTFrameworkModel diff --git a/tests/test_negotiation.py b/tests/test_negotiation.py index 04b89eb60..434fba496 100644 --- a/tests/test_negotiation.py +++ b/tests/test_negotiation.py @@ -1,10 +1,11 @@ from __future__ import unicode_literals -from django.test import TestCase -from rest_framework.negotiation import DefaultContentNegotiation -from rest_framework.request import Request -from rest_framework.renderers import BaseRenderer -from rest_framework.test import APIRequestFactory +from django.test import TestCase + +from rest_framework.negotiation import DefaultContentNegotiation +from rest_framework.renderers import BaseRenderer +from rest_framework.request import Request +from rest_framework.test import APIRequestFactory factory = APIRequestFactory() diff --git a/tests/test_pagination.py b/tests/test_pagination.py index eb3c78cc5..03c4fdf47 100644 --- a/tests/test_pagination.py +++ b/tests/test_pagination.py @@ -1,11 +1,15 @@ # coding: utf-8 from __future__ import unicode_literals -from rest_framework import exceptions, generics, pagination, serializers, status, filters -from rest_framework.request import Request -from rest_framework.pagination import PageLink, PAGE_BREAK -from rest_framework.test import APIRequestFactory + import pytest +from rest_framework import ( + exceptions, filters, generics, pagination, serializers, status +) +from rest_framework.pagination import PAGE_BREAK, PageLink +from rest_framework.request import Request +from rest_framework.test import APIRequestFactory + factory = APIRequestFactory() diff --git a/tests/test_parsers.py b/tests/test_parsers.py index fe6aec196..1e0f2e17f 100644 --- a/tests/test_parsers.py +++ b/tests/test_parsers.py @@ -1,12 +1,14 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals + from django import forms from django.core.files.uploadhandler import MemoryFileUploadHandler from django.test import TestCase from django.utils.six.moves import StringIO + from rest_framework.exceptions import ParseError -from rest_framework.parsers import FormParser, FileUploadParser +from rest_framework.parsers import FileUploadParser, FormParser class Form(forms.Form): diff --git a/tests/test_permissions.py b/tests/test_permissions.py index 59a85845e..398020002 100644 --- a/tests/test_permissions.py +++ b/tests/test_permissions.py @@ -1,16 +1,22 @@ from __future__ import unicode_literals -from django.contrib.auth.models import User, Permission, Group + +import base64 + +from django.contrib.auth.models import Group, Permission, User +from django.core.urlresolvers import ResolverMatch from django.db import models from django.test import TestCase from django.utils import unittest -from rest_framework import generics, serializers, status, permissions, authentication, HTTP_HEADER_ENCODING -from rest_framework.compat import guardian, get_model_name -from django.core.urlresolvers import ResolverMatch + +from rest_framework import ( + HTTP_HEADER_ENCODING, authentication, generics, permissions, serializers, + status +) +from rest_framework.compat import get_model_name, guardian from rest_framework.filters import DjangoObjectPermissionsFilter from rest_framework.routers import DefaultRouter from rest_framework.test import APIRequestFactory from tests.models import BasicModel -import base64 factory = APIRequestFactory() diff --git a/tests/test_relations.py b/tests/test_relations.py index 25a53f276..fd37e63e3 100644 --- a/tests/test_relations.py +++ b/tests/test_relations.py @@ -1,11 +1,16 @@ import uuid -from .utils import mock_reverse, fail_reverse, BadType, MockObject, MockQueryset + +import pytest from django.core.exceptions import ImproperlyConfigured from django.utils.datastructures import MultiValueDict + from rest_framework import serializers from rest_framework.fields import empty from rest_framework.test import APISimpleTestCase -import pytest + +from .utils import ( + BadType, MockObject, MockQueryset, fail_reverse, mock_reverse +) class TestStringRelatedField(APISimpleTestCase): diff --git a/tests/test_relations_generic.py b/tests/test_relations_generic.py index b600b3333..962857365 100644 --- a/tests/test_relations_generic.py +++ b/tests/test_relations_generic.py @@ -1,9 +1,13 @@ from __future__ import unicode_literals + +from django.contrib.contenttypes.generic import ( + GenericForeignKey, GenericRelation +) from django.contrib.contenttypes.models import ContentType -from django.contrib.contenttypes.generic import GenericRelation, GenericForeignKey from django.db import models from django.test import TestCase from django.utils.encoding import python_2_unicode_compatible + from rest_framework import serializers diff --git a/tests/test_relations_hyperlink.py b/tests/test_relations_hyperlink.py index 33b09713a..c0642eda2 100644 --- a/tests/test_relations_hyperlink.py +++ b/tests/test_relations_hyperlink.py @@ -1,11 +1,13 @@ from __future__ import unicode_literals + from django.conf.urls import url from django.test import TestCase + from rest_framework import serializers from rest_framework.test import APIRequestFactory from tests.models import ( - ManyToManyTarget, ManyToManySource, ForeignKeyTarget, ForeignKeySource, - NullableForeignKeySource, OneToOneTarget, NullableOneToOneSource + ForeignKeySource, ForeignKeyTarget, ManyToManySource, ManyToManyTarget, + NullableForeignKeySource, NullableOneToOneSource, OneToOneTarget ) factory = APIRequestFactory() diff --git a/tests/test_relations_pk.py b/tests/test_relations_pk.py index ca43272b0..169f7d9c5 100644 --- a/tests/test_relations_pk.py +++ b/tests/test_relations_pk.py @@ -1,10 +1,12 @@ from __future__ import unicode_literals + from django.test import TestCase from django.utils import six + from rest_framework import serializers from tests.models import ( - ManyToManyTarget, ManyToManySource, ForeignKeyTarget, ForeignKeySource, - NullableForeignKeySource, OneToOneTarget, NullableOneToOneSource, + ForeignKeySource, ForeignKeyTarget, ManyToManySource, ManyToManyTarget, + NullableForeignKeySource, NullableOneToOneSource, OneToOneTarget ) diff --git a/tests/test_relations_slug.py b/tests/test_relations_slug.py index cd2cb1ed6..680aee417 100644 --- a/tests/test_relations_slug.py +++ b/tests/test_relations_slug.py @@ -1,6 +1,9 @@ from django.test import TestCase + from rest_framework import serializers -from tests.models import NullableForeignKeySource, ForeignKeySource, ForeignKeyTarget +from tests.models import ( + ForeignKeySource, ForeignKeyTarget, NullableForeignKeySource +) class ForeignKeyTargetSerializer(serializers.ModelSerializer): diff --git a/tests/test_renderers.py b/tests/test_renderers.py index 85c6258d2..113c1b66d 100644 --- a/tests/test_renderers.py +++ b/tests/test_renderers.py @@ -1,25 +1,26 @@ # -*- coding: utf-8 -*- from __future__ import unicode_literals -from django.conf.urls import url, include + +import json +import re +from collections import MutableMapping + +from django.conf.urls import include, url from django.core.cache import cache from django.db import models from django.test import TestCase from django.utils import six from django.utils.translation import ugettext_lazy as _ -from rest_framework import status, permissions + +from rest_framework import permissions, serializers, status from rest_framework.compat import OrderedDict -from rest_framework.response import Response -from rest_framework.views import APIView -from rest_framework import serializers from rest_framework.renderers import ( - BaseRenderer, JSONRenderer, BrowsableAPIRenderer, HTMLFormRenderer + BaseRenderer, BrowsableAPIRenderer, HTMLFormRenderer, JSONRenderer ) +from rest_framework.response import Response from rest_framework.settings import api_settings from rest_framework.test import APIRequestFactory -from collections import MutableMapping -import json -import re - +from rest_framework.views import APIView DUMMYSTATUS = status.HTTP_200_OK DUMMYCONTENT = 'dummycontent' diff --git a/tests/test_request.py b/tests/test_request.py index 03d9f8e49..7c62c90a4 100644 --- a/tests/test_request.py +++ b/tests/test_request.py @@ -2,31 +2,30 @@ Tests for content parsing, and form-overloaded content parsing. """ from __future__ import unicode_literals + +import json +from io import BytesIO + +import django +import pytest from django.conf.urls import url -from django.contrib.auth.models import User from django.contrib.auth import authenticate, login, logout +from django.contrib.auth.models import User from django.contrib.sessions.middleware import SessionMiddleware from django.core.handlers.wsgi import WSGIRequest from django.test import TestCase from django.utils import six + from rest_framework import status from rest_framework.authentication import SessionAuthentication from rest_framework.parsers import ( - BaseParser, - FormParser, - MultiPartParser, - JSONParser + BaseParser, FormParser, JSONParser, MultiPartParser ) -from rest_framework.request import Request, Empty +from rest_framework.request import Empty, Request from rest_framework.response import Response from rest_framework.settings import api_settings -from rest_framework.test import APIRequestFactory, APIClient +from rest_framework.test import APIClient, APIRequestFactory from rest_framework.views import APIView -from io import BytesIO -import json -import django -import pytest - factory = APIRequestFactory() diff --git a/tests/test_response.py b/tests/test_response.py index 864478a87..1dd5d5ea0 100644 --- a/tests/test_response.py +++ b/tests/test_response.py @@ -1,21 +1,17 @@ from __future__ import unicode_literals -from django.conf.urls import url, include + +from django.conf.urls import include, url from django.test import TestCase from django.utils import six -from tests.models import BasicModel -from rest_framework.response import Response -from rest_framework.views import APIView -from rest_framework import generics -from rest_framework import routers -from rest_framework import serializers -from rest_framework import status + +from rest_framework import generics, routers, serializers, status, viewsets from rest_framework.renderers import ( - BaseRenderer, - JSONRenderer, - BrowsableAPIRenderer + BaseRenderer, BrowsableAPIRenderer, JSONRenderer ) -from rest_framework import viewsets +from rest_framework.response import Response from rest_framework.settings import api_settings +from rest_framework.views import APIView +from tests.models import BasicModel # Serializer used to test BasicModel diff --git a/tests/test_reverse.py b/tests/test_reverse.py index bf94f9ee1..c5486e5db 100644 --- a/tests/test_reverse.py +++ b/tests/test_reverse.py @@ -1,7 +1,9 @@ from __future__ import unicode_literals + from django.conf.urls import url from django.core.urlresolvers import NoReverseMatch from django.test import TestCase + from rest_framework.reverse import reverse from rest_framework.test import APIRequestFactory diff --git a/tests/test_routers.py b/tests/test_routers.py index 19eeb868d..ae2639bf2 100644 --- a/tests/test_routers.py +++ b/tests/test_routers.py @@ -1,14 +1,17 @@ from __future__ import unicode_literals -from django.conf.urls import url, include + +from collections import namedtuple + +from django.conf.urls import include, url +from django.core.exceptions import ImproperlyConfigured from django.db import models from django.test import TestCase -from django.core.exceptions import ImproperlyConfigured -from rest_framework import serializers, viewsets, permissions + +from rest_framework import permissions, serializers, viewsets from rest_framework.decorators import detail_route, list_route from rest_framework.response import Response -from rest_framework.routers import SimpleRouter, DefaultRouter +from rest_framework.routers import DefaultRouter, SimpleRouter from rest_framework.test import APIRequestFactory -from collections import namedtuple factory = APIRequestFactory() diff --git a/tests/test_serializer.py b/tests/test_serializer.py index b7a0484bc..c18cbb584 100644 --- a/tests/test_serializer.py +++ b/tests/test_serializer.py @@ -1,10 +1,14 @@ # coding: utf-8 from __future__ import unicode_literals -from .utils import MockObject + +import pickle + +import pytest + from rest_framework import serializers from rest_framework.compat import unicode_repr -import pickle -import pytest + +from .utils import MockObject # Tests for core functionality. diff --git a/tests/test_serializer_bulk_update.py b/tests/test_serializer_bulk_update.py index bc955b2ef..8d7240a7b 100644 --- a/tests/test_serializer_bulk_update.py +++ b/tests/test_serializer_bulk_update.py @@ -2,8 +2,10 @@ Tests to cover bulk create and update using serializers. """ from __future__ import unicode_literals + from django.test import TestCase from django.utils import six + from rest_framework import serializers diff --git a/tests/test_serializer_lists.py b/tests/test_serializer_lists.py index 35b68ae7d..e9234d8f7 100644 --- a/tests/test_serializer_lists.py +++ b/tests/test_serializer_lists.py @@ -1,6 +1,7 @@ -from rest_framework import serializers from django.utils.datastructures import MultiValueDict +from rest_framework import serializers + class BasicObject: """ diff --git a/tests/test_settings.py b/tests/test_settings.py index f2ff4ca14..85c36c1fd 100644 --- a/tests/test_settings.py +++ b/tests/test_settings.py @@ -1,5 +1,7 @@ from __future__ import unicode_literals + from django.test import TestCase + from rest_framework.settings import APISettings diff --git a/tests/test_status.py b/tests/test_status.py index 721a6e30b..1cd6e229e 100644 --- a/tests/test_status.py +++ b/tests/test_status.py @@ -1,7 +1,10 @@ from __future__ import unicode_literals + from django.test import TestCase + from rest_framework.status import ( - is_informational, is_success, is_redirect, is_client_error, is_server_error + is_client_error, is_informational, is_redirect, is_server_error, + is_success ) diff --git a/tests/test_templatetags.py b/tests/test_templatetags.py index 0cee91f19..746f51b7e 100644 --- a/tests/test_templatetags.py +++ b/tests/test_templatetags.py @@ -1,9 +1,12 @@ # encoding: utf-8 from __future__ import unicode_literals -from django.test import TestCase -from rest_framework.test import APIRequestFactory -from rest_framework.templatetags.rest_framework import add_query_param, urlize_quoted_links +from django.test import TestCase + +from rest_framework.templatetags.rest_framework import ( + add_query_param, urlize_quoted_links +) +from rest_framework.test import APIRequestFactory factory = APIRequestFactory() diff --git a/tests/test_testing.py b/tests/test_testing.py index b42c08756..70c83f8d9 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -1,13 +1,18 @@ # encoding: utf-8 from __future__ import unicode_literals + +from io import BytesIO + from django.conf.urls import url from django.contrib.auth.models import User from django.shortcuts import redirect from django.test import TestCase + from rest_framework.decorators import api_view from rest_framework.response import Response -from rest_framework.test import APIClient, APIRequestFactory, force_authenticate -from io import BytesIO +from rest_framework.test import ( + APIClient, APIRequestFactory, force_authenticate +) @api_view(['GET', 'POST']) diff --git a/tests/test_throttling.py b/tests/test_throttling.py index 50a53b3eb..6d8d05a32 100644 --- a/tests/test_throttling.py +++ b/tests/test_throttling.py @@ -2,14 +2,18 @@ Tests for the throttling implementations in the permissions module. """ from __future__ import unicode_literals -from django.test import TestCase + from django.contrib.auth.models import User from django.core.cache import cache +from django.test import TestCase + +from rest_framework.response import Response from rest_framework.settings import api_settings from rest_framework.test import APIRequestFactory +from rest_framework.throttling import ( + BaseThrottle, ScopedRateThrottle, UserRateThrottle +) from rest_framework.views import APIView -from rest_framework.throttling import BaseThrottle, UserRateThrottle, ScopedRateThrottle -from rest_framework.response import Response class User3SecRateThrottle(UserRateThrottle): diff --git a/tests/test_urlpatterns.py b/tests/test_urlpatterns.py index f58388c52..fee96878d 100644 --- a/tests/test_urlpatterns.py +++ b/tests/test_urlpatterns.py @@ -1,12 +1,14 @@ from __future__ import unicode_literals + from collections import namedtuple -from django.conf.urls import url, include + +from django.conf.urls import include, url from django.core import urlresolvers from django.test import TestCase + from rest_framework.test import APIRequestFactory from rest_framework.urlpatterns import format_suffix_patterns - # A container class for test paths for the test case URLTestPath = namedtuple('URLTestPath', ['path', 'args', 'kwargs']) diff --git a/tests/test_utils.py b/tests/test_utils.py index a25518424..062f78e11 100644 --- a/tests/test_utils.py +++ b/tests/test_utils.py @@ -1,14 +1,15 @@ from __future__ import unicode_literals -from django.core.exceptions import ImproperlyConfigured + from django.conf.urls import url +from django.core.exceptions import ImproperlyConfigured from django.test import TestCase from django.utils import six -from rest_framework.utils.model_meta import _resolve_model -from rest_framework.utils.breadcrumbs import get_breadcrumbs -from rest_framework.views import APIView -from tests.models import BasicModel import rest_framework.utils.model_meta +from rest_framework.utils.breadcrumbs import get_breadcrumbs +from rest_framework.utils.model_meta import _resolve_model +from rest_framework.views import APIView +from tests.models import BasicModel class Root(APIView): diff --git a/tests/test_validation.py b/tests/test_validation.py index 4234efd36..46e36f5d8 100644 --- a/tests/test_validation.py +++ b/tests/test_validation.py @@ -1,10 +1,13 @@ from __future__ import unicode_literals -from django.core.validators import RegexValidator, MaxValueValidator + +import re + +from django.core.validators import MaxValueValidator, RegexValidator from django.db import models from django.test import TestCase + from rest_framework import generics, serializers, status from rest_framework.test import APIRequestFactory -import re factory = APIRequestFactory() diff --git a/tests/test_validators.py b/tests/test_validators.py index 127ec6f8b..206e882a7 100644 --- a/tests/test_validators.py +++ b/tests/test_validators.py @@ -1,7 +1,9 @@ +import datetime + from django.db import models from django.test import TestCase + from rest_framework import serializers -import datetime def dedent(blocktext): diff --git a/tests/test_versioning.py b/tests/test_versioning.py index 000762b08..4029e0e43 100644 --- a/tests/test_versioning.py +++ b/tests/test_versioning.py @@ -1,14 +1,15 @@ -from .utils import UsingURLPatterns +import pytest from django.conf.urls import include, url -from rest_framework import serializers -from rest_framework import status, versioning + +from rest_framework import serializers, status, versioning from rest_framework.decorators import APIView +from rest_framework.relations import PKOnlyObject from rest_framework.response import Response from rest_framework.reverse import reverse from rest_framework.test import APIRequestFactory, APITestCase from rest_framework.versioning import NamespaceVersioning -from rest_framework.relations import PKOnlyObject -import pytest + +from .utils import UsingURLPatterns class RequestVersionView(APIView): diff --git a/tests/test_views.py b/tests/test_views.py index 77b113ee5..65024609c 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -1,8 +1,10 @@ from __future__ import unicode_literals -import sys import copy +import sys + from django.test import TestCase + from rest_framework import status from rest_framework.decorators import api_view from rest_framework.response import Response diff --git a/tests/test_viewsets.py b/tests/test_viewsets.py index 4d18a955d..0d9b6b310 100644 --- a/tests/test_viewsets.py +++ b/tests/test_viewsets.py @@ -1,10 +1,10 @@ from django.test import TestCase + from rest_framework import status from rest_framework.response import Response from rest_framework.test import APIRequestFactory from rest_framework.viewsets import GenericViewSet - factory = APIRequestFactory() diff --git a/tests/test_write_only_fields.py b/tests/test_write_only_fields.py index dd3bbd6e1..83ef366a5 100644 --- a/tests/test_write_only_fields.py +++ b/tests/test_write_only_fields.py @@ -1,4 +1,5 @@ from django.test import TestCase + from rest_framework import serializers From b4ba8ef4d7cb9f0fd5f64303b609603a7f7e1753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jose=CC=81=20Padilla?= Date: Thu, 25 Jun 2015 16:58:24 -0400 Subject: [PATCH 3/3] Setup isort for code style linting --- .gitignore | 1 + .isort.cfg | 6 ++++++ .travis.yml | 2 +- requirements/requirements-codestyle.txt | 3 +++ runtests.py | 21 ++++++++++++++++++--- tox.ini | 8 ++++---- 6 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 .isort.cfg diff --git a/.gitignore b/.gitignore index 3d5f1043d..e9222c2da 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ MANIFEST !.gitignore !.travis.yml +!.isort.cfg diff --git a/.isort.cfg b/.isort.cfg new file mode 100644 index 000000000..bd5648e08 --- /dev/null +++ b/.isort.cfg @@ -0,0 +1,6 @@ +[settings] +skip=.tox +atomic=true +multi_line_output=5 +known_third_party=pytest,django +known_first_party=rest_framework diff --git a/.travis.yml b/.travis.yml index 2134a144f..50dc368e4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,7 @@ language: python sudo: false env: - - TOX_ENV=py27-flake8 + - TOX_ENV=py27-lint - TOX_ENV=py27-docs - TOX_ENV=py34-django18 - TOX_ENV=py33-django18 diff --git a/requirements/requirements-codestyle.txt b/requirements/requirements-codestyle.txt index 88f61fdfc..1412a4d86 100644 --- a/requirements/requirements-codestyle.txt +++ b/requirements/requirements-codestyle.txt @@ -1,3 +1,6 @@ # PEP8 code linting, which we run on all commits. flake8==2.4.0 pep8==1.5.7 + +# Sort and lint imports +isort==3.9.6 diff --git a/runtests.py b/runtests.py index 0008bfae5..777f996a1 100755 --- a/runtests.py +++ b/runtests.py @@ -1,11 +1,11 @@ #! /usr/bin/env python from __future__ import print_function -import pytest -import sys import os import subprocess +import sys +import pytest PYTEST_ARGS = { 'default': ['tests', '--tb=short'], @@ -14,6 +14,7 @@ PYTEST_ARGS = { FLAKE8_ARGS = ['rest_framework', 'tests', '--ignore=E501'] +ISORT_ARGS = ['--recursive', '--check-only', '.'] sys.path.append(os.path.dirname(__file__)) @@ -30,6 +31,13 @@ def flake8_main(args): return ret +def isort_main(args): + print('Running isort code checking') + ret = subprocess.call(['isort'] + args) + print('isort failed' if ret else 'isort passed') + return ret + + def split_class_and_function(string): class_string, function_string = string.split('.', 1) return "%s and %s" % (class_string, function_string) @@ -50,8 +58,10 @@ if __name__ == "__main__": sys.argv.remove('--nolint') except ValueError: run_flake8 = True + run_isort = True else: run_flake8 = False + run_isort = False try: sys.argv.remove('--lintonly') @@ -67,6 +77,7 @@ if __name__ == "__main__": else: style = 'fast' run_flake8 = False + run_isort = False if len(sys.argv) > 1: pytest_args = sys.argv[1:] @@ -79,7 +90,7 @@ if __name__ == "__main__": expression = split_class_and_function(first_arg) pytest_args = ['tests', '-k', expression] + pytest_args[1:] elif is_class(first_arg) or is_function(first_arg): - # `runtests.py TestCase [flags]` + # `runtests.py TestCase [flags]` # `runtests.py test_function [flags]` pytest_args = ['tests', '-k', pytest_args[0]] + pytest_args[1:] else: @@ -87,5 +98,9 @@ if __name__ == "__main__": if run_tests: exit_on_failure(pytest.main(pytest_args)) + if run_flake8: exit_on_failure(flake8_main(FLAKE8_ARGS)) + + if run_isort: + exit_on_failure(isort_main(ISORT_ARGS)) diff --git a/tox.ini b/tox.ini index f77ba8f8e..d941b6d78 100644 --- a/tox.ini +++ b/tox.ini @@ -3,7 +3,7 @@ addopts=--tb=short [tox] envlist = - py27-{flake8,docs}, + py27-{lint,docs}, {py26,py27}-django14, {py26,py27,py32,py33,py34}-django{15,16}, {py27,py32,py33,py34}-django{17,18,master} @@ -22,14 +22,14 @@ deps = -rrequirements/requirements-testing.txt -rrequirements/requirements-optionals.txt -[testenv:py27-flake8] +[testenv:py27-lint] +commands = ./runtests.py --lintonly deps = -rrequirements/requirements-codestyle.txt -rrequirements/requirements-testing.txt -commands = ./runtests.py --lintonly [testenv:py27-docs] +commands = mkdocs build deps = -rrequirements/requirements-testing.txt -rrequirements/requirements-documentation.txt -commands = mkdocs build