-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Unnecessary parentheses added when unpacking single item tuples #1108
Comments
This is intentional, from #832. |
That is specifically trying to address tuple unpacking that doesn't fit on a single line, and doesn't explain why black prefers |
FWIW I actually like the new behavior, as it's much harder to miss that tuple unpacking when the left hand side is wrapped in parentheses (the same way as they are explicit in one-tuples on the right hand side) |
As Zsolt said, this is compatible with single-tuple behavior on the right-hand side. It's an intentional change. Multi-element tuples are obvious for the reader to see so we are not unnecessarily putting parentheses around them unless that helps with multi-line breaks. I'll leave this open since other users will likely come to ask about this change, too. |
@ambv can you update the docs on this? |
Ah to be fair the docs are here
|
@graingert, you're right that we could put this more front and center. |
Here via HypothesisWorks/hypothesis#2157, I expected this to be treated as a bug - mostly because it doesn't appear in the changelog! No actual objection, but documentation would be good 😅 |
For a single element unpack black now also encloses with parentheses to make it clearer: psf/black#1108 Fix flake8 warnings
after upgrading to 19.10b0 this change was applied
this does not feel as elegant as it did before ... |
I reverted to 19.3b0 because it actually made the code a lot worse by adding noisy parenthesis and caused lines to go from one to four, which was unacceptably noisy for a codebase that uses tuple unpacking as a fundamental idiom. |
An even simpler example of how black has nitpicked a simple tuple unpacking to the point of absurdity:
became
then became
and now becomes:
A simple, one-line unpacking, something that's meant to take a pair and unpack it into two variables, now consumes four lines, several unnecessary characters, and is much less readable and less elegant. I understand the motivations of trying to have consistent formatting across any number of tuple unpacks, but the ultimate effect is to add lint and unnecessary syntax. It's starting to look more like Java or C or lisp and losing the near-pseudocode syntax that I love about Python. |
@jaraco I can't repeat this: graingert@onomastic:~$ cat foo.py
def unmatched(pair):
test_key, item = pair
graingert@onomastic:~$ pipx run black foo.py
All done! ✨ 🍰 ✨ 'black'...
1 file left unchanged.
graingert@onomastic:~$ cat foo.py
def unmatched(pair):
test_key, item = pair I note that you have two steps, one adding a comma and one splitting over multiple lines. black should never do this because it checks if repeated application makes multiple changes did you use another tool to add a trailing comma? |
oh! You're right. I may have been mistaken. I traced the log and it seems that the original submission of this code was the second with the trailing comma. I was probably conflating the case of |
fwiw, I'd love a flag that strips "magic" trailing commas, |
The parentheses around a single-element LHS unpack are intentional and we're not planning to change it. Other discussion in this issue is off topic. |
is there a way to add settings for this, so the format will be persisted between versions? |
What's "this" referring to in your question? Parentheses around single-element tuples are consistently applied and persisted between all versions of Black released in the past 3 years. This won't be changing. |
I recently upgraded from version
and
It's not a big deal but tons of files are changed after the update. I was looking for a way to ignore these places but without luck. |
Describe the bug
Starting with version 19.10b0, black adds unnecessary parentheses around the left side of an assignment when unpacking tuples with a single item. This now produces inconsistent, and IMO less readable, behavior when compared with unpacking multiple items:
To Reproduce
Expected behavior
Previous formatting (without parentheses) should be preserved.
Environment (please complete the following information):
Does this bug also happen on master?
Yes
The text was updated successfully, but these errors were encountered: