4 ms·
Auto formatters are great but the problem with Black is that Python is fundamentally incompatible with them, at least if you're only willing to insert whitespac
by quietbritishjim 7y ago
Auto formatters are great but the problem with Black is that Python is fundamentally incompatible with them, at least if you're only willing to insert whitespace. For example a line of code like this:
x = y[1]['a'] + z[2]['b']
can be broken like this in Black (if it's over the line length limit):
x = y[1][
'a'] + z[2]['b']
But it needs brackets inserted so that it can be formatted something like this:
x = (
y[1]['a']
+ z[2]['b']
)
You can argue the formatting I've chosen e.g. whether the binary operator should go on the previous line (the above line is what PEP 8 suggests). But surely it's clear that breaking in the indexing bracket is atrocious.
- konschubert 7y agoBlack does add brackets in some situations.
- quietbritishjim 7y agoFrankly, that is a really annoying comment to make. Can it add brackets in the particular situation I just said? If so, I'd actually be interested, because it's while since I last checked so it could will have changed. But if not, you've made it look like my comment's invalid for no reason. It reminds me of the last time Black came up on a Hacker News. I posted the same objection (but hypothetically) and the actual creator of Black replied with a comment along the lines of "you should check for yourself before making a comment like that". By the time I finally did check, the discussion had died down (like this one now has) and not many people saw the reply. As the creator, he must have known that my objection was correct but he posted a low-effort misleading comment that invalidated it. It doesn't give me a great feeling about the project. I realise you couldn't have known that happened but you can see why I find your reply so annoying.
- jhayward 7y agoMy quick review of some changes Black made in the mercurial code base pointed out this same kind of thing in comprehensions, and a similar problem in the expressions of if statement. I think the newline-after-bracket behavior looks like some brain damage based on how it likes lists formatted. The complaint I have on if clauses is that it doesn't break at logical operators, but rather at an open-paren, which just ruins the readability of compound expressions.