Skip to main content

No project description provided

Project description

GitHub License

Flake8 Import Guard

Flake8 Import Guard is a Flake8 plugin that helps enforce import restrictions in your Python projects. It allows you to specify forbidden imports and detects their usage in your codebase, focusing on newly added imports in version-controlled files.

Features

  • 🚫 Detects forbidden imports in new and modified files
  • 🔧 Configurable via .flake8 or pyproject.toml
  • 🔍 Focuses on newly added imports in Git-versioned files
  • 🔗 Seamless integration with existing Flake8 workflows

Installation

You can install Flake8 Import Guard using pip.

pip install flake8-import-guard

Usage

Once installed, Flake8 Import Guard will automatically be used by Flake8. You can run it using the standard Flake8 command.

flake8 path/to/your/code

Configuration

You can configure Flake8 Import Guard using Flake8's standard configuration system or through pyproject.toml.

Using Flake8 Configuration

Add the following to your .flake8 file.

[flake8]
forbidden_imports = load_dotenv,subprocess

Using pyproject.toml

Add the following to your pyproject.toml file.

[tool.flake8-import-guard]
forbidden_imports = [
    "load_dotenv",
    "subprocess"
]

Example

Configuration

Let's say you have the following configuration in your .flake8 file.

[flake8]
forbidden_imports = load_dotenv,subprocess

Sample Python File

Consider the following Python file.

# test_file.py
import os
from datetime import datetime

from subprocess import check_output  # Violation Module

from dotenv import load_dotenv       # Violation Module

def main():
    pass

if __name__ == "__main__":
    main()

Execution and Result

When you run Flake8 on this file, you'll get the following output.

$ flake8 test_file.py
test_file.py:4:1: CPE001 Forbidden import found: subprocess.check_output
test_file.py:6:1: CPE001 Forbidden import found: dotenv.load_dotenv

How It Works

Flake8 Import Guard uses Git to detect changes in your codebase.

  1. For new files, it checks all imports against the forbidden list.
  2. For existing files, it compares the current version with the last committed version to identify newly added imports.
  3. Only newly added imports that match the forbidden list are reported as violations.

Capabilities and Limitations

What It Can Do

  • Detect newly added forbidden imports in both new and existing files
  • Work with Git-versioned projects
  • Configure forbidden imports through Flake8 config or pyproject.toml
  • Integrate seamlessly with existing Flake8 workflows

What It Cannot Do

  • Work in non-Git environments
  • Identify removed or modified imports (focus is on new additions only)
  • Detect indirect imports (e.g., imports within imported modules)

Error Codes

  • CPE001: Forbidden import found

Contributing

Contributions are welcome! Please feel free to submit a Pull Request. See CONTRIBUTING.md to get an idea of how contributions work.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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_import_guard-0.1.0.tar.gz (5.0 kB view details)

Uploaded Source

Built Distribution

flake8_import_guard-0.1.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file flake8_import_guard-0.1.0.tar.gz.

File metadata

  • Download URL: flake8_import_guard-0.1.0.tar.gz
  • Upload date:
  • Size: 5.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.14 Linux/6.5.0-1024-azure

File hashes

Hashes for flake8_import_guard-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bcf6804f0c8686711a1d7cfb5081ef36d04c6bb688512ad8bd587655ed686c5a
MD5 929df75b754a669591408aa996433d0d
BLAKE2b-256 acc3c97c4a0a6bae806ec309e497960bf43936e0c753b2c6e38add5c8aabea6c

See more details on using hashes here.

File details

Details for the file flake8_import_guard-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for flake8_import_guard-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a5062c122f7d202190d8ad5cf359ac7f32c21413b70c9a0b3985ebbdafb915e2
MD5 7875ffc6b210fb715652c8a2d77d6b4f
BLAKE2b-256 929f92cb7f695d6f79f301284f7a9297bc4e345f4069c2bf3ae65b3fe6625f8a

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