import sorting in python code

920 views
Skip to first unread message

John Sirois

unread,
Feb 6, 2015, 12:56:15 PM2/6/15
to pants-devel
Our import sorting is now automated via isort [1] and checked in ci.

You may see an error like so:

Given:
$ git diff
diff --git a/src/python/pants/binary_util.py b/src/python/pants/binary_util.py
index ab5df55..590c1db 100644
--- a/src/python/pants/binary_util.py
+++ b/src/python/pants/binary_util.py
@@ -5,10 +5,10 @@
 from __future__ import (absolute_import, division, generators, nested_scopes, print_function,
                         unicode_literals, with_statement)
 
-import os
 import posixpath
 import subprocess
 from contextlib import closing, contextmanager
+import os
 
 from twitter.common import log
 from twitter.common.collections import OrderedSet

A CI run will say:
$ ./build-support/bin/ci.sh
...
ERROR: /home/jsirois/dev/3rdparty/pants/src/python/pants/binary_util.py Imports are incorrectly sorted.

To fix import sort order, run `build-support/bin/isort.sh -f`

And you just follow the instructions and run`build-support/bin/isort.sh -f` to fix imports up.

So you may want to get in the habit or running the isort fix early and often before even posting RBs or pushing pull request CI branches.



Benjy Weinberger

unread,
Feb 9, 2015, 3:26:52 PM2/9/15
to John Sirois, pants-devel
Nice! Can we put some sort of wrapper script around this? I don't want to force everyone to remember that magic incantation (specifically that the thing is called 'isort' and requires the '-f' flag).

Eric Zundel Ayers

unread,
Feb 9, 2015, 3:29:16 PM2/9/15
to Benjy Weinberger, John Sirois, pants-devel
I hit this and the CI build fails pretty early saying, "run build-support/bin/isort.sh -f" pretty clearly so its easy to find and fix (didn't seem like magic to me)

John Sirois

unread,
Feb 9, 2015, 3:29:29 PM2/9/15
to Benjy Weinberger, pants-devel
You can do this - sure.  I think the best approach though is to impliement a commit hook and add docs to the dev center on how to install the local commit hook.  This will make all checks available via 1 step no matter what those checks are called going forward.

Brian Wickman

unread,
Feb 9, 2015, 3:30:48 PM2/9/15
to John Sirois, Benjy Weinberger, pants-devel
For examples, you can see what we do in the Aurora repo (purely opt-in):

Benjy Weinberger

unread,
Feb 9, 2015, 8:56:18 PM2/9/15
to Brian Wickman, John Sirois, pants-devel
Word to the wise: isort will fail the CI not only on incorrectly sorted imports but on files that don't end in a newline.

John Sirois

unread,
Mar 7, 2015, 1:56:35 AM3/7/15
to Benjy Weinberger, Brian Wickman, pants-devel
On Mon, Feb 9, 2015 at 6:55 PM, Benjy Weinberger <be...@foursquare.com> wrote:
Word to the wise: isort will fail the CI not only on incorrectly sorted imports but on files that don't end in a newline.

On Mon, Feb 9, 2015 at 12:30 PM, Brian Wickman <wic...@twitter.com> wrote:
For examples, you can see what we do in the Aurora repo (purely opt-in):


On Mon, Feb 9, 2015 at 12:29 PM, John Sirois <john....@gmail.com> wrote:
You can do this - sure.  I think the best approach though is to impliement a commit hook and add docs to the dev center on how to install the local commit hook.  This will make all checks available via 1 step no matter what those checks are called going forward.

On Mon, Feb 9, 2015 at 1:26 PM, Benjy Weinberger <be...@foursquare.com> wrote:
Nice! Can we put some sort of wrapper script around this? I don't want to force everyone to remember that magic incantation (specifically that the thing is called 'isort' and requires the '-f' flag).

Its still a magic incantation, but its documented and consolidated/stable now at least: https://rbcommons.com/s/twitter/r/1883/
Reply all
Reply to author
Forward
0 new messages