← Back to team overview

launchpad-dev team mailing list archive

Re: New coding standard for formatting imports

 

On Thu, Aug 19, 2010 at 6:30 AM, Henning Eggers
<henning.eggers@xxxxxxxxxxxxx> wrote:
> Am 18.08.2010 16:26, schrieb Brad Crittenden:
>> Henning Eggers will be writing a tool to reformat imports and will do it all at once.
>
> OK, the tool is ready and first tests look very promising. Here is what it
> will do:
>
> - Identify the import section at the top of each .py file. Other imports in
> the file are not treated. Early "from __future__" statements are skipped, too.
>
> - Group the imports in three sections as specified by PEP8[1]:
>  1. All standard library imports. I used this list[2].
>  2. Anything not in the other two sections, considered third-party modules.
>     Most notably lazr, storm and twisted but also pytz.
>  3. All imports beginning with "lp" and "canonical", considered local.
>
> - Sort import statements within each section alphabetically by module.
>
> - Break up multiple modules in simple import lines.
>
>    import os, sys, unittest
>
>  becomes
>
>    import os
>    import sys
>    import unittest
>
> - Statements importing only one object are placed on a single line unless the
> statement becomes too long for the line length limit of 78.
>
> - Multiple objects in one import statement are each placed on a line by
> itself, in list style (the core of this change). Objects are sorted
> alphabetically using Python's "sorted" function which sorts Capital letters
> *first*. I know this is a change to the previous style but who are we to argue
> with Python? ;)

Python does both - it all depends if you are running in C locale and
doing byte wise or an English locale and locale aware sorting. As the
end result is supposed to be read and used by humans, I'd go for
locale aware and make the sorting case insensitive.

> If anybody sees a problem with any of these changes, please let me know.

Yup. Other opinions wanted, as we will be stuck with it for a long time.


-- 
Stuart Bishop <stuart@xxxxxxxxxxxxxxxx>
http://www.stuartbishop.net/



Follow ups

References