Source Code Spelling Correction
Typos is a fast and low false-positive source code spell checker written in Rust, provided as a Python package primarily for command-line interface (CLI) usage or integration into CI/CD workflows. It focuses on correcting a curated list of 'known misspellings' to ensure high confidence and minimize false positives. The library maintains a monthly release cadence, primarily updating its internal dictionary and applying minor fixes.
Warnings
- gotcha The `typos` Python package is a wrapper around a Rust-based binary. Its primary interface is the command-line interface (CLI). Direct programmatic access via `import typos` to Python functions/classes for core spell-checking logic is generally not available, and interaction is typically done by calling the `typos` executable via `subprocess`.
- gotcha Configuration for `typos` is primarily managed through TOML files like `_typos.toml` or `typos.toml` in your project's root or parent directories, not through Python code. These files allow you to define custom dictionaries, ignore patterns, and exclude files.
- gotcha `typos` operates on a 'known misspellings' dictionary, meaning it only corrects errors it explicitly recognizes, rather than attempting to guess corrections for any word not in a dictionary. This design leads to a very low false-positive rate but may not catch every possible typo if it's not in its internal list of known errors.
- gotcha When running `typos` in a `subprocess` and `check=True` is used, a `CalledProcessError` will be raised if `typos` finds any errors (returns exit code 2). This is standard behavior for `subprocess.run` when the command exits with a non-zero status, but it signifies 'typos found', not necessarily an execution error of the `typos` tool itself.
Install
-
pip install typos
Imports
- typos_cli
import subprocess # Or directly run 'typos' from shell
Quickstart
import subprocess
import os
# Create a dummy file with a known typo
file_content = """\n# This is a comment with a common typo: recievice\ndef foo(arguemnt):\n # Some code\n pass\n"""
with open("temp_code.py", "w") as f:
f.write(file_content)
print("--- Original file content ---")
print(file_content)
# 1. Check for typos (will exit with code 2 if typos found)
print("\n--- Checking for typos ---")
try:
# Using check=True will raise CalledProcessError if typos are found (exit code 2)
result = subprocess.run(['typos', 'temp_code.py'], capture_output=True, text=True, check=False)
print(result.stdout)
if result.returncode == 0:
print("No typos found.")
elif result.returncode == 2:
print("Typos found (exit code 2).")
else:
print(f"Error during check: {result.stderr}")
except FileNotFoundError:
print("Error: 'typos' command not found. Ensure it's installed and in your PATH.")
# 2. Fix typos in place
print("\n--- Fixing typos ---")
try:
fix_result = subprocess.run(['typos', '--write-changes', 'temp_code.py'], capture_output=True, text=True, check=False)
if fix_result.returncode == 0:
print("Typos fixed successfully.")
elif fix_result.returncode == 2:
print("Typos fixed (some might remain if ambiguous).")
else:
print(f"Error during fix: {fix_result.stderr}")
print("\n--- File content after fixing ---")
with open("temp_code.py", "r") as f:
fixed_content = f.read()
print(fixed_content)
finally:
# Clean up the dummy file
if os.path.exists("temp_code.py"):
os.remove("temp_code.py")