-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Strange formatting of fluent interface #571
Comments
Forgive me for using this as the fluent questions thread, but another case: With a single calldef x():
base_ds["security_score_filtered"] = base_ds["security_score_risk_adj"].where(
base_ds["is_share_price_valid"]
& base_ds["is_market_cap_valid"]
& base_ds["is_liquid"]
) With two callsdef x():
base_ds["security_score_filtered"] = (
base_ds["security_score_risk_adj"]
.where(
base_ds["is_share_price_valid"]
& base_ds["is_market_cap_valid"]
& base_ds["is_liquid"]
)
.sum() # <- new line here (only change)
) Why the switch of the first line between the two? I would generally prefer the second format even without the second call (though the question stands independent of my opinion) |
Adding another here, as a (hopefully) helpful additional case: From: df = (
gbq
.read_gbq(query, project="sixty-capital-prod")
.sort_values("factset_entity_id")
) To: df = gbq.read_gbq(query, project="sixty-capital-prod").sort_values(
"factset_entity_id"
) |
And another: Looks good: completion_time = (
a.read_namespaced_job(job.metadata.name, namespace="default")
.status()
.completion_time()
) Now without the completion_time = a.read_namespaced_job(
job.metadata.name, namespace="default"
).status.completion_time |
I would prefer that black applies the fluent style across more cases of chained calls and attribute accesses, such as those examples provided above, even if it does use a few more lines. Consistency between these similar multi-line expressions is more valuable to me than removing a few extra lines. |
Is this possible to define flhent interface in black settings? |
Related to my comment: #2279 (comment) |
Since there are no maintainer comments yet on this issue, I'll echo the points already raised: I'd also like for Black to format fluent interfaces with or without initial parens consistently: def func():
(
thing("argument-1")
.thing("argument-2")
.thing("argument-3")
.thing("argument-4")
.thing("argument-5")
.thing("argument-6")
.thing("argument-7")
.thing("argument-8")
.thing("argument-9")
) adding the parens rather than splitting a single call if the line is too long. |
Here's a more complete set of variations I'd expect to behave similarly: playground
In the example playground, I find that the cases with no assignment and no initial parens are formatted against my expectations. |
From #571 (comment) I suggest we add to the list:
|
And in that case, #510 is very relevant, if not duplicate. But I'll leave it open for now. |
Anything here? |
It would indeed be much appreciated if the formatting of fluent interfaces were more consistent. The examples posted by @max-sixty above are very descriptive of the issue. With method chaining is often better for readability to have more lines but every method in the chain being called in a new line |
anything here? this is currently the only thing that is holding me back from using black |
Please write a PR to fix this issue instead of sending unproductive comments. |
@vmgustavo I'm using |
@JelleZijlstra is it possible to provide custom method chains? So, this issue gets avoided, or is skipping format the only option here? |
can we disable this with a flag? |
Any comments from a maintainer? This feature would benefit the scientific Python community greatly since pandas encourages method chaining so much. |
I'm using a lot of PySpark, and the following
annoyingly gets formatted by black as:
which is way less readable. I know you're looking for PR's, @JelleZijlstra , but not everyone is in the open source community business. Comments like this are just the users of these open source packages hoping to influence the prioritisation a little bit. |
There is no "business" and there is no "prioritisation". There are only PRs. Everyone here is a volunteer. I'm locking this issue to prevent more unproductive notifications for the many people interested in this issue. PRs are still welcome! |
Operating system: Mac
Python version: 3.7
Black version: 18.9b0
Does also happen on master:
Following on https://twitter.com/llanga/status/1052631100290420736
I have a code which uses
yarl.URL
Black formats it like this
I understand that first call is not on a new line because of reasons, but the rest still looks quite odd, especially considering that when there are three calls in the chain, Black produces
The text was updated successfully, but these errors were encountered: