Skip to content

Replace try-except-pass with contextlib.suppress#428

Merged
edvilme merged 3 commits intomainfrom
copilot/template-sync-contextlib-suppress
Mar 4, 2026
Merged

Replace try-except-pass with contextlib.suppress#428
edvilme merged 3 commits intomainfrom
copilot/template-sync-contextlib-suppress

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 4, 2026

Sync with upstream template (microsoft/vscode-python-tools-extension-template#204): replace bare try/except: pass blocks with contextlib.suppress(...).

  • bundled/tool/lsp_jsonrpc.py — Add import contextlib; replace 3 bare except/pass blocks in JsonRpc.close() and ProcessManager.stop_all_processes() with contextlib.suppress(Exception)
  • bundled/tool/lsp_utils.py — Replace 2 except SystemExit: pass blocks in _run_module() and _run_api() with contextlib.suppress(SystemExit)
# Before
try:
    self._reader.close()
except:  # noqa: E722
    pass

# After
with contextlib.suppress(Exception):
    self._reader.close()
Original prompt

This section details on the original issue you should resolve

<issue_title>Template Sync: Use contextlib.suppress instead of try-except-pass</issue_title>
<issue_description>### 🔄 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

Comments on the Issue (you are @copilot in this section)


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 2 commits March 4, 2026 20:04
- lsp_jsonrpc.py: Add import contextlib, replace 3 try/except:pass with
  contextlib.suppress(Exception)
- lsp_utils.py: Replace 2 try/except SystemExit:pass with
  contextlib.suppress(SystemExit)

Co-authored-by: edvilme <[email protected]>
Copilot AI changed the title [WIP] Update lsp_jsonrpc.py to use contextlib.suppress Replace try-except-pass with contextlib.suppress Mar 4, 2026
@edvilme edvilme added the debt label Mar 4, 2026
@edvilme edvilme marked this pull request as ready for review March 4, 2026 20:43
@vs-code-engineering vs-code-engineering bot added this to the 1.111.0 milestone Mar 4, 2026
@edvilme edvilme merged commit ac40bac into main Mar 4, 2026
20 of 21 checks passed
@edvilme edvilme deleted the copilot/template-sync-contextlib-suppress branch March 4, 2026 21:17
Copy link
Copy Markdown
Contributor

@rchiodo rchiodo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved via Review Center.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants