Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove redundant brackets from except clause #2937

Closed
jpy-git opened this issue Mar 17, 2022 · 2 comments · Fixed by #2939
Closed

Remove redundant brackets from except clause #2937

jpy-git opened this issue Mar 17, 2022 · 2 comments · Fixed by #2939
Labels
F: parentheses Too many parentheses, not enough parentheses, and so on. S: accepted The changes in this design / enhancement issue have been accepted and can be implemented T: enhancement New feature or request

Comments

@jpy-git
Copy link
Contributor

jpy-git commented Mar 17, 2022

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
@jpy-git jpy-git added the T: enhancement New feature or request label Mar 17, 2022
@JelleZijlstra JelleZijlstra added the F: parentheses Too many parentheses, not enough parentheses, and so on. label Mar 17, 2022
@JelleZijlstra
Copy link
Collaborator

Agree, this goes with a lot of other cases where we should remove redundant parens.

Not sure about the tuple case; that would also change the AST.

@jpy-git
Copy link
Contributor Author

jpy-git commented Mar 18, 2022

Not sure about the tuple case; that would also change the AST.

Good point! let's just fix the first case then 👍

@ichard26 ichard26 added the S: accepted The changes in this design / enhancement issue have been accepted and can be implemented label Mar 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: parentheses Too many parentheses, not enough parentheses, and so on. S: accepted The changes in this design / enhancement issue have been accepted and can be implemented T: enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants