yet another import fixing tool
Project description
Reformats Python imports so that they can pass flake8-import-order. This is roughly:
one import per line
alphabetically sorted, with stylistic options for how dots, case sensitivity, and dotted names are sorted
grouped by builtin / external library / current application (also stylistically controllable)
unused imports removed, using pyflakes to match “unused import” warnings to actual lines of code
duplicate imports removed (note this does not yet include duplicate symbol names against different imports)
no star imports (e.g. from <foo> import *); these are rewritten as explicit names, by importing all the names from each target module and then removing all the unused names.
The program now bolts itself on top of flake8-import-order, at https://github.com/PyCQA/flake8-import-order/, in order to reuse the import classification and sorting styles present there. Without options given, the script will look directly for a setup.cfg file with a [flake8] section and will consume flake8-import-order parameters "application-import-names", "application-package-names", and "import-order-style", to sort imports exactly as this linter then expects to find them. All of the single-line import styles, e.g. google, cryptography, pycharm, should just work.
Special classifications can be given to imports, as either a “ # noqa” comment indicating the import should not be removed, and optionally the comment “ # noqa nosort” which will place the import into a special “don’t sort” category, placing all of the “nosort” imports in the order they originally appeared, grouped after all the sorted imports. This can be used for special situations where a few imports have to be in a certain order against each other (SQLAlchemy has two lines like this at the moment).
The application also does not affect imports that are inside of conditionals or defs, or otherwise indented in any way. This is also the behavior of flake8-import-order; only imports in column zero of the source file are counted, although imports that are on lines below other definitions are counted, which are moved up to the top section of the source file.
zzzeek why are you writing one of these, there are a dozen pep8 import fixers
I’ve just gone through a whole bunch. I need one that:
works directly with flake8-import-order so we are guaranteed to have a match
has shell capability, not only a plugin for vim or sublime text (Python Fix Imports, gratis)
Removes unused imports, not just reformats them (importanize)
Reformats imports, not just removes unused ones (autoflake)
Doesn’t miss removing an import that isn’t used just because it’s on a multiline import (autoflake)
Breaks up all imports into individual lines, not just if the line is >80 char (importanize)
Is still pretty simple (we’re a bit beyond our original “extremely” simple baseline, because all problems are ultimately not that simple) because (since pyflakes and now flake8-import-order do most of the hard work) this is an extremely simple job, there’s (still) no need for a giant application here.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file zimports-0.1.1.tar.gz
.
File metadata
- Download URL: zimports-0.1.1.tar.gz
- Upload date:
- Size: 36.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.7.0
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 58c97a677a9abc1c5174c20ed81b25437273dfb78e749e3178dd6066e45ba485 |
|
MD5 | 4fe3d899008e64c737f20263f2a415e9 |
|
BLAKE2b-256 | 1173e44c33275de8fc354547220b46d84ef8cf4aabf72ce461b4a03b1d0df36e |