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

# fmt: skip ignored in fluent interface #3364

Open
metov opened this issue Oct 30, 2022 · 3 comments
Open

# fmt: skip ignored in fluent interface #3364

metov opened this issue Oct 30, 2022 · 3 comments
Labels
F: fmtskip fmt: skip implementation T: bug Something isn't working

Comments

@metov
Copy link

metov commented Oct 30, 2022

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 metov added the T: bug Something isn't working label Oct 30, 2022
@ichard26 ichard26 added F: fmtoff fmt: off implementation F: fmtskip fmt: skip implementation and removed F: fmtoff fmt: off implementation labels Oct 30, 2022
@FSpanhel
Copy link

FSpanhel commented Dec 15, 2022

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.

@JelleZijlstra
Copy link
Collaborator

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.

@FSpanhel
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: fmtskip fmt: skip implementation T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants