All Projects → adamchainz → flake8-tidy-imports

adamchainz / flake8-tidy-imports

Licence: MIT License
❄️ A flake8 plugin that helps you write tidier imports.

Programming Languages

python
139335 projects - #7 most used programming language

Labels

Projects that are alternatives of or similar to flake8-tidy-imports

dlint
Dlint is a tool for encouraging best coding practices and helping ensure Python code is secure.
Stars: ✭ 130 (+225%)
Mutual labels:  flake8
flake8-type-checking
Flake8 plugin for managing type-checking imports & forward references.
Stars: ✭ 38 (-5%)
Mutual labels:  flake8
autoflake8
Tool to automatically fix some issues reported by flake8 (forked from autoflake).
Stars: ✭ 14 (-65%)
Mutual labels:  flake8
flake8-annotations
Flake8 Type Annotation Checking
Stars: ✭ 117 (+192.5%)
Mutual labels:  flake8
flake8-assertive
Flake8 unittest assert method checker
Stars: ✭ 30 (-25%)
Mutual labels:  flake8
flake8-docstrings
Integration of pydocstyle and flake8 for combined linting and reporting
Stars: ✭ 73 (+82.5%)
Mutual labels:  flake8
Wemake Python Styleguide
The strictest and most opinionated python linter ever!
Stars: ✭ 1,714 (+4185%)
Mutual labels:  flake8
python-lint
GitHub Action for Lint your code
Stars: ✭ 57 (+42.5%)
Mutual labels:  flake8
flake8-import-order-spoqa
Spoqa's import order style for flake8-import-order
Stars: ✭ 15 (-62.5%)
Mutual labels:  flake8
flake8-broken-line
🚨 Flake8 plugin to forbid backslashes (\) for line breaks
Stars: ✭ 85 (+112.5%)
Mutual labels:  flake8
jupyterlab-flake8
Jupyterlab python linter for notebooks and text files using flake8
Stars: ✭ 105 (+162.5%)
Mutual labels:  flake8
flake8-simplify
❄ A flake8 plugin that helps you to simplify code
Stars: ✭ 97 (+142.5%)
Mutual labels:  flake8
flake8-sql
Flake8 plugin that checks SQL code against opinionated style rules
Stars: ✭ 25 (-37.5%)
Mutual labels:  flake8
flake8-mypy
A plugin for flake8 integrating Mypy.
Stars: ✭ 103 (+157.5%)
Mutual labels:  flake8
flake8-functions
flake8 plugin for validation of function parameters (length, complexity, etc)
Stars: ✭ 32 (-20%)
Mutual labels:  flake8
jira-sprint-analytics
No description or website provided.
Stars: ✭ 13 (-67.5%)
Mutual labels:  flake8
rubric
Linter Config Initializer for Python
Stars: ✭ 21 (-47.5%)
Mutual labels:  flake8
flake8-walrus
flake8 plugin which forbids assignment expressions (the walrus operator)
Stars: ✭ 36 (-10%)
Mutual labels:  flake8
flake8-cognitive-complexity
An extension for flake8 that validates cognitive functions complexity
Stars: ✭ 44 (+10%)
Mutual labels:  flake8
flake8-alfred
Alfred is a flake8 plugin to warn on unsafe/obsolete symbols.
Stars: ✭ 26 (-35%)
Mutual labels:  flake8

flake8-tidy-imports

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.7 to 3.10 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.

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].