Skip to content

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

@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 except: pass and except SystemExit: pass patterns with the idiomatic contextlib.suppress(...) context manager. This improves code clarity and eliminates pylint bare-except warnings.

Files with missing changes

  • bundled/tool/lsp_jsonrpc.py: Three bare-except blocks (except: # pylint: disable=bare-except followed by pass) in the close() and stop_all_processes() methods have not been replaced with contextlib.suppress(Exception). The import contextlib statement is also missing.
  • bundled/tool/lsp_utils.py: Two try...except SystemExit: pass blocks in _run_module() (line ~161) and _run_api() (line ~240) have not been replaced with with contextlib.suppress(SystemExit):.

Suggested fix

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

 import atexit
+import contextlib
 import io
     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
     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
         self._thread_pool.shutdown(wait=False)

bundled/tool/lsp_utils.py β€” replace both except SystemExit: pass blocks (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):
                     if use_stdin and source is not None:
                         ...
                     else:
                         runpy.run_module(module, run_name="__main__")
-    except SystemExit:
-        pass

Apply the same pattern for the _run_api function.

Files skipped

  • src/test/python_tests/ β€” test files are tool-specific and don't need to be synced directly.

πŸ€– 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