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/master?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.

Installation

Install from pip with:

python -m pip install flake8-tidy-imports

Python 3.6 to 3.9 supported.

When installed it will automatically be run as part of flake8; you can check it is being picked up with:

$ 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

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 (see below). A map where each line is a banned import string, followed by ‘=’, then the message to use when encountering that banned import. Note that despite the name, you can ban imported objects too, since the syntax is the same, such as decimal.Decimal.

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.

Whilst the option can be passed on the commandline, it’s much easier to configure it in your config file, such as setup.cfg, for example:

[flake8]
banned-modules = mock = use unittest.mock!
                 urlparse = use six.moves.urllib.parse!
                 {python2to3}

ban-relative-imports

Enables rule I252, which bans relative imports. See below.

[flake8]
ban-relative-imports = true

(If you want to ban absolute imports, put your project’s modules in banned-modules.)

Rules

N.B. Before version 4.0.0, the rule codes were numbered 50 less, 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 be correct at current), for example:

$ flake8 file.py
file.py:1:1: I250 Unnecessary import alias - rewrite as 'from foo import bar'.

I251: Banned import ‘foo’ used

Complains about importing of banned imports. This might be useful when refactoring code, for example when moving from Python 2 to 3. 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


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.2.0.tar.gz (14.6 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.2.0-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: flake8-tidy-imports-4.2.0.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0

File hashes

Hashes for flake8-tidy-imports-4.2.0.tar.gz
Algorithm Hash digest
SHA256 6cdd51e0d2f221e43ff4d5ac6331b1d95bbf4a5408906e36da913acaaed890e0
MD5 8876846cb2eeb722b7ac2f894af98899
BLAKE2b-256 ba186cfcb403d66cae69c235187a821d86fbb65efe9f6d5fc6bf133319dce11a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: flake8_tidy_imports-4.2.0-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.25.0 setuptools/51.0.0 requests-toolbelt/0.9.1 tqdm/4.50.2 CPython/3.9.0

File hashes

Hashes for flake8_tidy_imports-4.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2821c79e83c656652d5ac6d3650ca370ed3c9752edb5383b1d50dee5bd8a383f
MD5 37490750cc11a6f4f1e2a476d4ac424c
BLAKE2b-256 7a8c209ceb3debd539b8d63a038aa4aa1fc97ff22df2dc3d307a6ee87e901d9f

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