Skip to content

Commit f6157d3

Browse files
committed
fixes and add a instruction
1 parent c1d50ad commit f6157d3

File tree

4 files changed

+90
-16
lines changed

4 files changed

+90
-16
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.8+, 40+ rule families (flake8, isort, pyupgrade, etc.)
56+
- **Pyright**: Version 1.1.308 (pinned), 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)

.vscode/tasks.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@
1212
"type": "npm",
1313
"script": "compile",
1414
"isBackground": true,
15-
"problemMatcher": [
16-
"$tsc-watch"
17-
],
15+
"problemMatcher": ["$tsc-watch"],
1816
"group": {
1917
"kind": "build",
2018
"isDefault": true
@@ -34,6 +32,13 @@
3432
"script": "preTestJediLSP",
3533
"problemMatcher": [],
3634
"label": "preTestJediLSP"
35+
},
36+
{
37+
"type": "npm",
38+
"script": "check-python",
39+
"problemMatcher": ["$python"],
40+
"label": "npm: check-python",
41+
"detail": "npm run check-python:ruff && npm run check-python:pyright"
3742
}
3843
]
3944
}

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1695,6 +1695,9 @@
16951695
"lint-fix": "eslint --fix src build pythonExtensionApi gulpfile.js",
16961696
"format-check": "prettier --check 'src/**/*.ts' 'build/**/*.js' '.github/**/*.yml' gulpfile.js",
16971697
"format-fix": "prettier --write 'src/**/*.ts' 'build/**/*.js' '.github/**/*.yml' gulpfile.js",
1698+
"check-python": "npm run check-python:ruff && npm run check-python:pyright",
1699+
"check-python:ruff": "cd python_files && python -m pip install -U ruff && python -m ruff check . && python -m ruff format --check",
1700+
"check-python:pyright": "cd python_files && npx --yes [email protected] .",
16981701
"clean": "gulp clean",
16991702
"addExtensionPackDependencies": "gulp addExtensionPackDependencies",
17001703
"updateBuildNumber": "gulp updateBuildNumber",

python_files/vscode_pytest/__init__.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -560,9 +560,7 @@ def construct_nested_folders(
560560
# IMPORTANT: Use session_node["path"] directly as it's already a pathlib.Path object
561561
# Do NOT use get_node_path(session_node["path"]) as get_node_path expects pytest objects,
562562
# not Path objects directly.
563-
common_parent = os.path.commonpath(
564-
[file_node["path"], session_node["path"]]
565-
)
563+
common_parent = os.path.commonpath([file_node["path"], session_node["path"]])
566564
common_parent_path = pathlib.Path(common_parent)
567565
print("[vscode-pytest]: Session node now set to: ", common_parent)
568566
session_node["path"] = common_parent_path # pathlib.Path
@@ -719,12 +717,7 @@ def build_test_tree(session: pytest.Session) -> TestNode:
719717
continue
720718
parent_path = get_node_path(
721719
cast(
722-
pytest.Session
723-
| pytest.Item
724-
| pytest.File
725-
| pytest.Class
726-
| pytest.Module
727-
| HasPathOrFspath,
720+
"pytest.Session | pytest.Item | pytest.File | pytest.Class | pytest.Module | HasPathOrFspath",
728721
test_case.parent,
729722
)
730723
)
@@ -948,10 +941,7 @@ def get_node_path(
948941
node_path = getattr(node, "path", None)
949942
if node_path is None:
950943
fspath = getattr(node, "fspath", None)
951-
if fspath is not None:
952-
node_path = pathlib.Path(fspath)
953-
else:
954-
node_path = None
944+
node_path = pathlib.Path(fspath) if fspath is not None else None
955945

956946
if not node_path:
957947
raise VSCodePytestError(

0 commit comments

Comments
 (0)