-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Black moves comments to wrong place #1355
Comments
Duplicate of #379? |
@hugovk I'm not sure that it's the same issue as Black didn't reformat the lines at all and just moved the comments. The other strange thing is that in our code this block is in a list of very similar blocks and the comments get moved differently in different blocks - which makes me suspect there's something non-deterministic or some sort of state bug happening. |
I cannot reproduce the bug when running Black as you commented... but Interestingly, I can reproduce this weird bug when running Black on the same file twice with different options supplied. The first one as A few things I noted of this specific chain of Black calls:
Here's a few more command chains that reproduce the bug...
IMO, I think its something relating to how Black behaves when ran on the same file multiple of times over... maybe something relating to caching? I could be very wrong though since I am not knowledgeable on how Black works. Feel free to ask for more info though. Environment:
|
Oh, wow, thank you @ichard26 ! Amazing detective work - that's actually what I had done, I was playing around with different line lengths and rerunning Black to see what it would output and ran across this issue as I was spot checking. Didn't even realize Black maintained a cache. |
Actually, on further reflection and testing, I think that the cache has nothing to do with the bug. Even when I do:
and deleting the cache files myself in between the first and second run, the bug still appears. I think that the real bug is when running Black again on the example which you get after
Also though, I am pretty sure that the example above is related to #1353. So a bug on an edge case (I am pretty sure Black doesn't merge comments)... |
The current behavior here is entirely reasonable:
@ichard26 mentioned above that the comments get squashed together if you first run Black with |
Thanks for cleaning up this ticket @JelleZijlstra. What you're saying makes sense and I'm sure that there's a lot of other cases that led to the decision to merge comments in Black like that, but here's a way that I think this behavior may not be as reasonable:
Then reformatting that to:
could lead to confusion. The two lines of comments could be parts of a multiline comment (which is what I'm guessing led to the behavior of merging them) but if they're two separate lines/sentences/sentence fragments merging them carries that risk. |
I'd actually be OK with never merging comments, as it can definitely lead to confusing output. That should be a new issue, though, and I'm not sure how easy it would be to implement. |
Describe the bug A clear and concise description of what the bug is.
Black moves comments from their lines to a spot where they don't make sense.
To Reproduce Steps to reproduce the behavior:
Start with this code:
black -t py37 -S
Expected behavior A clear and concise description of what you expected to happen.
Trailing comma added, comment location preserved
Environment (please complete the following information):
Does this bug also happen on master? To answer this, you have two options:
Strangely, this does not repro on either master or stable on https://black.now.sh/ :-/
Additional context Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: