microsoft / microsoft/vscode-flake8
Template Sync: fix: 'result' referenced before assignment
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 52
- Forks
- 34
- Avg merge
- 1d 1h
- Merged PRs (30d)
- 5
Description
### 🔄 Template Sync Required
Changes from the upstream [vscode-python-tools-extension-template](https://github.com/microsoft/vscode-python-tools-extension-template) have not yet been incorporated into this repository.
#### Source PR
- microsoft/vscode-python-tools-extension-template#159 — fix: 'result' referenced before assignment
#### Summary
The template fixed a bug in `bundled/tool/lsp_jsonrpc.py` where the `result` variable was assigned inside the `if "error" in (redacted) block but then referenced unconditionally in the `return RpcRunResult(result, "")` statement after that block. This causes a `NameError: name 'result' is not defined` whenever the JSON-RPC response contains no `"error"` key (i.e., on success). The fix moves the `result` assignment to before the error check so it is always initialized.
#### Files with missing changes
- **`bundled/tool/lsp_jsonrpc.py`**: The `result = data["result"] if "result" in data else ""` assignment is still indented inside the `if "error" in (redacted) block, but is used outside it. This will raise `NameError` on any successful RPC response.
Current (buggy) code in `run_over_json_rpc`:
```python
if "error" in data:
result = data["result"] if "result" in data else "" # ← only assigned when error present
error = data["error"]
if data.get("exception", False):
return RpcRunResult(result, "", error)
return RpcRunResult(result, error)
return RpcRunResult(result, "") # ← NameError if no "error" in data
```
#### Suggested fix
Move the `result` assignment before the `if "error" in (redacted) block:
````diff
--- a/bundled/tool/lsp_jsonrpc.py
+++ b/bundled/tool/lsp_jsonrpc.py
@@ -237,9 +237,9 @@ def run_over_json_rpc(
if data["id"] != msg_id:
return RpcRunResult(
"", f"Invalid result for request: {json.dumps(msg, indent=4)}"
)
+ result = data["result"] if "result" in data else ""
if "error" in data:
- result = data["result"] if "result" in data else ""
error = data["error"]
if data.get("exception", False):
````
#### Files skipped
None — all changes affect shared template infrastructure.
---
🤖 This issue was auto-generated by the [`extension-template-sync`](.github/workflows/extension-template-sync.yml) workflow.
> Generated by [Extension Template Sync](https://github.com/microsoft/vscode-flake8/actions/runs/22929150192) · [◷](https://github.com/search?q=repo%3Amicrosoft%2Fvscode-flake8+is%3Aissue+%22gh-aw-workflow-call-id%3A+microsoft%2Fvscode-flake8%2FExtension+Template+Sync%22&type=issues)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in bundled/tool/lsp_jsonrpc.py, at the run_over_json_rpc function and the result handling around the response error check. Verify that successful JSON-RPC responses no longer reference an uninitialized result, and confirm the existing error and exception paths remain unchanged.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- devtools
- Issue type
- Bug
- Difficulty
- 1/5
- Estimated time
- Under an hour
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 65/100