1

我正在尝试为 django-rest-framework 做出贡献,我运行后身份验证文件中的导入isort是这样的(我添加了导入六):

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

当我运行./runtests --lintonly它时,它通过了所有测试,但是当我运行tox它时,它给了我这个错误:

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'
4

2 回答 2

2

我遇到了类似的错误(Imports are incorrectly sorted)。

isort直接运行时很高兴,通过运行时失败toxisort抱怨的线路是:

import pytest

from my_module import MyThing

isort,当直接运行时,知道我的模块my_module是第一方(我自己的)代码,而通过tox它不是。pytest因此,当直接运行时,它对import 和 my import之间的空行感到满意,但通过tox它不想看到空行,因为两者都pytestmy_module解释为第三方导入。

解决方案是将此行添加到我的setup.cfg

[isort]
...
known_first_party = my_module
于 2018-01-01T10:42:25.160 回答
1

从我在 REST 框架源代码中看到的(例如这里),six是从django.utils. 替换import sixfrom django.utils import six应该解决isort警告:

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
于 2016-04-03T13:03:27.170 回答