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

A flake8 plugin that helps you write tidier imports.

  • Free software: ISC license

Installation

Install from pip with:

pip install flake8-tidy-imports

It will then automatically be run as part of flake8; you can check it has been picked up with:

$ flake8 --version
2.4.1 (pep8: 1.7.0, pyflakes: 0.8.1, flake8-tidy-imports: 1.0.0, mccabe: 0.3.1) CPython 2.7.11 on Darwin

Options

banned-modules

Config for rule I201 (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. Whilst it 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!

Rules

Currently this plugin has two rules.

I200: 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: I200 Unnecessary import alias - rewrite as 'from foo import bar'.

I201: 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: I201 Banned import 'mock' used - use unittest.mock instead.

History

Pending Release

  • New release notes here

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

Uploaded Source

Built Distribution

flake8_tidy_imports-1.0.5-py2.py3-none-any.whl (7.1 kB view details)

Uploaded Python 2Python 3

File details

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

File metadata

File hashes

Hashes for flake8-tidy-imports-1.0.5.tar.gz
Algorithm Hash digest
SHA256 5024be3d247e2239e35a6baf95a9a3b364a62dbfbaa3a290f96c48f746f935e9
MD5 6fd06f45b30847071c7315ff6e12aa9c
BLAKE2b-256 e61319b1f806cea17bf2e4dd45dc7f644bd19767e7f1cbd00d48dc9d14a13835

See more details on using hashes here.

File details

Details for the file flake8_tidy_imports-1.0.5-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for flake8_tidy_imports-1.0.5-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c529fded1761d7da457bba9ca9b81e7201241db285d71be56c2138e88cea0948
MD5 cb3aa43d01e3d543d9cbf8dfccf65334
BLAKE2b-256 b48779882aa4f3fb8cae6000a027ee04c318cd1619c3b94f9bd4709c9144caa9

See more details on using hashes here.

Supported by

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