-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
tweak collection literals to explode with trailing comma #826
Conversation
If it's of interest, https://phab.mercurial-scm.org/D6342 shows some results on the hg codebase (which is motivating this change). |
I take it the idea is to explode collection literals on multiple lines when they contain a trailing comma in the input, regardless of their size? I'm not sure I like that idea, because it kind of breaks Black's principle of not depending on the original formatting. Your diff on hg also seems to show that it's exploding very short tuples unnecessarily: see the two big dicts in mercurial/policy.py. |
I talked about this some more with @ambv and I'm OK with the change now. Please fix the CI problems though. If you need help and you're still at PyCon, Lukasz and I are still around, so feel free to find us. |
Related to @JelleZijlstra's first comment, should we really split up collections within a line like this does in the policy.py example? It would probably be good to add a test case for that either way (i.e. how things like |
I plan on getting back to this tonight. I want to try and stop exploding
the sub-containers that lack trailing commas - if anyone has advice for how
I should approach that it would be appreciated.
I'm not at sprints, but plan to be hacking on this at least some of today
and much of Wednesday, and I'm in the same time zone as the sprints in case
some sort of real time communication would be helpful.
…On Mon, May 6, 2019, 11:52 martinvonz ***@***.***> wrote:
Related to @JelleZijlstra <https://github.com/JelleZijlstra>'s first
comment, should we really split up collections within a line like this does
in the policy.py example? It would probably be good to add a test case for
that either way (i.e. how things like {{1,2},{3,4},} get formatted).
Perhaps also test how it gets formatted if some inner collection should get
exploded. For example, how should {{1,2,},{3,4}} get formatted? What if
the outer collection has a trailing comma? clang-format adds a newline
after the trailing comma in all these cases (and doesn't when there is no
trailing comma).
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#826 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AAAE6LKUX2W2LKNNMEWUPZDPUBH4XANCNFSM4HK2GOOQ>
.
|
Please take a look - I think this is workable now. I can't say I'm 100% thrilled with my approach in some corners, but I've also got no idea what I'm doing. :) https://phab.mercurial-scm.org/D6342 is updated with the new results from this patch. |
I ran your branch on my codebase and noticed some changes like this:
This seems undesirable; we need the trailing comma to make the tuple, but that doesn't mean we need to explode it. |
Another example where it's especially ugly:
|
Huh. I guess my defense against one-element tuples isn't good enough. I'll try and figure it out, but if @ambv has any ideas I'd love to hear them. :/ |
Unfortunately no genius suggestions here but I agree we need to not explode them. |
Oh, I see. I explicitly detected the case of [LPAR, one_token, COMMA, RPAR], so I'll miss any one-tuple that's got a more complicated construct inside. :( Is there any prior art for matching a one-tuple in the codebase? |
Use |
I'm a little lost - that takes a Node or Leaf, but I have a Line in the place where I'm doing the detection (look at |
You're right. Feeding leaves to a |
That code was helpful, moved it to another property on Line because it let the code read more clearly. See what you think? I added test cases based on the misformattings upthread. |
I'm still seeing some issues in my codebase:
Seems like it still explodes one-tuples within other collections. |
@@ -142,7 +142,7 @@ def inline_comments_in_brackets_ruin_everything(): | |||
syms.simple_stmt, | |||
[ | |||
Node(statement, result), | |||
Leaf(token.NEWLINE, '\n'), # FIXME: \r\n? | |||
Leaf(token.NEWLINE, '\n') # FIXME: \r\n? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What caused this change? Black should put a trailing comma here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, but you're commenting on the pre-formatted part of the test case, not the result. The spirit of the test appeared to be that the comment moved correctly with the collapsed line, so I removed the trailing comma in the input.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Sorry for that, it's not always easy to tell from changes in the test suite what is actually changing.
Addressed those cases too. I'm off to bed now (got very nerdsniped by this one-tuple problem), but the tests I constructed based on your examples pass. I'm decreasingly happy with the code in |
The current version crashes on this file:
The stack trace ends:
It also caused a bunch of changes to my (already formatted) codebase, which is unexpected. It's hard to tell for me right now exactly what the changes are because I messed up distinguishing this diff from the other changes in Black master (I'll redo the formatting later and try to distinguish the changes properly), but I think it caused this ugly change:
|
Alrighty, my discomfort with how I was handling oneples wore me down. I have reworked the optional-trailing-comma detection pretty extensively, and it feels better to me. At this point, the "meat" of the change is all in the @JelleZijlstra I'm curious if this works on your codebase now. You're right that you shouldn't see changes on already-blackened code. Thanks for all the test cases - it's helped a lot. |
I tried it again and got a similar set of changes, like this:
It also still crashes on the file with |
Sigh, the import thing. That was a latent bug, now tested and fixed. As for the other issue: the line in your left hand diff side gets reformatted by black for me without my patches applied. Check again? I even added that exact case as a test to collections.py and you can see it gets reformatted even in the base commit of this chain, where I haven't changed any black behavior yet. |
@JelleZijlstra Can you please explain what changed your opinion on this matter? One of the most appealing things about Black is that (with few exceptions?) the only thing that affects code formatting is the AST and line length. This obviously goes against that. |
This brings psf/black#826, which helps with psf/black#601.
This brings psf/black#826, which helps with psf/black#601.
This brings psf/black#826, which helps with psf/black#601.
Various formatting changes created by the upgrade to Black 20.8b1: #2060 These mostly have to do with a bug that was fixed in Black's magic trailing comma feature: * psf/black#1288 * psf/black#826 * https://black.readthedocs.io/en/stable/the_black_code_style.html#the-magic-trailing-comma
Various formatting changes created by the upgrade to Black 20.8b1: #2060 These mostly have to do with a bug that was fixed in Black's magic trailing comma feature: * psf/black#1288 * psf/black#826 * https://black.readthedocs.io/en/stable/the_black_code_style.html#the-magic-trailing-comma
need an custom option whether to explode one-element tuple |
This is a change I discussed with @ambv at PyCon - the commit sequence should make things clear. I didn't go to the effort of fixing up existing tests (they'll fail a lot right now), but I can if this looks like a valid approach. This is somewhere between "proof of concept" and something I'd feel happy about, but I wanted feedback on how I'm tackling this early in development since I'm very much noideadog on this code.