I would expect Black to add trailing commas to multi-line dict/list/set/etc. literals with a single element, based on:
Black will add trailing commas to expressions that are split by comma where each element is on its own line. This includes function signatures.
Unnecessary trailing commas are removed if an expression fits in one line. This makes it 1% more likely that your line won't exceed the allotted line length limit. Moreover, in this scenario, if you added another argument to your call, you'd probably fit it in the same line anyway. That doesn't make diffs any larger.
Operating system: OS X 10.11.6
Python version: 3.6.5
Black version: 18.5b1
Does also happen on master: yes, as of f471271831ffc7b0f71cc119edac33c267735d36
data = {
"long_field_name": "something faaaaaaaaaaaaaar too long to fit on the first line",
}
--- t.py (original)
+++ t.py (formatted)
@@ -1,4 +1,4 @@
data = {
- "long_field_name": "something faaaaaaaaaaaaaar too long to fit on the first line",
+ "long_field_name": "something faaaaaaaaaaaaaar too long to fit on the first line"
}
Yes, that will be fixed.
This also happens with multi element (when short enough to span one line) functions
@graingert, yes, this is where the bug stems from. That behavior will stay in calls and signatures for now as it's intentional and documented. I admit that it increasingly starts looking like an edge case as the style is evolving. We can revisit this later.
@ambv I don't see the documentation for that
oh you mean this:
Unnecessary trailing commas are removed if an expression fits in one line. This makes it 1% more likely that your line won't exceed the allotted line length limit. Moreover, in this scenario, if you added another argument to your call, you'd probably fit it in the same line anyway. That doesn't make diffs any larger.
Yes, and before you have an example with TracebackException that specifically demonstrates this behavior.
I misunderstood because an argument list is not an expression
Agreed, I try to be rather strict in vocabulary but that's not always easy. Some wording applies to both statements and expressions, some wording applies to argument lists, and so on.
Ah I'd prefer behaviour that matched https://github.com/PyCQA/flake8-commas (obviously because I wrote it) then I can point people to use black instead
Moreover, in this scenario, if you added another argument to your call, you'd probably fit it in the same line anyway.
To be fair this is only true for functions, because you only get 4 extra spaces when you split the parenth-form/list/dict/set literal, so I can see why you'd special case them.
special cases aren't special enough to ...
@ambv any thoughts on which release this would be addressed? Thank you!
The one this week.
Hello @ambv, thank you so much for black. Do you have any idea of when this fix would be released?
This seems to still be true, any plans on addressing?
That will be fixed with https://github.com/python/black/pull/826.
That will be fixed with #826.
@ambv according to https://github.com/psf/black/pull/826#issuecomment-524593373 by @durin42, this issue will not be fix in #826.
An _ugly workaround_ if you really need the trailing commas (specially in flake8) is to call add-trailing-comma right after black is called in your editor, or as a pre-commit hook.
Note that #826 will cause trailing commas to no longer be removed, but it won't add new ones if they're not needed (needed is complicated and I can't quite explain it clearly here, but hopefully that helps?)
In cases like:
data = {
"test": "test"
}
flake8-commas complains of missing comma. It would be nice if Black added comma in this case.
The remaining issues here will be handled in #1288.
Most helpful comment
Ah I'd prefer behaviour that matched https://github.com/PyCQA/flake8-commas (obviously because I wrote it) then I can point people to use black instead