Skip to main content

A flake8 plugin that helps you write tidier imports.

Project description

flake8-tidy-imports

https://img.shields.io/pypi/v/flake8-tidy-imports.svg https://img.shields.io/travis/adamchainz/flake8-tidy-imports.svg https://img.shields.io/badge/code%20style-black-000000.svg

A flake8 plugin that helps you write tidier imports.

Installation

Install from pip with:

python -m pip install flake8-tidy-imports

Python 3.5 to 3.8 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

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

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.

See also

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

History

4.1.0 (2020-03-25)

  • Make {python2to3} ban functools.wraps in favour of six.wraps.

4.0.1 (2020-03-24)

  • Fix cStringIO message for {python2to3} built-in banned-modules. Previously it looked for cStringIO.cStringIO which seems to never have existed.

4.0.0 (2020-01-04)

  • Add 50 to all rule codes to avoid conflict with flake8-import-order. This means rule I200 is now I250, etc.

    If you have configured flake8’s select or ignore options, you may need to update it to keep the flake8-tidy-imports rules active.

3.1.0 (2019-11-15)

  • Support Python 3.8.

  • Converted setuptools metadata to configuration file. This meant removing the __version__ attribute from the package. If you want to inspect the installed version, use importlib.metadata.version("flake8-tidy-imports") (docs / backport).

  • Add dependency on importlib-metadata.

3.0.0 (2019-10-15)

  • Add rule I202 to ban relative imports, when activated with the new ban-relative-imports configuration option.

  • Update Python support to 3.5-3.7, as 3.4 has reached its end of life.

  • Update Flake8 support to 3.0+ only. 3.0.0 was released in 2016 and the plugin hasn’t been tested with it since.

2.0.0 (2019-02-02)

  • Drop Python 2 support, only Python 3.4+ is supported now.

1.1.0 (2017-07-10)

  • Added a big list of python 2 to 3 import bans for I201, which can be activated by adding {python2to3} to the banned-modules option.

1.0.6 (2017-03-07)

  • Fixed the whitespace in the help message for --banned-modules.

1.0.5 (2017-01-13)

  • Changed the error message for I201 to be about the banned import instead of module.

  • Fix a bug introduced in 1.0.4 that broke parsing relative imports.

1.0.4 (2017-01-12)

  • Don’t allow installation with Flake8 3.2.0 which doesn’t enable the plugin. This bug was fixed in Flake8 3.2.1.

  • Use the most specific message available for a banned import.

1.0.3 (2016-11-05)

  • Fixed reading config from flake8 3+

1.0.2 (2016-07-04)

  • Fixed I201 rule to detect banned imports like from x import y.

1.0.1 (2016-07-01)

  • I201 rule that allows you to configure complaining about certain modules being imported, e.g. if you are moving from Python 2 to 3 you could stop urlparse being imported in favour of six.moves.urllib.parse.

1.0.0 (2016-01-23)

  • First release on PyPI.

  • I200 rule that complains about unnecessary import aliasing, e.g. from foo import bar as bar.

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

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

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

File metadata

  • Download URL: flake8-tidy-imports-4.1.0.tar.gz
  • Upload date:
  • Size: 24.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

File hashes

Hashes for flake8-tidy-imports-4.1.0.tar.gz
Algorithm Hash digest
SHA256 c30b40337a2e6802ba3bb611c26611154a27e94c53fc45639e3e282169574fd3
MD5 6645c6458718f3e91d5deb700a94c6e0
BLAKE2b-256 590c4d16c56a0f76d290626f2bb18ddf242d69bcf1e2c456956f1d43b1551730

See more details on using hashes here.

File details

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

File metadata

  • Download URL: flake8_tidy_imports-4.1.0-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.2

File hashes

Hashes for flake8_tidy_imports-4.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 62059ca07d8a4926b561d392cbab7f09ee042350214a25cf12823384a45d27dd
MD5 9e0f2b2923e4bfb02e34570c5102564e
BLAKE2b-256 5b8ca3dff01a8f5b7b2341bd86e7392acbc3c05fd8dba91ed687468ab6e9ad88

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