Skip to content

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

Merged
edvilme merged 3 commits intomainfrom
copilot/update-template-sync-contextlib
Mar 9, 2026
Merged

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

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 9, 2026

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

  • bundled/tool/lsp_jsonrpc.py: Add import contextlib. Replace 3 bare-except blocks in JsonRpc.close() and ProcessManager.stop_all_processes() with contextlib.suppress(Exception).
  • bundled/tool/lsp_utils.py: Replace 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 and try: ... except SystemExit: pass patterns with idiomatic contextlib.suppress(...) context managers. This improves code clarity and eliminates pylint bare-except warnings.

Files with missing changes

  • bundled/tool/lsp_jsonrpc.py: Missing import contextlib. Three bare-except try/except blocks in JsonRpc.close() and ProcessManager.stop_all_processes() still use the old try/except: pass pattern instead of contextlib.suppress(Exception).
  • bundled/tool/lsp_utils.py: Two try: ... except SystemExit: pass blocks in _run_module() and _run_api() have not been updated to with contextlib.suppress(SystemExit):.

Suggested fix

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

 import atexit
+import contextlib
 import io

In JsonRpc.close():

-        try:
-            self._reader.close()
-        except:  # pylint: disable=bare-except
-            pass
-        try:
-            self._writer.close()
-        except:  # pylint: disable=bare-except
-            pass
+        with contextlib.suppress(Exception):
+            self._reader.close()
+        with contextlib.suppress(Exception):
+            self._writer.close()

In ProcessManager.stop_all_processes():

         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():

-    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

Apply this same change to both _run_module() and _run_api().

Files skipped

  • src/test/python_tests/test_change_cwd.py — test file added by the template; this repo has its own test structure and any relevant tests should be added per repo conventions.

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


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

@edvilme edvilme added the debt Code quality issues label Mar 9, 2026
Copilot AI and others added 2 commits March 9, 2026 17:37
Replace bare try/except: pass and try/except SystemExit: pass patterns
with idiomatic contextlib.suppress() context managers, syncing with
upstream vscode-python-tools-extension-template#204.

Co-authored-by: edvilme <5952839+edvilme@users.noreply.github.com>
Co-authored-by: edvilme <5952839+edvilme@users.noreply.github.com>
Copilot AI changed the title [WIP] Update template sync to use contextlib.suppress Use contextlib.suppress instead of try-except-pass Mar 9, 2026
@edvilme edvilme marked this pull request as ready for review March 9, 2026 17:41
@edvilme edvilme enabled auto-merge (squash) March 9, 2026 17:41
@edvilme edvilme merged commit 399c132 into main Mar 9, 2026
19 checks passed
@edvilme edvilme deleted the copilot/update-template-sync-contextlib branch March 9, 2026 17:42
@vs-code-engineering vs-code-engineering bot added this to the 1.112.0 milestone Mar 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

debt Code quality issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants