black icon indicating copy to clipboard operation
black copied to clipboard

Option to ignore in-line comments

Open FSpanhel opened this issue 3 years ago • 0 comments

Consider this code snippet

import datetime as dt

default_date = dt.date.today() - dt.timedelta(1)  # my long comment appears here # fmt: skip

After running black in https://black.vercel.app/ this becomes

import datetime as dt

default_date = dt.date.today() - dt.timedelta(
    1
)  # my long comment appears here # fmt: skip

which I find really confusing and by far inferior to the original formatting. Note that black has formatted the code although the line ended with #fmt: skip (!) (probably related to #3009). Indeed, black does not format the code when I remove #fm: skip because the line length is then small enough.

So I wonder whether it might sense to make black ignore in-line comments?

At the moment the only possible solution to avoid this ugly formatting is

  1. Use

    import datetime as dt
    
    # fmt: off
    default_date = dt.date.today() - dt.timedelta(1)  # my long comment appears here
    # fmt: on
    

    which I don't like because I then have to add two empty lines for an in-line comment.

  2. Put the comment on an empty line

    import datetime as dt
    
    # my long comment appears here
    default_date = dt.date.today() - dt.timedelta(1)
    

    which I don't like because this also adds and additional line of code and is not useful if one uses chall chains and wants to add a comment to a particular line of the chain.

FSpanhel avatar Dec 18 '22 12:12 FSpanhel