black icon indicating copy to clipboard operation
black copied to clipboard

`# fmt: skip` ignored in fluent interface

Open metov opened this issue 2 years ago • 3 comments

Say I'd like to format my code like so:

v = (
    foo_dict
    .setdefault("a", {})
    .setdefault("b", {})
    .setdefault("c", {})
    .setdefault("d", {})
    .setdefault("e", {})
)

Black will of course reformat this to put foo_dict.setdefault("a", {}) on the first line.

Luckily, there are directives to not auto-format some lines:

[black] doesn’t reformat lines that end with # fmt: skip

Yet this doesn't work:

v = (
    foo_dict  # fmt: skip
    .setdefault("a", {})
    .setdefault("b", {})
    .setdefault("c", {})
    .setdefault("d", {})
    .setdefault("e", {})
)

becomes:

v = (
    foo_dict.setdefault("a", {})  # fmt: skip
    .setdefault("b", {})
    .setdefault("c", {})
    .setdefault("d", {})
    .setdefault("e", {})
)

Since the line foo_dict # fmt: skip ends with # fmt: skip, I would expect black to leave it alone.

Playground link

metov avatar Oct 30 '22 22:10 metov

I also have this issue (see #3443).

What's the motivation that foo_dict.setdefault("a", {}) is put on the first line? The docs only refer to https://en.wikipedia.org/wiki/Fluent_interface.

I had a look on this page. I seems that the majority of languages use

v = (
    foo_dict
    .setdefault("a", {})
    .setdefault("b", {})
    .setdefault("c", {})
    .setdefault("d", {})
    .setdefault("e", {})
)

instead of

v = (
    foo_dict.setdefault("a", {})
    .setdefault("b", {})
    .setdefault("c", {})
    .setdefault("d", {})
    .setdefault("e", {})
)

In my point of view, the former formatting is by far more readable because I see the object and then its methods that are applied. In the second formatting I have to move my eyes to the right to see what the first method is and then again to the left to see the remaining methods.

Alternatively, one could also use

v = (
    foo_dict.setdefault("a", {})
            .setdefault("b", {})
            .setdefault("c", {})
            .setdefault("d", {})
            .setdefault("e", {})
)

but I would still strongly prefer option 1.

FSpanhel avatar Dec 15 '22 13:12 FSpanhel

Please open a new issue if you want Black's normal formatting to change. This issue should be only about # fmt: skip in this context.

JelleZijlstra avatar Dec 15 '22 17:12 JelleZijlstra

@JelleZijlstra You are right. My apologies. I have opened #3449.

FSpanhel avatar Dec 18 '22 11:12 FSpanhel