PyCQA / PyCQA/pycodestyle

E722: (bare excepts) too strict (reraise)

Open
#703 17 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.2k
Forks
754
PR merge metrics
No merged PRs in 30d

Description

How I read PEP-8, it doesn't outlaw bare excepts, but merely recommends to catch more specific exceptions when possible. But what if there is no particular exception you want to catch, but when you just want to do some cleanup before propagating any exception?

try:
    self.connection.send(...)
except:
    # We only close the connection on failure, otherwise we keep reusing it.
    self.connection.close()
    raise

Using try...finally isn't an option here, since we want to reuse the resource on success, and only clean up on failure. I could just explicitly catch BaseException instead, but there is no indication in PEP-8 that this is preferable (otherwise why would bare except be supported in the first place).

So how about suppressing E722 if there is a raise statement in the except block?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the E722 entry point in pycodestyle and review how the bare-except example with a re-raise is currently handled. Check the related regression tests, then resolve the requested behavior and add coverage showing when E722 should or should not be reported.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.