black icon indicating copy to clipboard operation
black copied to clipboard

fmt: skip is required at the line which follows the one incorrectly formatted

Open mgoral opened this issue 9 months ago • 0 comments

Hello, I believe I have found a bug in how black handles fmt: skip. Consider the following snippet:

t = (
    {"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"},
    {"foo": "bar"},
)

Without any changes, the line with first dictionary is 114 characters long and should be reformatted by black, which has a default line length limit set to 88 characters.. Second line is only 18 characters long and should be left intact.

Now, if I wanted to preserve formatting in this snippet, I believe I should add fmt: skip after the first dictionary:

t = (
    {"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"},  # fmt: skip
    {"foo": "bar"},
)

But this snippet is reformatted by black to:

t = (
    {
        "foo": "very long string",
        "bar": "another very long string",
        "baz": "we should run out of space by now",
    },  # fmt: skip
    {"foo": "bar"},
)

To prevent formatting of first dictionary, I have to add fmt: skip to both dictionaries:

t = (
    {"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"},  # fmt: skip
    {"foo": "bar"},  # fmt: skip
)

or surround the first dictionary with fmt: off - fmt:on:

t = (
    # fmt: off
    {"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"},
    # fmt: on
    {"foo": "bar"},
)

I'm using the following version, installed with pipx on Debian Trixie (current testing):

$ black --version
black, 24.4.2 (compiled: yes)
Python (CPython) 3.11.9

mgoral avatar May 17 '24 15:05 mgoral