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

Black moves comments to wrong place #1355

Closed
leos opened this issue Apr 22, 2020 · 8 comments
Closed

Black moves comments to wrong place #1355

leos opened this issue Apr 22, 2020 · 8 comments
Labels
F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. T: bug Something isn't working

Comments

@leos
Copy link

leos commented Apr 22, 2020

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:

INFO = [
    {
        'a': 'advertisers',
        'b': [
            {'active': [0, 1]}, # All non-deleted rows
            {'active': -1}      # All deleted rows
        ],
        'c': 'd',
    },
]
  1. Run Black on it: black -t py37 -S
  2. Results in:
INFO = [
    {
        'a': 'advertisers',
        'b': [
            {'active': [0, 1]},
            {'active': -1},
        ],  # All non-deleted rows  # All deleted rows
        'c': 'd',
    },
]

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):

  • Version: 19.10b0
  • OS and Python version: Ubuntu 18.04 & Python 3.7.5

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.

@leos leos added the T: bug Something isn't working label Apr 22, 2020
@hugovk
Copy link
Contributor

hugovk commented Apr 23, 2020

Duplicate of #379?

@leos
Copy link
Author

leos commented Apr 26, 2020

@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.

@ichard26
Copy link
Collaborator

ichard26 commented Apr 27, 2020

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 black -t py37 -S -l 94 and the second as black -t py37 -S.

image

A few things I noted of this specific chain of Black calls:

  1. For the first run, --line-length needs to be >93.
  2. Also for the first run, --diff can't be used
  3. The value for --line-length must be really specific for the bug to show up.

Here's a few more command chains that reproduce the bug...

black -t  py37  -S -l 100
black -t py37 -S
black -t  py37  -S -l 100
black -t py37 -S -l 90
black -t py37 -S
black -t  py37  -S -l 100
black -t py37 -S 90

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:

  • Version: 19.10b0
  • OS and Python version: Windows 10 Home Build 18363 & Python 3.8.1

@leos
Copy link
Author

leos commented Apr 27, 2020

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.

@ichard26
Copy link
Collaborator

ichard26 commented Apr 27, 2020

Actually, on further reflection and testing, I think that the cache has nothing to do with the bug. Even when I do:

black -t py37 -S -l 94
black -t py37 -S

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 black -t py37 -S -l 94:

INFO = [
    {
        'a': 'advertisers',
        'b': [{'active': [0, 1]}, {'active': -1},],  # All non-deleted rows  # All deleted rows
        'c': 'd',
    },
]

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)...

@JelleZijlstra JelleZijlstra added the F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. label May 30, 2021
@JelleZijlstra
Copy link
Collaborator

The current behavior here is entirely reasonable:

 % black --diff ~/py/tmp/info.py -S -t py37 --preview
--- /Users/jelle/py/tmp/info.py	2023-04-29 02:14:39.326010 +0000
+++ /Users/jelle/py/tmp/info.py	2023-04-29 02:15:05.270084 +0000
@@ -1,10 +1,10 @@
 INFO = [
     {
         'a': 'advertisers',
         'b': [
-            {'active': [0, 1]}, # All non-deleted rows
-            {'active': -1}      # All deleted rows
+            {'active': [0, 1]},  # All non-deleted rows
+            {'active': -1},  # All deleted rows
         ],
         'c': 'd',
     },
 ]

@ichard26 mentioned above that the comments get squashed together if you first run Black with -l 100 and then don't get moved back, but I think that's reasonable behavior: if you set -l 100, Black tries to move the comments together, and then if you run it with a shorter line length again, it doesn't know how to move the comments where they came from.

@leos
Copy link
Author

leos commented Apr 29, 2023

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:

[
            {'active': [0, 1]},  # DO NOT CHANGE
            {'active': -1},  # This is ok to modify
]

Then reformatting that to:

            [{'active': [0, 1]}, {'active': -1}] # DO NOT CHANGE This is ok to modify

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.

@JelleZijlstra
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: comments The syntactic kind. Not in the language grammar, always on our minds. Best bugs. T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants