Alphabetize is a Flake8 plugin for checking the
order of import
statements and the __all__
list. It is designed to work
well with the Black formatting tool,
in that Black never alters the
Abstract Syntax Tree (AST), while
Alphabetize is only interested in the AST, and so the two tools never conflict. In
the spirit of Black, Alphabetize is an 'uncompromising import style checker' in that
the style can’t be configured, there’s just one style (see below for the rules).
Alphabetise is released under the Unlicense. It is tested on Python 3.6+.
-
Create a virtual environment:
python3 -m venv venv
-
Activate it:
source venv/bin/activate
-
Install:
pip install flake8-alphabetize
Say we have a Python file:
from datetime import time, date
print(time(9, 39), date(2021, 4, 11))
by running the command flake8
we’ll get:
myfile.py:1:1: AZ200 Imported names are in the wrong order. Should be date, time
We can tell Alphabetize what the package name is, and then it’ll know that its imports should be in group at the bottom of the imports. Here’s an example:
import uuid
from myapp import myfunc
print(uuid.UUID4(), myfunc())
by running the command flake8 --application-names myapp
we won’t get any
errors.
As you use Flake8 in the normal way, Alphabetize will report errors using the following codes:
Code | Error Type |
---|---|
AZ100 |
Import statements are in the wrong order |
AZ200 |
The names in the |
AZ300 |
Two |
AZ400 |
The names in the |
Alphabetize follows the Black formatter’s uncompromising approach and so there’s only
one configuration option which is application-names
. This is a comma-separated list
of top-level, package names that are to be treated as application imports, eg. 'myapp'.
Since Alphabetize is a Flake8 plugin, this configuration option is set using
Flake8 configuration.
Here are the ordering rules that Alphabetize follows:
-
The special case
from __future__
import comes first. -
Imports from the standard library come next, followed by third party imports, followed by application imports.
-
Relative imports are assumed to be application imports.
-
The standard library group has
import
statements first (in alphabetical order), followed byfrom import
statements (in alphabetical order). -
The third party group is further grouped by library name. Then each library subgroup has
import
statements first (in alphabetical order), followed byfrom import
statements (in alphabetical order). -
The application group is further grouped by import level, with absolute imports first and then relative imports of increasing level. Within each level, the imports should be ordered by library name. Then each library subgroup has
import
statements first (in alphabetical order), followed byfrom import
statements (in alphabetical order). -
from import
statements for the same library must be combined. -
Alphabetize only looks at imports at the module level, any imports within the code are ignored.
Run tox
to make sure all tests pass, then update the release notes, then do:
git tag -a x.y.z -m "version x.y.z" rm -r build rm -r dist python setup.py sdist bdist_wheel for f in dist/*; do gpg --detach-sign -a $f; done twine upload dist/*
-
Fix bug where
from . import logging
appears in message asfrom .None import logging
.
-
Fix bug where it fails on a relative import such as
from . import logging
.
-
There’s a clash of option names, so now application imports can now be identified by setting the
application-names
configuration option.
-
Application imports can now be identified by setting the
application-package-names
configuration option.