Skip to content

Use contextlib.suppress instead of try-except-pass in lsp_utils.py#470

Merged
edvilme merged 3 commits intomainfrom
copilot/update-lsp-utils-with-suppress
Mar 10, 2026
Merged

Use contextlib.suppress instead of try-except-pass in lsp_utils.py#470
edvilme merged 3 commits intomainfrom
copilot/update-lsp-utils-with-suppress

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 10, 2026

Template sync from microsoft/vscode-python-tools-extension-template#204. Replaces bare try/except SystemExit: pass with contextlib.suppress(SystemExit) in _run_api.

  • _run_module intentionally unchanged — it captures exit_code = ex.code
  • contextlib was already imported; no new dependencies
-    try:
+    with contextlib.suppress(SystemExit):
         with substitute_attr(sys, "argv", argv):
             ...
-    except SystemExit:
-        pass
Original prompt

This section details on the original issue you should resolve

<issue_title>Template Sync: Use contextlib.suppress instead of try-except-pass in lsp_utils.py</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 SomeException: pass patterns with the idiomatic contextlib.suppress(SomeException) context manager in bundled/tool/lsp_utils.py. This improves readability and avoids bare-except pylint warnings.

Files with missing changes

  • bundled/tool/lsp_utils.py: The _run_api function still uses the old try/except SystemExit: pass pattern.

    Note: _run_module's except SystemExit as ex: block was intentionally kept as-is since it captures exit_code = ex.code (mypy-specific behavior that cannot be replaced with suppress).

Suggested fix

--- a/bundled/tool/lsp_utils.py
+++ b/bundled/tool/lsp_utils.py
@@ -... @@
 def _run_api(...) -> RunResult:
     str_output = None
     str_error = None

-    try:
+    with contextlib.suppress(SystemExit):
         with substitute_attr(sys, "argv", argv):
             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)
                     str_output, str_error, exit_code = callback(argv, str_input)
             else:
                 str_output, str_error, exit_code = callback(argv, None)
-    except SystemExit:
-        pass

     return RunResult(str_output, str_error, exit_code)

You will also need to ensure import contextlib is present at the top of lsp_utils.py (it is already imported for change_cwd).

Files skipped

  • bundled/tool/lsp_jsonrpc.py: This file does not exist in this repository (the template's lsp_jsonrpc.py changes are not applicable here).

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

@edvilme edvilme added the debt Technical debt or repo cleanup label Mar 10, 2026
@edvilme edvilme marked this pull request as ready for review March 10, 2026 17:41
Copilot AI and others added 2 commits March 10, 2026 17:42
…it) in _run_api

Co-authored-by: edvilme <5952839+edvilme@users.noreply.github.com>
Co-authored-by: edvilme <5952839+edvilme@users.noreply.github.com>
@vs-code-engineering vs-code-engineering bot added this to the 1.112.0 milestone Mar 10, 2026
Copilot AI changed the title [WIP] Update lsp_utils.py to use contextlib.suppress Use contextlib.suppress instead of try-except-pass in lsp_utils.py Mar 10, 2026
@edvilme edvilme enabled auto-merge (squash) March 10, 2026 17:47
@edvilme edvilme merged commit 0a2cafe into main Mar 10, 2026
21 checks passed
@edvilme edvilme deleted the copilot/update-lsp-utils-with-suppress branch March 10, 2026 17:53
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 in lsp_utils.py

3 participants