Skip to main content

A flake8 plugin that helps you write tidier imports.

Project description

https://img.shields.io/github/workflow/status/adamchainz/flake8-tidy-imports/CI/main?style=for-the-badge https://img.shields.io/pypi/v/flake8-tidy-imports.svg?style=for-the-badge https://img.shields.io/badge/code%20style-black-000000.svg?style=for-the-badge pre-commit

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

Controls rule I252 (below). Accepts two values:

  • parents - bans imports from parent modules (and grandparents, etc.), i.e. with more than one ..

  • true - bans all relative imports.

For example:

[flake8]
ban-relative-imports = parents

(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 <from parent modules> are banned.

Complains about use of relative imports:

  • from . import foo (sibling import)

  • from .bar import foo (sibling import)

  • from .. import foo (parent import)

Controlled by the ban-relative-imports configuration option.

Absolute imports, or relative imports from siblings, are recommended by PEP8:

Absolute imports are recommended, as they are usually more readable and tend to be better behaved…

import mypkg.sibling
from mypkg import sibling
from mypkg.sibling import example

However, explicit relative imports are an acceptable alternative to absolute imports…

from . import sibling
from .sibling import example

See also

For more advanced control of imports in your project, try import-linter.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flake8-tidy-imports-4.5.0.tar.gz (13.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flake8_tidy_imports-4.5.0-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file flake8-tidy-imports-4.5.0.tar.gz.

File metadata

  • Download URL: flake8-tidy-imports-4.5.0.tar.gz
  • Upload date:
  • Size: 13.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7

File hashes

Hashes for flake8-tidy-imports-4.5.0.tar.gz
Algorithm Hash digest
SHA256 ac637961d0f319012d099e49619f8c928e3221f74e00fe6eb89513bc64c40adb
MD5 e1a4227f75ec63b7ad057cf91a7b8408
BLAKE2b-256 4cec40fdf23f234e37fcf2e43bca5fb2c41277e3c693b3b5228e43f872a459b1

See more details on using hashes here.

File details

Details for the file flake8_tidy_imports-4.5.0-py3-none-any.whl.

File metadata

  • Download URL: flake8_tidy_imports-4.5.0-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.7

File hashes

Hashes for flake8_tidy_imports-4.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 87eed94ae6a2fda6a5918d109746feadf1311e0eb8274ab7a7920f6db00a41c9
MD5 90474f2030bb2d36612a4451f792b352
BLAKE2b-256 36c4e33783bee9cf9ef16d6cbfb3e86ef93c664bce42569baccbe4a456d94728

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page