Skip to main content

A flake8 plugin that helps you write tidier imports.

Project description

https://img.shields.io/github/actions/workflow/status/adamchainz/flake8-tidy-imports/main.yml?branch=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.8 to 3.12 supported.

Installation

First, install with pip:

python -m pip install flake8-tidy-imports

Second, if you define Flake8’s select setting, add the I25 prefix to it. Otherwise, the plugin should be active by default.


Linting a Django project? Check out my book Boost Your Django DX which covers Flake8 and many other code quality tools.


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.

Entries containing * are treated as wildcards matching zero or more path components. For example:

  • example.yellow.* matches example.yellow, example.yellow.truck, example.yellow.truck.driving etc.

  • example.*.truck matches example.truck, example.yellow.truck, example.red.truck, example.big.red.truck, etc.

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.10.0.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

flake8_tidy_imports-4.10.0-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file flake8_tidy_imports-4.10.0.tar.gz.

File metadata

  • Download URL: flake8_tidy_imports-4.10.0.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.4

File hashes

Hashes for flake8_tidy_imports-4.10.0.tar.gz
Algorithm Hash digest
SHA256 bd6cf86465402d2b86903009b748d85a628e599e17b76e810c9857e3a2815173
MD5 f319f2ed59171ea5c175e3a17dccc318
BLAKE2b-256 767ee4b9e42a5434e7f819f50f47b1bd1c04b71539d22f06e917e0d0846e412c

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for flake8_tidy_imports-4.10.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b0387fb2ea200441bd142309e716fb7b8f4b0937bdf5f8b7c0c118a5f5e2b8ed
MD5 8c1b35e05423ccca9d20c9ac7bdb6e8a
BLAKE2b-256 3246ba219b0e774b68993008afe963fd809d2ba28de45ad2f4c8acc345f15249

See more details on using hashes here.

Supported by

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