-
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
Inconsistent wrapping for long call chain #2279
Comments
It is even worse if there are statements such as:
Having it linear gives us at least a chance to deal with this. Another option would be to create some black ignored sections?
|
We have that, it's called |
Actually, in line with the documentation, I'd expect it to be formatted like this: 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")
.thing("argument-10")
.thing("argument-11")
.thing("argument-12")
) which Black does keep (playground), but doesn't quite produce. |
I agree that #2279 (comment) is better than the current output, however due to the presence of trailing commas here I would expect that this would be wrapped as in my original report. |
@JelleZijlstra Thank you for correcting me I moved the comment there. I was not sure how this feature is called, however I got bitten by the call chain as well :) |
To add to this. While using SQLAlchemy and long chained ORM query calls black has weird formatting. Here is what black does
Here is what I expect it to do VIA the Black docs:
|
I think this is essentially the same as #517, which has been mentioned here a couple of times already. So let's continue the discussion there. |
@felix-hilden apologies if I'm missing something, but I'm not sure how this relates to #517? That issue looks like it's a release or release-process related issue, while this one describes a particular formatting bug. Is there a typo in the number you're referring to? |
Oh, indeed! It should be #571 👍 |
Aha, sorry, yeah, just found that myself. I think that this case is different to that one as this issue relate to statements which are not themselves within a parenthesised block, that is: x = (
foo().bar().etc()
) vs x = foo().bar().etc() While there is some similarity in how Black handles those cases, I believe they are separate cases. (I've not tested this example recently, but at the time of raising I believe Black's behaviour in the parenthesised case for otherwise similar input code was quite different to the issue reported here). |
I agree with you there, but whatever fix we apply should definitely take care of both cases at the same time. Whether they are somehow separate in our implementation or not is a different consideration. They seem to be formatted similarly (and correctly), but what doesn't quite work is adding the assignment with the arguments. I'll add this distinction to the other issue as well. |
Ah, possibly I've confused things with that example? Here's a comparison of black handling the code input from this issue, with and without surrounding parens. The output is rather different between the two inputs (though I'd argue neither is great in their current form). While I don't entirely disagree that a fix would ideally address both, what complicates this is that the expected behaviour in each case is different. For parenthesised expressions Black tends to wrap at the member-separator, while for non-parenthesised ones it wraps at method arguments 1. For clarity: the issue here is that it's not achieving a self-consistent style given the non-parenthesised non-assignment input, rather whether Black should aim for the with- and without-parens (or with/without assignment) cases to behave the same. Footnotes
|
I think I get what you're saying. And I'd expect trailing commas to behave exactly as they do in your last parenthesised example, exploding the individual call parens. #571 suggests that we don't break inside calls, and you prefer breaks in calls. But because we advertise formatting call chains by separating from dots, I'd say the expected result you provided goes against that to a degree. So let's try to hash it out. I think whether or not the expression has been parenthesised, or if it's an assignment should not affect the result. And even if they should be different, I'd prefer we determine that on a single issue. I'll try to come up with a more comprehensive set of examples. |
Method chaining is inconsistent when the overall expression is a call (rather than, say, an assignment). Not sure if this is a style change or a bug.
Describe the style change
It would be great if black were consistent in its wrapping such that it either chained horizontally or vertically, but not both within the same expression (at least within the same level in the call-nesting of an expression).
Examples in the current Black style
Adding trailing commas doesn't help, though I understand that that's a separate issue (#1671):
Desired style
Additional context
Possibly related issues:
The text was updated successfully, but these errors were encountered: