Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions .github/instructions/python-quality-checks.instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
applyTo: 'python_files/**'
description: Guide for running and fixing Python quality checks (Ruff and Pyright) that run in CI
---

# Python Quality Checks — Ruff and Pyright

Run the same Python quality checks that run in CI. All checks target `python_files/` and use config from `python_files/pyproject.toml`.

## Commands

```bash
npm run check-python # Run both Ruff and Pyright
npm run check-python:ruff # Linting and formatting only
npm run check-python:pyright # Type checking only
```

## Fixing Ruff Errors

**Auto-fix most issues:**

```bash
cd python_files
python -m ruff check . --fix
python -m ruff format
npm run check-python:ruff # Verify
```

**Manual fixes:**

- Ruff shows file, line number, rule code (e.g., `F841`), and description
- Open the file, read the error, fix the code
- Common: line length (100 char max), import sorting, unused variables

## Fixing Pyright Errors

**Common patterns and fixes:**

- **Undefined variable/import**: Add the missing import
- **Type mismatch**: Correct the type or add type annotations
- **Missing return type**: Add `-> ReturnType` to function signatures
```python
def my_function() -> str: # Add return type
return "result"
```

**Verify:**

```bash
npm run check-python:pyright
```

## Configuration

- **Ruff**: Line length 100, Python 3.9+, 40+ rule families (flake8, isort, pyupgrade, etc.)
- **Pyright**: Version 1.1.308 (or whatever is found in the environment), ignores `lib/` and 15+ legacy files
- Config: `python_files/pyproject.toml` sections `[tool.ruff]` and `[tool.pyright]`

## Troubleshooting

**"Module not found" in Pyright**: Install dependencies

```bash
python -m pip install --upgrade -r build/test-requirements.txt
nox --session install_python_libs
```

**Import order errors**: Auto-fix with `ruff check . --fix`

**Type errors in ignored files**: Legacy files in `pyproject.toml` ignore list—fix if working on them

## Learnings

- Always run `npm run check-python` before pushing to catch CI failures early (1)
- Use `ruff check . --fix` to auto-fix most linting issues before manual review (1)
- Pyright version must match CI (1.1.308) to avoid inconsistent results between local and CI runs (1)
11 changes: 8 additions & 3 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
"type": "npm",
"script": "compile",
"isBackground": true,
"problemMatcher": [
"$tsc-watch"
],
"problemMatcher": ["$tsc-watch"],
"group": {
"kind": "build",
"isDefault": true
Expand All @@ -34,6 +32,13 @@
"script": "preTestJediLSP",
"problemMatcher": [],
"label": "preTestJediLSP"
},
{
"type": "npm",
"script": "check-python",
"problemMatcher": ["$python"],
"label": "npm: check-python",
"detail": "npm run check-python:ruff && npm run check-python:pyright"
}
]
}
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1695,6 +1695,9 @@
"lint-fix": "eslint --fix src build pythonExtensionApi gulpfile.js",
"format-check": "prettier --check 'src/**/*.ts' 'build/**/*.js' '.github/**/*.yml' gulpfile.js",
"format-fix": "prettier --write 'src/**/*.ts' 'build/**/*.js' '.github/**/*.yml' gulpfile.js",
"check-python": "npm run check-python:ruff && npm run check-python:pyright",
"check-python:ruff": "cd python_files && python -m pip install -U ruff && python -m ruff check . && python -m ruff format --check",
"check-python:pyright": "cd python_files && npx --yes [email protected] .",
"clean": "gulp clean",
"addExtensionPackDependencies": "gulp addExtensionPackDependencies",
"updateBuildNumber": "gulp updateBuildNumber",
Expand Down
Loading