Skip to content

Template Sync: fix: 'result' referenced before assignmentΒ #438

@github-actions

Description

@github-actions

πŸ”„ Template Sync Required

Changes from the upstream vscode-python-tools-extension-template have not yet been incorporated into this repository.

Source PR

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 aNameError: name 'result' is not definedwhenever the JSON-RPC response contains no"error"key (i.e., on success). The fix moves theresult` 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:

    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:

--- 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 workflow.

Generated by Extension Template Sync Β· β—·

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions