---
name: pre-commit-check
description: >-
  Run pre-commit code quality checks (ruff lint/format, mypy type check, deptry dependency audit) 
  and fix any issues before committing. Use when the user wants to commit code, asks to check code 
  quality, or mentions pre-commit validation. Also use when preparing code for submission or 
  when the user says "check before commit" or "run checks".
---

# Pre-Commit Code Quality Check

## Workflow

Run all checks in parallel first, then fix any issues iteratively:

```
Step 1: Run checks (parallel)
  - uv run ruff check .
  - uv run ruff format --check .
  - uv run mypy src/
  - uv run deptry .

Step 2: If any check fails, fix issues
  - ruff check --fix . (auto-fix lint issues)
  - ruff format . (auto-format)
  - Fix type annotation errors manually

Step 3: Re-run all checks to confirm
Step 4: Report results with table
```

## Common Fixes

| Error | Fix |
|-------|-----|
| `UP038` | Replace `isinstance(e, (X, Y))` with `isinstance(e, X \| Y)` |
| `no-untyped-def` | Add return type annotation (`-> None` for void functions) |
| `I001` | Run `uv run ruff check --fix .` |
| `no-any-return` | Use `cast(Type, expression)` from `typing` |
| `type-arg` missing | Add type arguments: `dict[str, Any]` instead of `dict` |
| `unused-ignore` | Remove stale `# type: ignore` comments |

## Type Annotation Rules

- Functions that modify data in-place and return nothing: `-> None`
- Functions that call untyped external APIs: add `-> Any` return type
- Dicts that hold mixed types (str + float): use `dict[str, Any]`
- Import `Any` from `typing` when needed
- Import `cast` from `typing` when `no-any-return` triggers

## Verification Report

After all checks pass, report:

| 检查项 | 状态 |
|--------|------|
| `ruff check .` | ✅ |
| `ruff format --check .` | ✅ |
| `mypy src/` | ✅ |
| `deptry .` | ✅ |

## Notes

- All commands use `uv run` prefix (project virtual environment)
- `mypy` has `strict = true` in `pyproject.toml` - expect strict type checking
- `ignore_missing_imports = true` is set, so missing third-party stubs are OK
- If `tests.*` override shows "unused section" note, it's normal (no tests dir yet)
- Never skip a check - all four must pass before committing