{"id":4832,"library":"typos","title":"Source Code Spelling Correction","description":"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.","status":"active","version":"1.45.0","language":"en","source_language":"en","source_url":"https://github.com/crate-ci/typos","tags":["spelling","code quality","linter","cli","rust"],"install":[{"cmd":"pip install typos","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"note":"The `typos` Python package is primarily a wrapper for the Rust-based CLI tool. Most common usage is via the command line or `subprocess` calls, rather than direct Python API imports. The core functionality is not exposed as Python classes/functions in the traditional sense for programmatic interaction beyond subprocess calls.","wrong":"from typos import TyposChecker","symbol":"typos_cli","correct":"import subprocess\n# Or directly run 'typos' from shell"}],"quickstart":{"code":"import subprocess\nimport os\n\n# Create a dummy file with a known typo\nfile_content = \"\"\"\\n# This is a comment with a common typo: recievice\\ndef foo(arguemnt):\\n    # Some code\\n    pass\\n\"\"\"\nwith open(\"temp_code.py\", \"w\") as f:\n    f.write(file_content)\n\nprint(\"--- Original file content ---\")\nprint(file_content)\n\n# 1. Check for typos (will exit with code 2 if typos found)\nprint(\"\\n--- Checking for typos ---\")\ntry:\n    # Using check=True will raise CalledProcessError if typos are found (exit code 2)\n    result = subprocess.run(['typos', 'temp_code.py'], capture_output=True, text=True, check=False)\n    print(result.stdout)\n    if result.returncode == 0:\n        print(\"No typos found.\")\n    elif result.returncode == 2:\n        print(\"Typos found (exit code 2).\")\n    else:\n        print(f\"Error during check: {result.stderr}\")\nexcept FileNotFoundError:\n    print(\"Error: 'typos' command not found. Ensure it's installed and in your PATH.\")\n\n# 2. Fix typos in place\nprint(\"\\n--- Fixing typos ---\")\ntry:\n    fix_result = subprocess.run(['typos', '--write-changes', 'temp_code.py'], capture_output=True, text=True, check=False)\n    if fix_result.returncode == 0:\n        print(\"Typos fixed successfully.\")\n    elif fix_result.returncode == 2:\n        print(\"Typos fixed (some might remain if ambiguous).\")\n    else:\n        print(f\"Error during fix: {fix_result.stderr}\")\n\n    print(\"\\n--- File content after fixing ---\")\n    with open(\"temp_code.py\", \"r\") as f:\n        fixed_content = f.read()\n        print(fixed_content)\n\nfinally:\n    # Clean up the dummy file\n    if os.path.exists(\"temp_code.py\"):\n        os.remove(\"temp_code.py\")\n","lang":"python","description":"This quickstart demonstrates how to use `typos` as a command-line tool via Python's `subprocess` module. It creates a temporary file with a known typo, first checks for the typo, and then applies fixes. The `typos` tool exits with a status code of 0 if no typos are found, and 2 if typos are found and/or fixed."},"warnings":[{"fix":"Interact with `typos` using `subprocess.run()` to execute CLI commands (e.g., `typos file.py`, `typos --write-changes file.py`) and parse its `stdout` or `stderr` for results. Alternatively, integrate `typos` directly into CI/CD pipelines or `pre-commit` hooks.","message":"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`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Create and manage a `_typos.toml` or `typos.toml` file at the root of your project to customize `typos` behavior. Refer to the official GitHub repository for detailed configuration options.","message":"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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Understand that `typos` prioritizes low false positives for automated workflows. If you need to catch specific 'valid but unusual' words as typos, you may need to extend its dictionary via `_typos.toml` or use a different spell checker that employs a larger general dictionary approach.","message":"`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.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Handle `subprocess.CalledProcessError` (or set `check=False`) and inspect `result.returncode` explicitly. A `returncode` of 0 means no typos were found, and 2 means typos were detected (and potentially fixed if `--write-changes` was used).","message":"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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}