flake8-tidy-imports

4.12.0 · active · verified Wed Apr 15

A flake8 plugin that helps you write tidier imports by enforcing rules on import aliases, banned modules, and relative imports. It is currently at version 4.12.0, actively maintained, and supports Python versions 3.10 and newer. Releases typically occur a few times a year, with major versions introducing significant changes.

Warnings

Install

Quickstart

After installation, `flake8-tidy-imports` automatically registers itself with `flake8`. You typically configure it via a `setup.cfg`, `pyproject.toml`, or `.flake8` file. The primary options are `banned-modules` to specify disallowed imports with custom messages and `ban-relative-imports` to control relative import restrictions. If you use `flake8`'s `select` option, ensure you include `I25` to activate the plugin's checks.

import os

# Example file: my_module.py
# import json as json # I250: Unnecessary import alias
# from datetime import timedelta as timedelta # I250: Unnecessary import alias
# from ..utils import helper # I252: Relative import from parent module (if configured)
# import mock # I251: Banned import 'mock' used - use unittest.mock instead. (if configured)

# Configure flake8 in setup.cfg or pyproject.toml
# [flake8]
# banned-modules = 
#   mock = use unittest.mock instead.
#   django.utils.six = no more six!
#   {python2to3}
# ban-relative-imports = parents
# select = I25

# To run flake8:
# flake8 my_module.py

# For demonstration purposes, this code is not meant to be run directly
# but shows common violations flake8-tidy-imports would catch.
# The actual plugin operates via the flake8 command-line tool.

print("Run 'flake8 <your_file.py>' after configuring flake8-tidy-imports.")

view raw JSON →