Skip to content

Template Sync: Use contextlib.suppress instead of try-except-passΒ #423

@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 replaced bare try/except: pass blocks (which silence all exceptions without logging) with contextlib.suppress(...), a more idiomatic and explicit Python pattern. This applies to stream-close operations in lsp_jsonrpc.py and SystemExit handling in lsp_utils.py.

Files with missing changes

bundled/tool/lsp_jsonrpc.py β€” Three locations still use try/except: pass that should be replaced with contextlib.suppress(Exception). Also missing import contextlib at the top.

bundled/tool/lsp_utils.py β€” Two locations in _run_module and _run_api still use try/except SystemExit: pass that should be replaced with contextlib.suppress(SystemExit) (contextlib is already imported here).

Suggested fix

bundled/tool/lsp_jsonrpc.py β€” add import contextlib and update three locations:

--- a/bundled/tool/lsp_jsonrpc.py
+++ b/bundled/tool/lsp_jsonrpc.py
@@ -4,6 +4,7 @@
 
 import atexit
+import contextlib
 import io
 import json
 import pathlib
@@ -99,14 +100,10 @@ def __init__(self, reader: io.TextIOWrapper, writer: io.TextIOWrapper):
     def close(self):
         """Closes the underlying streams."""
-        try:
+        with contextlib.suppress(Exception):
             self._reader.close()
-        except:  # noqa: E722
-            pass
-        try:
+        with contextlib.suppress(Exception):
             self._writer.close()
-        except:  # noqa: E722
-            pass
 
@@ -134,10 +132,8 @@ def stop_all_processes(self):
         """Send exit command to all processes and shutdown transport."""
         for i in self._rpc.values():
-            try:
+            with contextlib.suppress(Exception):
                 i.send_data({"id": str(uuid.uuid4()), "method": "exit"})
-            except:  # noqa: E722
-                pass
         self._thread_pool.shutdown(wait=False)

bundled/tool/lsp_utils.py β€” update _run_module (around line 172) and _run_api (around line 251):

--- a/bundled/tool/lsp_utils.py
+++ b/bundled/tool/lsp_utils.py
@@ -172,13 +172,11 @@ def _run_module(...):
-    try:
+    with contextlib.suppress(SystemExit):
         with substitute_attr(sys, "argv", argv):
             with redirect_io("stdout", str_output):
                 with redirect_io("stderr", str_error):
                     if use_stdin and source is not None:
                         str_input = CustomIO("(stdin)", encoding="utf-8", newline="\n")
                         with redirect_io("stdin", str_input):
                             str_input.write(source)
                             str_input.seek(0)
                             runpy.run_module(module, run_name="__main__")
                     else:
                         runpy.run_module(module, run_name="__main__")
-    except SystemExit:
-        pass

@@ -251,13 +249,11 @@ def _run_api(...):
-    try:
+    with contextlib.suppress(SystemExit):
         with substitute_attr(sys, "argv", argv):
             with redirect_io("stdout", str_output):
                 with redirect_io("stderr", str_error):
                     if use_stdin and source is not None:
                         str_input = CustomIO("(stdin)", encoding="utf-8", newline="\n")
                         with redirect_io("stdin", str_input):
                             str_input.write(source)
                             str_input.seek(0)
                             callback(argv, str_output, str_error, str_input)
                     else:
                         callback(argv, str_output, str_error)
-    except SystemExit:
-        pass

Files skipped

None β€” both files are shared template infrastructure files.


πŸ€– This issue was auto-generated by the extension-template-sync workflow.

Generated by Extension Template Sync

Metadata

Metadata

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