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

Should Black remove optional parentheses that wrap assignment expressions? #1656

Closed
ichard26 opened this issue Aug 29, 2020 · 3 comments
Closed
Labels
F: parentheses Too many parentheses, not enough parentheses, and so on. T: style What do we want Blackened code to look like?

Comments

@ichard26
Copy link
Collaborator

Describe the style change

Black would now remove optional parentheses that surround assignment expressions. Also Black will now have to take into account of situations where such removal would be unsafe (e.g. top level unparenthesized assignment expressions are illegal).

Examples in the current style

while (temp := True):
    pass

if (temp := True):
    pass

Examples in the new style:

while temp := True:
    pass

if temp := True:
    pass

Things to discuss:

  • What are the conditions where Black would remove/keep the wrapping parentheses? Perhaps we can use this specification as a starting point?

  • Do we want this in the first place?

Additional context:

This was originally filed in #1522 by @xrisk. I closed it as a duplicate of another issue and broke out the other topic in it as this new issue.

Related comments in original issue:

  1. JelleZijlstra

I'm not as sure we should remove the parentheses, maybe it's better for clarity to have them? Black is opinionated, but we should make sure we're opinionated in the right way.

  1. xrisk (author of original issue)

[snipped mention] black does convert if (expr): to if expr: so…

And there are multiple examples in the PEP that omit the parentheses when there is no RHS.

@ichard26 ichard26 added the T: style What do we want Blackened code to look like? label Aug 29, 2020
@ichard26 ichard26 added the F: parentheses Too many parentheses, not enough parentheses, and so on. label Aug 29, 2020
@JelleZijlstra
Copy link
Collaborator

I vote we remove the parentheses if the assignment expr is the whole body of an if/while condition. As someone mentioned on the other issue, that's consistent with Black generally turning if (expr): into if expr:.

@xrisk
Copy link
Contributor

xrisk commented Jan 4, 2021

Is there consensus regarding this? If so, I would like to try my hand at attempting a fix :)

@JelleZijlstra
Copy link
Collaborator

Other maintainers can chime in, but so far nobody has disagreed, so I say go for it.

xrisk added a commit to xrisk/black that referenced this issue Jan 5, 2021
xrisk added a commit to xrisk/black that referenced this issue Feb 23, 2021
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. T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

3 participants