Skip to content

non-PEP8 compliant sorting #3

@dsvensson

Description

@dsvensson

Weird sorting? I refer to the inline comments below:

import csv
import json
import logging # shouldn't there be a newline here?
from StringIO import StringIO # shouldn't sorting be case-insensitive?
from collections import namedtuple
from urllib import unquote

import boto3
import dateutil
import psycopg2
from datetime import datetime # why is datetime here, and not among the "from.." above?
from datetime import timedelta
from dateutil.parser import parse as dateparser
from dateutil.tz import gettz
from itertools import groupby
from operator import itemgetter

from schema import And # why is schema here, and not together with psycopg2?
from schema import Schema

See details: https://www.python.org/dev/peps/pep-0008/#imports

Specifically:

Imports should be grouped in the following order:

standard library imports
related third party imports
local application/library specific imports

You should put a blank line between each group of imports.

There's also the Reddit extension to this:

Reddit official repository also states that, In general PEP-8 import ordering should be used. However there are a few additions which is

for each imported group the order of imports should be:
import . style lines in alphabetical order
from . import style in alphabetical order

Which would mean:

import datetime
import operator
from datetime import timedelta
from operator import itemgetter

import boto3
import psycopg2
from boto3 import something

import localmoduleA
import localmoduleB
from localmoduleC import foo

This last style is an interpretation of PEP8 as it itsn't specific enough on the subject. I personally favor it, but at least getting the sorting working would be a big win.

I see that the README mentions the following:

pyimpsort.el tries to identify the third party libraries if are installed in in the PYTHONPATH, if a package is not installed it is assumed that belongs to the application. pyimpsort.el also tries to identify if a python virtualenv is activated

Couldn't you just fall back to checking if modules are in the base framework without site-packages, and in the set of imports that aren't found there, split those between the imports that are found via current directory and those that aren't, and you ought to have a decent guess that splits between base modules, external modules, and local modules?

When creating a venv directory in the same directory as the Python file in question, and issueing pyvenv-activate the result is slightly better, see inline comments:

import csv
import json
import logging
from StringIO import StringIO # sort should be case insensitive
from collections import namedtuple
from datetime import datetime
from datetime import timedelta
from operator import itemgetter
from urllib import unquote

import boto3
import dateutil
import psycopg2
from dateutil.parser import parse as dateparser
from dateutil.tz import gettz
from itertools import groupby # this is part of standard library and should not be here
from schema import And
from schema import Schema

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions