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

Provide option for formatting fluent interfaces #3449

Open
FSpanhel opened this issue Dec 18, 2022 · 0 comments
Open

Provide option for formatting fluent interfaces #3449

FSpanhel opened this issue Dec 18, 2022 · 0 comments
Labels
T: style What do we want Blackened code to look like?

Comments

@FSpanhel
Copy link

FSpanhel commented Dec 18, 2022

Consider the fluent interface

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

At the moment, black formats this into


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

The reason for the decision to put the first method call on the first line is unclear to me.
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 the first formatting (?).

In my opinion, the first formatting is by far more readable because I can see the object and the methods applied from top to bottom.
The second formatting requires me to move my eyes to the right to see the first method, and then back to the left to see the remaining method(s).
#3364 also suggests that there are other people who definitely prefer the first option.

However, some people might have a different opinion on that and I've seen both styles used. I think this issue is comparable to string formatting. Since blacks also allows an option to use “single quotes for data, double quotes for human-readable strings" (see here) I wonder whether it would be possible to create an option to allow the previous formatting?

@FSpanhel FSpanhel added the T: style What do we want Blackened code to look like? label Dec 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

1 participant