Black: Should Black remove optional parentheses that wrap assignment expressions?

Created on 30 Aug 2020  Â·  1Comment  Â·  Source: psf/black

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.

  2. 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.

design parentheses

Most helpful comment

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:.

>All comments

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:.

Was this page helpful?
0 / 5 - 0 ratings