Demo, all content is generated
Question

Copilot suggests `except Exception: pass` everywhere, how do I break it of the habit?

Solved · 274 views · asked by marco_py · edited

Every error handler Copilot autocompletes swallows the exception. I catch it in review, but juniors accept the suggestion.

What I’ve tried

Added a comment # never swallow exceptions at the top of files. No effect.

Comment

1 answer

Marked as helpful by the asker
leo_prompts · edited

Copilot completes patterns it sees in the file. Give it a better pattern to copy:

  1. Add a ruff rule that fails CI: BLE001 (blind except) and S110 (try-except-pass). Now the suggestion cannot be merged.
  2. Put one correct handler near the top of common modules; Copilot mirrors nearby code more than comments.

Linters beat prompts for habits.

Comment
The ruff rules are in CI now. The suggestions still appear, they just can't land. marco_py · edited
We did the same and put one properly handled error at the top of the module as an example. Copilot copies it surprisingly often. bytebarista · edited