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

Use parentheses with equality check in walrus/assignment statements #2770

Conversation

Shivansh-007
Copy link
Contributor

Closes #449

Checklist - did you ...

  • Add a CHANGELOG entry if necessary?
  • Add / update tests if necessary?
  • Add new / update outdated documentation?

@JelleZijlstra
Copy link
Collaborator

Looking at the results of this change, I'm not sure it's really worth it. The diff shows that this is a pretty common pattern, so this would be a relatively disruptive change. To me, most of the changes don't seem like clear improvements, so I'd default to sticking to Black's existing style. (Relatedly, the original issue got more downvote reactions than upvotes.)

The parentheses seem especially unnecessary in cases where the left operand of the == isn't a valid assignment target, like this one in pandas:

-            is_last_column = len(columns) - 1 == i
+            is_last_column = (len(columns) - 1 == i)

(diff-shades commenting currently doesn't work yet, but you can manually download the artifacts from https://github.com/psf/black/actions/runs/1697124259.)

@Shivansh-007
Copy link
Contributor Author

Shivansh-007 commented Jan 16, 2022

Sorry, I was looking to edit my previous comment, but misclicked delete instead.

I agree with you but in cases where the expression is in form of a = b == c where b and c can only be a single leaf, like system.platform() and not len(columns) - 1. This is pretty helpful:

-        left_ea = blk_vals.ndim == 1
+        left_ea = (blk_vals.ndim == 1)

-        result = arr == other
+        result = (arr == other)

-        actual = self.index.values == self.index
+        actual = (self.index.values == self.index)

But still, I am not sure about cases like, where they are in the above format but are looking better without the parentheses:

-        result = Series(a1, dtype=cat_type) == Series(a2, dtype=cat_type)
+        result = (Series(a1, dtype=cat_type) == Series(a2, dtype=cat_type))

-                mask &= (self._ascending_count - start) % step == 0
+                mask &= ((self._ascending_count - start) % step == 0)

-    debug = env.get(str("_VIRTUALENV_PERIODIC_UPDATE_INLINE")) == str("1")
+    debug = (env.get(str("_VIRTUALENV_PERIODIC_UPDATE_INLINE")) == str("1"))

I think this can still be discussed a bit more on the issue with the original commenters.

@JelleZijlstra
Copy link
Collaborator

To be clear my current preference is just to never add these parentheses, it's just that the parens are more jarring in cases where the LHS of the == is complex. I'm OK with landing a version of this change if there's consensus for it though.

@felix-hilden
Copy link
Collaborator

I tend to always add the parens, but this isn't the hill I'm willing to die on. A good middle ground could be using a definition of "complex" like in our power hugging endeavour. It's not so easy to determine if something can be used as an assignment target, but a combination of name-attribute-subscript is a good approximation. foo().bar = 3 is just asking for trouble 😄

@Shivansh-007 Shivansh-007 force-pushed the equality-check-assignment-parentheses branch from 6984931 to 32e5544 Compare January 20, 2022 03:13
@github-actions
Copy link

github-actions bot commented Jan 20, 2022

diff-shades results comparing this PR (ce9e47d) to main (71e71e5). The full diff is available in the logs under the "Generate HTML diff report" step.

╭─────────────────────── Summary ────────────────────────╮
│ 2 projects & 8 files changed / 20 changes [+10/-10]    │
│                                                        │
│ ... out of 2 162 297 lines, 10 458 files & 23 projects │
╰────────────────────────────────────────────────────────╯

Differences found.

What is this? | Workflow run | diff-shades documentation

@JelleZijlstra
Copy link
Collaborator

This will now need to go in the preview style only. (And my personal preference is still to just not change anything here, but the preview style does allow us to change our mind more easily.)

@Shivansh-007 Shivansh-007 force-pushed the equality-check-assignment-parentheses branch from 434d8bf to 0aee688 Compare March 11, 2022 15:07
@ichard26 ichard26 added F: parentheses Too many parentheses, not enough parentheses, and so on. S: up for grabs (PR only) Available for anyone to work on as the PR author is busy or unreachable. S: blocked A decision or another issue needs to be made/resolved before this can be worked on (more). labels Aug 3, 2022
@JelleZijlstra
Copy link
Collaborator

Why is this blocked? There are a lot of merge conflicts now. I vote that we close this and leave the style as is.

@ichard26
Copy link
Collaborator

I don't remember well. I think I marked it as blocked because it wasn't even clear if the style change it was implementing was accepted (the PR is blocked until the style decision is made). Made sense in my head when I added the label, but I agree it's confusing in hindsight.

@Shivansh-007 Shivansh-007 deleted the equality-check-assignment-parentheses branch April 2, 2023 09:47
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: blocked A decision or another issue needs to be made/resolved before this can be worked on (more). S: up for grabs (PR only) Available for anyone to work on as the PR author is busy or unreachable.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use parentheses in: a = b == c
4 participants