Skip to content

Commit b4ba8ef

Browse files
committed
Setup isort for code style linting
1 parent 7351a3f commit b4ba8ef

File tree

6 files changed

+33
-8
lines changed

6 files changed

+33
-8
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ MANIFEST
1414

1515
!.gitignore
1616
!.travis.yml
17+
!.isort.cfg

.isort.cfg

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[settings]
2+
skip=.tox
3+
atomic=true
4+
multi_line_output=5
5+
known_third_party=pytest,django
6+
known_first_party=rest_framework

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ language: python
33
sudo: false
44

55
env:
6-
- TOX_ENV=py27-flake8
6+
- TOX_ENV=py27-lint
77
- TOX_ENV=py27-docs
88
- TOX_ENV=py34-django18
99
- TOX_ENV=py33-django18
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# PEP8 code linting, which we run on all commits.
22
flake8==2.4.0
33
pep8==1.5.7
4+
5+
# Sort and lint imports
6+
isort==3.9.6

runtests.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
#! /usr/bin/env python
22
from __future__ import print_function
33

4-
import pytest
5-
import sys
64
import os
75
import subprocess
6+
import sys
87

8+
import pytest
99

1010
PYTEST_ARGS = {
1111
'default': ['tests', '--tb=short'],
@@ -14,6 +14,7 @@
1414

1515
FLAKE8_ARGS = ['rest_framework', 'tests', '--ignore=E501']
1616

17+
ISORT_ARGS = ['--recursive', '--check-only', '.']
1718

1819
sys.path.append(os.path.dirname(__file__))
1920

@@ -30,6 +31,13 @@ def flake8_main(args):
3031
return ret
3132

3233

34+
def isort_main(args):
35+
print('Running isort code checking')
36+
ret = subprocess.call(['isort'] + args)
37+
print('isort failed' if ret else 'isort passed')
38+
return ret
39+
40+
3341
def split_class_and_function(string):
3442
class_string, function_string = string.split('.', 1)
3543
return "%s and %s" % (class_string, function_string)
@@ -50,8 +58,10 @@ def is_class(string):
5058
sys.argv.remove('--nolint')
5159
except ValueError:
5260
run_flake8 = True
61+
run_isort = True
5362
else:
5463
run_flake8 = False
64+
run_isort = False
5565

5666
try:
5767
sys.argv.remove('--lintonly')
@@ -67,6 +77,7 @@ def is_class(string):
6777
else:
6878
style = 'fast'
6979
run_flake8 = False
80+
run_isort = False
7081

7182
if len(sys.argv) > 1:
7283
pytest_args = sys.argv[1:]
@@ -79,13 +90,17 @@ def is_class(string):
7990
expression = split_class_and_function(first_arg)
8091
pytest_args = ['tests', '-k', expression] + pytest_args[1:]
8192
elif is_class(first_arg) or is_function(first_arg):
82-
# `runtests.py TestCase [flags]`
93+
# `runtests.py TestCase [flags]`
8394
# `runtests.py test_function [flags]`
8495
pytest_args = ['tests', '-k', pytest_args[0]] + pytest_args[1:]
8596
else:
8697
pytest_args = PYTEST_ARGS[style]
8798

8899
if run_tests:
89100
exit_on_failure(pytest.main(pytest_args))
101+
90102
if run_flake8:
91103
exit_on_failure(flake8_main(FLAKE8_ARGS))
104+
105+
if run_isort:
106+
exit_on_failure(isort_main(ISORT_ARGS))

tox.ini

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ addopts=--tb=short
33

44
[tox]
55
envlist =
6-
py27-{flake8,docs},
6+
py27-{lint,docs},
77
{py26,py27}-django14,
88
{py26,py27,py32,py33,py34}-django{15,16},
99
{py27,py32,py33,py34}-django{17,18,master}
@@ -22,14 +22,14 @@ deps =
2222
-rrequirements/requirements-testing.txt
2323
-rrequirements/requirements-optionals.txt
2424

25-
[testenv:py27-flake8]
25+
[testenv:py27-lint]
26+
commands = ./runtests.py --lintonly
2627
deps =
2728
-rrequirements/requirements-codestyle.txt
2829
-rrequirements/requirements-testing.txt
29-
commands = ./runtests.py --lintonly
3030

3131
[testenv:py27-docs]
32+
commands = mkdocs build
3233
deps =
3334
-rrequirements/requirements-testing.txt
3435
-rrequirements/requirements-documentation.txt
35-
commands = mkdocs build

0 commit comments

Comments
 (0)