Search code examples
pythondjango-rest-frameworkpep8tox

Incorrectly ordered imports using tox


I am trying to make a contribution to django-rest-framework, the imports in authentication file after I run isort are like this(I have added import six):

from __future__ import unicode_literals

import base64

import six
from django.contrib.auth import authenticate, get_user_model
from django.middleware.csrf import CsrfViewMiddleware
from django.utils.translation import ugettext_lazy as _

from rest_framework import HTTP_HEADER_ENCODING, exceptions

when I run ./runtests --lintonly it passes all the tests but when I run tox it gives me this error:

py27-lint runtests: commands[0] | ./runtests.py --lintonly
Running flake8 code linting
flake8 passed
Running isort code checking
ERROR: /home/nitesh/open_source/django-rest-framework/rest_framework/authentication.py Imports are incorrectly sorted.
isort failed: Some modules have incorrectly ordered imports. Fix by running `isort --recursive .`
ERROR: InvocationError: '/home/nitesh/open_source/django-rest-framework/runtests.py --lintonly'

Solution

  • From what I see in the REST framework source code (e.g. here), six is imported from the django.utils. Replacing import six with from django.utils import six should resolve the isort warning:

    from __future__ import unicode_literals
    
    import base64
    
    from django.utils import six
    from django.contrib.auth import authenticate, get_user_model
    from django.middleware.csrf import CsrfViewMiddleware
    from django.utils.translation import ugettext_lazy as _
    
    from rest_framework import HTTP_HEADER_ENCODING, exceptions