Skip to content

Use contextlib.suppress instead of try-except-pass#732

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

Use contextlib.suppress instead of try-except-pass#732
edvilme merged 3 commits intomainfrom
copilot/template-sync-use-contextlib-suppress

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 5, 2026

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

  • bundled/tool/lsp_jsonrpc.py: Added import contextlib; replaced 3 bare-except blocks in JsonRpc.close() and ProcessManager.stop_all_processes() with contextlib.suppress(Exception)
  • bundled/tool/lsp_utils.py: Replaced 2 try/except SystemExit: pass blocks in _run_module() and _run_api() with contextlib.suppress(SystemExit)
# Before
try:
    self._reader.close()
except:  # pylint: disable=bare-except
    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 with the idiomatic contextlib.suppress(...) context manager. This improves code readability and avoids the need for # pylint: disable=bare-except comments. Two shared Python files are affected: lsp_jsonrpc.py and lsp_utils.py.

Files with missing changes

  • bundled/tool/lsp_jsonrpc.py: Three locations still use try: ... except: pass (bare-except) patterns in JsonRpc.close() and ProcessManager.stop_all_processes().
  • bundled/tool/lsp_utils.py: Two locations still use try: ... except SystemExit: pass in _run_module() and _run_api().

Suggested fix

bundled/tool/lsp_jsonrpc.py — add import contextlib and replace bare-except blocks:

--- 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
@@ -100,14 +101,10 @@ class JsonRpc:
     def close(self):
         """Closes the underlying streams."""
-        try:
+        with contextlib.suppress(Exception):
             self._reader.close()
-        except:  # pylint: disable=bare-except
-            pass
-        try:
+        with contextlib.suppress(Exception):
             self._writer.close()
-        except:  # pylint: disable=bare-except
-            pass
 
@@ -135,10 +132,8 @@ class ProcessManager:
     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:  # pylint: disable=bare-except
-                pass

bundled/tool/lsp_utils.py — replace try/except SystemExit in _run_module() and _run_api():

--- a/bundled/tool/lsp_utils.py
+++ b/bundled/tool/lsp_utils.py
@@ -114,13 +114,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):
                     ...
-    except SystemExit:
-        pass
 
@@ -193,13 +191,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):
                     ...
-    except SystemExit:
-        pass

Note: contextlib is already imported in lsp_utils.py; only lsp_jsonrpc.py needs the new import.


🤖 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)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

- Add `import contextlib` to lsp_jsonrpc.py
- Replace 2 bare-except blocks in JsonRpc.close() with contextlib.suppress(Exception)
- Replace 1 bare-except block in ProcessManager.stop_all_processes() with contextlib.suppress(Exception)
- Replace 2 try/except SystemExit blocks in lsp_utils.py with contextlib.suppress(SystemExit)

Co-authored-by: edvilme <5952839+edvilme@users.noreply.github.com>
Copilot AI changed the title [WIP] Update template to use contextlib.suppress instead of try-except Use contextlib.suppress instead of try-except-pass Mar 5, 2026
@edvilme edvilme marked this pull request as ready for review March 5, 2026 00:21
Copy link
Copy Markdown

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

@vs-code-engineering vs-code-engineering bot added this to the 1.111.0 milestone Mar 5, 2026
@edvilme edvilme added the debt Technical debt or repo cleanup label Mar 5, 2026
@edvilme edvilme enabled auto-merge (squash) March 5, 2026 00:36
@edvilme edvilme merged commit e56295b into main Mar 5, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

debt Technical debt or repo cleanup

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

5 participants