Skip to content

Implement useless try-except detection (TRY302) #4308

@dorschw

Description

@dorschw

Bad code:

# case 1
try:
    do_thing()
except Exception:
    raise
# case 2
try:
    do_thing()
except:
    raise
# case 3
try:
    do_thing()
except SomeKindOfException:
    raise

Fix:
remove the try/except, as it has no effect.

do_thing()

In cases 1&2, all exceptions are raised immediately, and in case 3 only a subset of exceptions is caught, then reraised immediately.

Notes:

  • This shouldn't run if there's any other code in the except block.
  • If there are multiple excpt cases, empty ones (with only raise) should be removed, and others should remain.

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueGood for newcomersruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions