Skip to content

Remove redundant brackets from except clause #2937

@jpy-git

Description

@jpy-git

Is your feature request related to a problem? Please describe.

The except clause can accept a parenthesised tuple of exceptions. However, in the case of only a single exception the brackets should be removed.

Black currently leaves both of these examples unchanged:

a = 1
try:
    a.something
except (AttributeError) as err:
    raise err

and

a = 1
try:
    a.something
except (AttributeError,) as err:
    raise err

Describe the solution you'd like

I would expect both examples to be corrected to:

a = 1
try:
    a.something
except AttributeError as err:
    raise err

Describe alternatives you've considered

I could see an argument to leave the tuple example (with the trailing comma) as is, maybe the user wants to add more exceptions in the future. Easy either way on that one, but we should at least fix the first (non-tuple) example.

Additional context

I actually came across this when someone assumed the except was a function in a PR. Black added the space before the brackets, but also removing the brackets would really help highlight that it's a keyword not a function 😄

try:
    a.something
except(AttributeError) as err:
    raise err

Metadata

Metadata

Assignees

No one assigned

    Labels

    F: parenthesesToo many parentheses, not enough parentheses, and so on.S: acceptedThe changes in this design / enhancement issue have been accepted and can be implementedT: enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions