|
| 1 | +--- |
| 2 | +applyTo: 'python_files/**' |
| 3 | +description: Guide for running and fixing Python quality checks (Ruff and Pyright) that run in CI |
| 4 | +--- |
| 5 | + |
| 6 | +# Python Quality Checks — Ruff and Pyright |
| 7 | + |
| 8 | +Run the same Python quality checks that run in CI. All checks target `python_files/` and use config from `python_files/pyproject.toml`. |
| 9 | + |
| 10 | +## Commands |
| 11 | + |
| 12 | +```bash |
| 13 | +npm run check-python # Run both Ruff and Pyright |
| 14 | +npm run check-python:ruff # Linting and formatting only |
| 15 | +npm run check-python:pyright # Type checking only |
| 16 | +``` |
| 17 | + |
| 18 | +## Fixing Ruff Errors |
| 19 | + |
| 20 | +**Auto-fix most issues:** |
| 21 | + |
| 22 | +```bash |
| 23 | +cd python_files |
| 24 | +python -m ruff check . --fix |
| 25 | +python -m ruff format |
| 26 | +npm run check-python:ruff # Verify |
| 27 | +``` |
| 28 | + |
| 29 | +**Manual fixes:** |
| 30 | + |
| 31 | +- Ruff shows file, line number, rule code (e.g., `F841`), and description |
| 32 | +- Open the file, read the error, fix the code |
| 33 | +- Common: line length (100 char max), import sorting, unused variables |
| 34 | + |
| 35 | +## Fixing Pyright Errors |
| 36 | + |
| 37 | +**Common patterns and fixes:** |
| 38 | + |
| 39 | +- **Undefined variable/import**: Add the missing import |
| 40 | +- **Type mismatch**: Correct the type or add type annotations |
| 41 | +- **Missing return type**: Add `-> ReturnType` to function signatures |
| 42 | + ```python |
| 43 | + def my_function() -> str: # Add return type |
| 44 | + return "result" |
| 45 | + ``` |
| 46 | + |
| 47 | +**Verify:** |
| 48 | + |
| 49 | +```bash |
| 50 | +npm run check-python:pyright |
| 51 | +``` |
| 52 | + |
| 53 | +## Configuration |
| 54 | + |
| 55 | +- **Ruff**: Line length 100, Python 3.9+, 40+ rule families (flake8, isort, pyupgrade, etc.) |
| 56 | +- **Pyright**: Version 1.1.308 (or whatever is found in the environment), ignores `lib/` and 15+ legacy files |
| 57 | +- Config: `python_files/pyproject.toml` sections `[tool.ruff]` and `[tool.pyright]` |
| 58 | + |
| 59 | +## Troubleshooting |
| 60 | + |
| 61 | +**"Module not found" in Pyright**: Install dependencies |
| 62 | + |
| 63 | +```bash |
| 64 | +python -m pip install --upgrade -r build/test-requirements.txt |
| 65 | +nox --session install_python_libs |
| 66 | +``` |
| 67 | + |
| 68 | +**Import order errors**: Auto-fix with `ruff check . --fix` |
| 69 | + |
| 70 | +**Type errors in ignored files**: Legacy files in `pyproject.toml` ignore list—fix if working on them |
| 71 | + |
| 72 | +## Learnings |
| 73 | + |
| 74 | +- Always run `npm run check-python` before pushing to catch CI failures early (1) |
| 75 | +- Use `ruff check . --fix` to auto-fix most linting issues before manual review (1) |
| 76 | +- Pyright version must match CI (1.1.308) to avoid inconsistent results between local and CI runs (1) |
0 commit comments