-
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
Always split "complex" comprehensions into multiple lines #2841
Comments
Hi! Thanks for submitting. I think there's something to this request, even though it would be quite hard to define a good heuristic for "complex". But I'm not sure if the improvements are worth the misses yet, so let's discuss. I have some issues with the definition you proposed in some common cases:
In a similar vein to above, I think there are many possible situations where I'd consider this simple. For example:
I think if we have just some slightly long names (which are arguably often better because they carry more meaning), 40 chars is over quite quickly. Just a multiplication on minimal names takes 31 chars:
I don't quite agree, because a comprehension can be used as a substitute for
This seems quite reasonable, although one quite common use case is flattening a nested list: I realise I'm reading your proposal here like the Devil reads a Bible, even though I tried to provide reasonable examples. They are a good starting point though! So thank you for coming up with quite a varied list of requirements 👍 I think overall they should be made stricter though, meaning less cases of "complex". But I'm not sure how. Maybe if multiple conditions apply simultaneously..? Perhaps instead of discussing over every detail, we could see what the effect of this would be on The proposal and its implementation should also be evaluated for performance, but at this stage it's not really as important. |
I think this issue is a more thought-out dupe of #2121 I agree with Felix's points, the heuristics laid out in the original post are probably too eager to split into multiple lines. One heuristic that I have yet to see a good counter example to is this, i.e. if there's an |
Nice catch! I think we'll continue the discussion there. I'll include a tl;dr of this discussion as well. |
Describe the style change
"Complex" comprehensions (for some measure of "complex") should always be treated as multiline expressions, analogous to long lists/dicts that won't fit on a single line. "Simple" comprehensions continue to be rendered as a single line.
I appreciate that "simple" and "complex" are handwave-y statements; but as a starting point for discussion, I'll assert a comprehension ceases to be "simple" if:
if
clausefor
clauseI won't claim this is a perfect definition of "simple", but it's a starting point.
Why make this change? As I understand it, one of the goals of Black is to produce small, easily readable diffs on future edits of the code. The simplest list/set comprehension is a dense expression containing at least 3 sub-expressions; the simplest dict comprehension has at least 4 sub-expressions. Each one of those sub-expressions could potentially be the subject of a meaningful code change. If there is any complexity in those terms, the code (and, by extension, a diff of that code) immediately becomes harder to read.
A straightforward key-value reversing comprehension:
is nearing the simplest possible dictionary expression you can write, but to my eyes is also nearing the practical limit of what can be reasonably mentally consumed as a single line (especially if that expression is inlined as an argument to a function or something similar).
The slightly more complex expression:
would exceed the 40 character "complexity" limit, and be transformed into:
As an aside, this simplifies some cases that currently cause problems for Black. If a test assertion is a comparison of 2 comprehensions, each individual comprehension may not trigger the line length limit, but the overall expression will. Black currently takes the approach of splitting the second comprehension onto a separate line in a way that introduces problems of "symmetry" (This was the originally reported on #733). This change removes any question of "symmetry", and reduces the problem in #733 to one of whether the individual comprehensions are "complex"; if they are, whitespace improves readability.
Examples in the current Black style
Desired style
Additional context
This proposal follows on from a discussion that is part of #733.
The text was updated successfully, but these errors were encountered: