-
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
Slicing is not formatted according to PEP 8 #157
Comments
I opened a pull request that tries addressing this. I made a couple more decisions along the way. I think these should be considered well-formatted:
|
@ambv PEP-8 says this:
not what you quoted above:
Notice that the original from PEP-8 does not have whitespace around the + operator. The parenthesized reference to "as the operator with the lowest priority" in PEP-8 Pet Peeves section is related to the following exception on whitespace around operators in PEP-8 that few people pay attention to. YES:
NO:
Normally, according to PEP-8, we would write Similarly, what PEP-8 is saying about the slicing operator is that it is the "lowest precedence" and therefore it has the spaces around it, but notice that also the higher precedence addition (+) has no spaces around it. That is why it should be written as Similarly, in the Black's README the example given as |
Choosing whether to hug math operators or split them with spaces is not easy so Black will not do it. See #148 for a reference why not.
That's not true. The example you're claiming is missing is literally taken from PEP 8. Both variants are allowed in PEP 8. |
@ambv To paraphrase the color quote: "We can have it both ways, as long as it's Black's way." :-) Thanks for pointing out #148. When seen together with this issue, it may raise some questions. According to the answers in these two issues, one exception is easy to implement (space around slice operator), and the other is hard to implement (no space around highest priority math operators). Still, both are exceptions to standard rules: "no space around simple slice operator", such as The fact is that, in the math operator case, Black chooses to (over)simplify by always using the standard rule, at the cost of making the original math expressions unreadable. In the slice operator case, it chooses to implement the exception, because it's easy, at the benefit of very small readability improvement. I doubt people have too many issues with reading cuddled slice operators the way pycodestyle simplifies them. Simplifying both cases seems a reasonable choice too. By implementing this slice operator exception, Black gets a little higher percentage of PEP-8 compliance, albeit, at the price of making the choice between the two equally acceptable slice operator exceptions, as you point out. The first formatting of that exception, as I pointed out, satisfies both the math and the slice operator exceptions, providing even more of PEP-8 compliance. The reason why one exception formatting, among the two, was not chosen by Black, reveals, through issue #148, how hard it is to implement all these exceptions. Unfortunately, because of that choice, Black changes Thus, this kind of cherry-picking the PEP-8 exceptions may feel arbitrary and subjective. Personally, I accept the fact that Black developers already made their decisions and that this issue is closed. Thanks! |
Just to add a relevant point to save other people the trouble:
The real issue: Whitespace around colons in slices is broken in There are a couple other
A simpler and more thorough example: a[b-1:b+1] # no complaint
a[b-1 :b+1]
a[b-1: b+1] # no complaint
a[b-1 : b+1] # recommended by pep8
a[b - 1:b + 1] # no complaint
a[b - 1 : b + 1]
a[b-1:] # no complaint
a[b-1 :]
a[b - 1:] # no complaint
a[b - 1 :]
a[b - 1 : ]
While I'd ordinarily argue that |
What makes you say that Black is violating " |
add E203 ignore to flake8 Ref: psf/black#157
I always found what other formatters are doing here wrong and currently Black is also doing the wrong thing.
Needs changing
The explicit PEP 8 recommendation is:
YES:
NO:Essentially, we should treat
:
as an "operator" inside slices when any operand of the slice includes punctuation (dots, brackets, and other math, binary or logic operators).More examples:
YES
NO:Note that PEP 8 allows for the following that we cannot properly enforce:
Black won't do that because "lower", "upper", or "offset" can be arbitrary complexity and then operator hugging isn't improving readability anymore.
Already well formatted
Black is already formatting the other PEP 8 recommendation well:
YES:
NO:(edited: a previous version of this issue suggested hugging operators but I since realized this cannot be performed consistently)
The text was updated successfully, but these errors were encountered: