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

Strip redundant parentheses from assignment exprs #1906

Merged
merged 3 commits into from Feb 28, 2021

Conversation

xrisk
Copy link
Contributor

@xrisk xrisk commented Jan 5, 2021

#1656

This converts code of the form:

if (foo := 0):
    pass

into

if foo := 0:
    pass

which is consistent with how if (foo): is transformed into if foo: and also with examples in PEP 572.

Although all tests are passing, please review particularly this condition:
and parent.children[1].type == token.EQUAL

I am unsure if children[1] is always guaranteed to be the operator.

@@ -5481,6 +5478,9 @@ def maybe_make_parens_invisible_in_atom(node: LN, parent: LN) -> bool:
or is_one_tuple(node)
or (is_yield(node) and parent.type != syms.expr_stmt)
or max_delimiter_priority_in_atom(node) >= COMMA_PRIORITY
or parent.type == syms.expr_stmt
and parent.children[1].type == token.EQUAL
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not fully sure about this condition

@xrisk
Copy link
Contributor Author

xrisk commented Jan 5, 2021

Seems to break on pre-3.8. Will fix.

EDIT: I’m not sure what these fuzzer errors are.

@xrisk
Copy link
Contributor Author

xrisk commented Jan 5, 2021

Alright fuzzer failures seem to be unrelated and they’re failing on HEAD as well.

@xrisk
Copy link
Contributor Author

xrisk commented Jan 9, 2021

@JelleZijlstra requesting a review!

@ichard26
Copy link
Collaborator

Reopening to re-run CI as the fuzz issues should be fixed.

@ichard26 ichard26 closed this Jan 18, 2021
@ichard26 ichard26 reopened this Jan 18, 2021
@ichard26
Copy link
Collaborator

Okay we're hitting #1913 by chance now...

@xrisk
Copy link
Contributor Author

xrisk commented Jan 18, 2021

Hm the code I've touched seems unrelated to #1913. Do we wait for that to get resolved first?

@ichard26
Copy link
Collaborator

ichard26 commented Jan 18, 2021

Sorry, should've made it clearer that the new unrelated fuzz error was something we've seen before. TR;DR: CI passed for your PR 🎉

@xrisk
Copy link
Contributor Author

xrisk commented Jan 27, 2021

Hi, please let me know if I need to do anything else here.

Copy link
Collaborator

@ichard26 ichard26 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this! This is my first review of a formatting PR (I handle docs usually) so I hope I'm doing this right and carefully :) This should also be reviewed by Jelle since I believe they are our most qualified reviewer for this sort of stuff. But on with my review first!

Ideally this would have more test cases (complex if and while statements would be nice!), but it isn't a blocker.

I am unsure if children[1] is always guaranteed to be the operator.

The answer is no :(

Here's a few examples of where parent.children[1].type == token.EQUAL falls apart:

  1. test += (a := 1) (who would write this tho?)
  2. a, b = (test := (1, 2))
  3. test: int = (test2 := 2)

edit: don't ask me how to workaround this problem, I have no idea

Also, you'll need to edit this section adding a note how Black will not remove parentheses where an assignment expression is used since it's invalid.

@xrisk
Copy link
Contributor Author

xrisk commented Feb 23, 2021

Alright, I think it should be correct now. But perhaps we should wait for the refactoring that’s going on in #1958 to finish.

@jayaddison Are you planning to touch this code?

@jayaddison
Copy link
Contributor

@xrisk Thanks for checking; I'm not yet fully confident about the changes in #1958, so please don't consider it a blocker.

@ichard26 ichard26 self-requested a review February 24, 2021 01:48
Copy link
Collaborator

@ichard26 ichard26 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, you'll need to edit this section adding a note how Black will not remove parentheses where an assignment expression is used since it's invalid.

This was never resolved, please do so.

But overall, great improvement, thanks for your time and effort! If you please, add yourself to the AUTHORS list in the readme.

@ichard26
Copy link
Collaborator

ichard26 commented Feb 27, 2021

This is my first review of a formatting PR (I handle docs usually) so I hope I'm doing this right and carefully :) This should also be reviewed by Jelle since I believe they are our most qualified reviewer for this sort of stuff. But on with my review first

@JelleZijlstra, I would appreciate if you could make sure I haven't missed anything vital! This PR should be mostly ready, just a few documentation issues (of which I can handle and review).

Copy link
Collaborator

@JelleZijlstra JelleZijlstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test cases look good and the change in black itself seems reasonable.

By the way, I'm the main person reviewing formatting changes in Black these days, but even I don't exactly feel like I know the codebase well.

@JelleZijlstra JelleZijlstra merged commit 858225d into psf:master Feb 28, 2021
@JelleZijlstra
Copy link
Collaborator

Oops, I merged without resolving @ichard26's request about the documentation, sorry for that! @xrisk would you be able to send a followup PR? If not I can do it myself.

@ichard26
Copy link
Collaborator

By the way, I'm the main person reviewing formatting changes in Black these days, but even I don't exactly feel like I know the codebase well.

You've got some experience at least compared to me :)

Oops, I merged without resolving @ichard26's request about the documentation, sorry for that! @xrisk would you be able to send a followup PR? If not I can do it myself.

I was about to comment about that, don't fret too much as now I do see how you might have skipped over it since it was poorly worded. I should really reread stuff I write before pressing "comment". Ugh, old habits die hard.

@JelleZijlstra
Copy link
Collaborator

@ichard26 don't blame yourself, it's my bad! You clearly requested changes, so I should have noticed.

@xrisk
Copy link
Contributor Author

xrisk commented Mar 1, 2021

@ichard26

Also, you'll need to edit this section adding a note how Black will not remove parentheses where an assignment expression is used since it's invalid.

Isn’t this implicit, since that section in the docs talks about “optional parentheses”? The parentheses as used in something like x = (y := 5) aren’t optional.

Also I would expect that behavior from the principle of least surprise, we should never be generating invalid code in the first place :P

@ichard26
Copy link
Collaborator

ichard26 commented Mar 1, 2021

@xrisk yeah you're right lol. Apologies for the noise. Thanks for the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants