A flake8 plugin that helps you write tidier imports.
Project description
A flake8 plugin that helps you write tidier imports.
Requirements
Python 3.6 to 3.10 supported.
Installation
First, install with pip:
python -m pip install flake8-tidy-imports
Second, check that flake8 lists the plugin in its version line:
$ flake8 --version
3.7.9 (flake8-tidy-imports: 3.1.0, mccabe: 0.6.1, pycodestyle: 2.5.0, pyflakes: 2.1.1) CPython 3.8.0 on Darwin
Third, add the I25 prefix to your select list. For example, if you have your configuration in setup.cfg:
[flake8]
select = E,F,W,I25
Linting a Django project? Check out my book Speed Up Your Django Tests which covers loads of best practices so you can write faster, more accurate tests.
Options
banned-modules
Config for rule I251 (below). Should contain a map where each line is a banned import string, followed by ‘=’, then the message to use when encountering that import.
There is also a special directive to ban a preselected list of removed/moved modules between Python 2 and Python 3, recommending replacements from six where possible. It can be turned on by adding {python2to3} to the list of banned-modules.
For example in setup.cfg:
[flake8]
banned-modules = mock = Use unittest.mock.
{python2to3}
Note that despite the name, you can ban imported objects too, since the syntax is the same. For example:
[flake8]
banned-modules = decimal.Decimal = Use ints and floats only.
ban-relative-imports
When set to ‘true’, enables rule I252 (below), which bans relative imports:
[flake8]
ban-relative-imports = true
(Or, if you want to ban absolute imports, you can put your project’s modules in banned-modules.)
Rules
Note: Before version 4.0.0, the rule codes were numbered 50 lower, e.g. I250 was I200. They were changed in Issue #106 due to conflict with flake8-import-order.
I250: Unnecessary import alias
Complains about unnecessary import aliasing of three forms:
import foo as foo -> import foo
import foo.bar as bar -> from foo import bar
from foo import bar as bar -> from foo import bar
The message includes the suggested rewrite (which may not always be correct), for example:
$ flake8 file.py
file.py:1:1: I250 Unnecessary import alias - rewrite as 'from foo import bar'.
Such aliases can be automatically fixed by isort if you activate its remove_redundant_aliases option.
I251: Banned import <import> used.
Complains about use of banned imports. By default there are no imports banned - you should configure them with banned-modules as described above in ‘Options’.
The message includes a user-defined part that comes from the configuration. For example:
$ flake8 file.py
file.py:1:1: I251 Banned import 'mock' used - use unittest.mock instead.
I252: Relative imports are banned.
Complains about use of relative imports:
from . import foo
from .bar import foo
Needs enabling with ban-relative-imports configuration option.
Absolute imports are recommended by PEP8:
Absolute imports are recommended, as they are usually more readable and tend to be better behaved…
See also
For more advanced control of imports in your project, try import-linter.
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
Built Distribution
Hashes for flake8-tidy-imports-4.3.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e66d46f58ed108f36da920e7781a728dc2d8e4f9269e7e764274105700c0a90c |
|
MD5 | 84b742eab1ec1228dd4d73899b9337fc |
|
BLAKE2b-256 | 5efe49b0b5584fec1f8b205d7970e289c3a2e31c53602bc4f5e78de63fc30673 |
Hashes for flake8_tidy_imports-4.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d6e64cb565ca9474d13d5cb3f838b8deafb5fed15906998d4a674daf55bd6d89 |
|
MD5 | b84a078cd140bac25b8e72b94ad3245e |
|
BLAKE2b-256 | 89b575a6c3896e7e22626457e6c7eedcd08f88ec1494bbc923630304ec2bda78 |