Removes unused imports and unused variables from Python code
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Functions for processing individual files and directories with proper encoding detection, pattern matching, and batch operation support. These functions enable integration into larger workflows and tools.
Core functions for processing individual files with proper encoding detection and error handling.
def fix_file(
filename: str,
args: Mapping[str, Any],
standard_out: IO[str] | None = None
) -> int:
"""
Runs fix_code() on a file and returns exit status.
Processes a single file with autoflake transformations, handling encoding
detection, file I/O, and error reporting.
Args:
filename: Path to the Python file to process
args: Dictionary of configuration options (same keys as fix_code parameters)
standard_out: Optional output stream for results (defaults to stdout)
Returns:
Exit status code (0 for success, non-zero for errors)
"""Functions for finding and filtering Python files based on patterns and criteria.
def find_files(
filenames: list[str],
recursive: bool,
exclude: Iterable[str]
) -> Iterable[str]:
"""
Yields filenames that match the specified criteria.
Discovers Python files from input paths, with support for recursive
directory traversal and pattern-based exclusion.
Args:
filenames: List of file or directory paths to process
recursive: Whether to recurse into subdirectories
exclude: Glob patterns for files/directories to exclude
Yields:
Paths to Python files that match the criteria
"""def is_python_file(filename: str) -> bool:
"""
Returns True if filename refers to a Python source file.
Checks both file extension (.py, .pyi) and shebang line to identify
Python files, including executable scripts.
Args:
filename: Path to file to check
Returns:
True if file is identified as Python source code
"""def is_exclude_file(filename: str, exclude: Iterable[str]) -> bool:
"""
Returns True if file matches any exclusion pattern.
Uses glob pattern matching to determine if a file should be excluded
from processing based on user-specified patterns.
Args:
filename: Path to file to check
exclude: Iterable of glob patterns for exclusion
Returns:
True if file matches any exclusion pattern
"""def match_file(filename: str, exclude: Iterable[str]) -> bool:
"""
Returns True if file is acceptable for processing.
Combines Python file detection and exclusion pattern matching to
determine if a file should be processed.
Args:
filename: Path to file to check
exclude: Iterable of glob patterns for exclusion
Returns:
True if file should be processed (is Python and not excluded)
"""Functions for handling file encoding detection and safe file operations.
def open_with_encoding(
filename: str,
encoding: str | None,
mode: str = "r",
limit_byte_check: int = -1
) -> IO[str]:
"""
Opens file with specified or detected encoding.
Handles encoding detection and file opening with proper error handling
for various encoding scenarios commonly found in Python projects.
Args:
filename: Path to file to open
encoding: Specific encoding to use, or None for auto-detection
mode: File open mode (default "r")
limit_byte_check: Byte limit for encoding detection (-1 for no limit)
Returns:
Opened file handle with correct encoding
"""def detect_encoding(filename: str, limit_byte_check: int = -1) -> str:
"""
Returns the detected encoding of a Python file.
Uses Python's tokenize module to detect file encoding from PEP 263
encoding declarations or defaults to utf-8.
Args:
filename: Path to file to analyze
limit_byte_check: Byte limit for detection (-1 for no limit)
Returns:
Detected encoding name (e.g., 'utf-8', 'latin-1')
"""Functions for generating and formatting diffs showing changes made to code.
def get_diff_text(
old: Sequence[str],
new: Sequence[str],
filename: str
) -> str:
"""
Returns unified diff text between old and new content.
Generates a unified diff format showing changes made to a file,
suitable for display in terminals or integration with version control.
Args:
old: Original lines of content
new: Modified lines of content
filename: File name to display in diff header
Returns:
Unified diff as a string
"""Utility functions for processing text and preserving formatting.
def get_indentation(line: str) -> str:
"""
Returns the leading whitespace from a line.
Extracts indentation (spaces and tabs) to preserve Python code structure
when modifying lines.
Args:
line: Source code line to analyze
Returns:
Leading whitespace characters
"""def get_line_ending(line: str) -> str:
"""
Returns the line ending characters from a line.
Detects and preserves original line endings (\\n, \\r\\n, \\r) to maintain
file format consistency.
Args:
line: Source code line to analyze
Returns:
Line ending characters found in the line
"""import autoflake
# Process a single file with basic options
exit_code = autoflake.fix_file(
"example.py",
{
"remove_unused_variables": True,
"remove_all_unused_imports": True,
"in_place": True
}
)
if exit_code == 0:
print("File processed successfully")
else:
print("Error processing file")import autoflake
# Find all Python files in a project, excluding tests
files = list(autoflake.find_files(
["src/", "lib/"],
recursive=True,
exclude=["*/test_*.py", "*/tests/*", "__pycache__"]
))
print(f"Found {len(files)} Python files to process")import autoflake
import os
# Check if files are Python before processing
for filename in os.listdir("."):
if autoflake.is_python_file(filename):
if not autoflake.is_exclude_file(filename, ["__pycache__"]):
print(f"Would process: {filename}")import autoflake
# Detect encoding before processing
encoding = autoflake.detect_encoding("script.py")
print(f"File encoding: {encoding}")
# Open with detected encoding
with autoflake.open_with_encoding("script.py", encoding) as f:
content = f.read()
# Process with encoding awareness
cleaned = autoflake.fix_code(content, remove_unused_variables=True)import autoflake
# Generate diff showing changes
original_lines = source_code.splitlines(keepends=True)
cleaned_code = autoflake.fix_code(source_code, remove_unused_variables=True)
cleaned_lines = cleaned_code.splitlines(keepends=True)
diff = autoflake.get_diff_text(original_lines, cleaned_lines, "example.py")
print(diff)import autoflake
def process_project(root_dir, config):
"""Process all Python files in a project directory."""
files = autoflake.find_files(
[root_dir],
recursive=True,
exclude=config.get("exclude", [])
)
results = {"success": 0, "errors": 0}
for filename in files:
try:
exit_code = autoflake.fix_file(filename, config)
if exit_code == 0:
results["success"] += 1
else:
results["errors"] += 1
print(f"Error processing {filename}")
except Exception as e:
results["errors"] += 1
print(f"Exception processing {filename}: {e}")
return results
# Usage
config = {
"remove_unused_variables": True,
"remove_all_unused_imports": True,
"in_place": True
}
results = process_project("src/", config)
print(f"Processed {results['success']} files successfully, {results['errors']} errors")Install with Tessl CLI
npx tessl i tessl/pypi-autoflake