Flake8 plugin to find commented out (or so called "dead") code
npx @tessl/cli install tessl/pypi-flake8-eradicate@1.5.0A flake8 plugin that detects commented out (or so called "dead") code in Python files. It helps maintain code quality by identifying code that has been commented out but not removed, encouraging proper code cleanup practices.
pip install flake8-eradicateThe plugin is used through flake8 and doesn't require direct imports in code:
# No direct imports needed - plugin integrates with flake8For programmatic usage (not typical):
from flake8_eradicate import CheckerInstall and use with flake8:
# Install the plugin
pip install flake8-eradicate
# Run flake8 with the plugin
flake8 your_file.py
# The plugin will automatically detect commented out code
# Example output:
# your_file.py:5:1: E800 Found commented out codeConfigure options in setup.cfg:
[flake8]
eradicate-aggressive = True
eradicate-whitelist = noqa#fmtOr via command line:
flake8 --eradicate-aggressive your_file.py
flake8 --eradicate-whitelist="pattern1#pattern2" your_file.pyFlake8 plugin class that integrates with the flake8 linting framework to detect commented out code.
class Checker:
"""Flake8 plugin to find commented out code."""
name: str
version: str
options: ClassVar[Optional[Dict[str, Any]]]
def __init__(
self,
tree,
file_tokens: List[tokenize.TokenInfo],
lines: Sequence[str]
) -> None:
"""
Constructor called by flake8 for each file.
Parameters:
- tree: AST tree (unused by this plugin)
- file_tokens: All tokens for the current file
- lines: All lines in the current file
"""
def run(self) -> Iterator[Tuple[int, int, str, Type['Checker']]]:
"""
Main entry point called by flake8 to check for violations.
Returns:
Iterator yielding error tuples (line_no, column, message, checker_type)
"""
@classmethod
def add_options(cls, parser: OptionManager) -> None:
"""
Registers plugin-specific command line options with flake8.
Parameters:
- parser: flake8 option parser instance
"""
@classmethod
def parse_options(cls, options) -> None:
"""
Stores parsed options for use by checker instances.
Parameters:
- options: Parsed options object from flake8
"""The plugin supports three configuration options that can be specified via command line or configuration file.
# Command line option: --eradicate-aggressive
# Config file option: eradicate-aggressive = True/False
# Default: FalseEnables aggressive mode for better detection but may produce false positives.
# Command line option: --eradicate-whitelist="pattern1#pattern2"
# Config file option: eradicate-whitelist = pattern1#pattern2
# Default: FalseCustom whitelist patterns (# separated) that override default patterns. Patterns are interpreted as regex.
# Command line option: --eradicate-whitelist-extend="pattern1#pattern2"
# Config file option: eradicate-whitelist-extend = pattern1#pattern2
# Default: FalseCustom whitelist patterns (# separated) that extend default patterns. Takes precedence over --eradicate-whitelist. Patterns are interpreted as regex.
The plugin detects a single type of error code.
# Error Code: E800
# Message: "Found commented out code"Reported when the plugin detects lines containing commented out Python code that should be removed.
from typing import Any, ClassVar, Dict, Iterable, Iterator, List, Optional, Sequence, Tuple, Type
import tokenize
from flake8.options.manager import OptionManager
# Core types used by the plugin
TokenInfo = tokenize.TokenInfo
OptionManager = OptionManager
# Error tuple format returned by run()
ErrorTuple = Tuple[int, int, str, Type['Checker']]# setup.cfg
[flake8]
# Enable aggressive mode
eradicate-aggressive = True
# Custom whitelist (overrides defaults)
eradicate-whitelist = noqa#fmt#type
# Extend default whitelist (preferred)
eradicate-whitelist-extend = custom_pattern#another_pattern# Basic usage - plugin runs automatically
flake8 myproject/
# With aggressive mode
flake8 --eradicate-aggressive myproject/
# With custom whitelist override
flake8 --eradicate-whitelist="noqa#fmt" myproject/
# With whitelist extension
flake8 --eradicate-whitelist-extend="custom" myproject/
# Select only E800 errors
flake8 --select=E800 myproject/import tokenize
from flake8_eradicate import Checker
# Setup options (normally done by flake8)
class MockOptions:
eradicate_aggressive = False
eradicate_whitelist = False
eradicate_whitelist_extend = False
Checker.options = MockOptions()
# Read file content
with open('example.py', 'r') as f:
lines = f.readlines()
# Tokenize file
with open('example.py', 'r') as f:
tokens = list(tokenize.generate_tokens(f.readline))
# Create checker instance
checker = Checker(tree=None, file_tokens=tokens, lines=lines)
# Get violations
violations = list(checker.run())
for line_no, col, message, checker_type in violations:
print(f"Line {line_no}: {message}")