flake8-eradicate
flake8-eradicate is a Flake8 plugin designed to identify and flag commented-out code within your Python projects. It helps enforce code cleanliness by pointing out code that might be dead, unused, or temporarily disabled. The current version is 1.5.0, and it maintains an active release cadence, typically releasing updates every few months to support newer Python and Flake8 versions.
Warnings
- breaking Python 3.6 support was dropped in version 1.4.0.
- gotcha The plugin is designed to find *commented-out code blocks*, not just any comments. It targets code that appears to be disabled or left in comments, not descriptive comments.
- gotcha Compatibility with `flake8` versions has evolved. Version 1.2.0 added support for `flake8@4.0.0`, and version 1.3.0 added support for `flake8@5.0`.
- gotcha Fine-tuning which commented-out code is ignored can be done using CLI options, which were introduced in version 1.0.0.
Install
-
pip install flake8-eradicate
Imports
- flake8-eradicate as a plugin
This is a flake8 plugin and does not require direct Python imports in user code. It integrates automatically with flake8 upon installation.
Quickstart
# 1. Install flake8 and flake8-eradicate
pip install flake8 flake8-eradicate
# 2. Create a Python file with commented-out code (e.g., example.py)
# def old_function():
# print('This is old code')
def new_function():
print('This is new code')
# old_function()
# 3. Run flake8 on your project
# It will automatically detect and use the flake8-eradicate plugin.
# The default error code is E7901.
# Expected output: example.py:3:5: E7901 Found commented out code
import os
if os.getenv('RUN_FLAKE8_EXAMPLE', 'false').lower() == 'true':
# In a real scenario, you'd run this from your terminal
# For this example, we simulate the effect by creating a dummy file
# and noting the expected output.
with open('temp_example.py', 'w') as f:
f.write("# def old_function():\n# print('This is old code')\n\ndef new_function():\n print('This is new code')\n\n# old_function()")
print("To run, save the code to 'example.py' and execute: 'flake8 example.py'")
print("Expected output (or similar): 'example.py:3:5: E7901 Found commented out code'")
# You would typically run 'flake8 temp_example.py' in a shell.