Skip to content
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

Black removes trailing commas from single element dicts #274

Closed
rouge8 opened this issue May 29, 2018 · 20 comments
Closed

Black removes trailing commas from single element dicts #274

rouge8 opened this issue May 29, 2018 · 20 comments
Labels
F: trailing comma Full of magic T: bug Something isn't working

Comments

@rouge8
Copy link
Contributor

rouge8 commented May 29, 2018

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 f471271

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"
 }
@ambv
Copy link
Collaborator

ambv commented May 29, 2018

Yes, that will be fixed.

@ambv ambv added the T: bug Something isn't working label May 29, 2018
@graingert
Copy link
Contributor

graingert commented May 30, 2018

This also happens with multi element (when short enough to span one line) functions

@ambv
Copy link
Collaborator

ambv commented May 30, 2018

@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.

@graingert
Copy link
Contributor

@ambv I don't see the documentation for that

@graingert
Copy link
Contributor

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.

@ambv
Copy link
Collaborator

ambv commented May 30, 2018

Yes, and before you have an example with TracebackException that specifically demonstrates this behavior.

@graingert
Copy link
Contributor

I misunderstood because an argument list is not an expression

@ambv
Copy link
Collaborator

ambv commented May 30, 2018

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.

@graingert
Copy link
Contributor

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

@graingert
Copy link
Contributor

graingert commented May 30, 2018

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 ...

@volksport
Copy link

@ambv any thoughts on which release this would be addressed? Thank you!

@ambv
Copy link
Collaborator

ambv commented Aug 27, 2018

The one this week.

nbraud added a commit to spreadspace/onionbalance-docker that referenced this issue Sep 30, 2018
This does not include missing trailing commas due to a bug in black:

  psf/black#274
@luismmontielg
Copy link

Hello @ambv, thank you so much for black. Do you have any idea of when this fix would be released?

@KenjiTakahashi
Copy link

This seems to still be true, any plans on addressing?

@ambv
Copy link
Collaborator

ambv commented May 7, 2019

That will be fixed with #826.

@idgserpro
Copy link

That will be fixed with #826.

@ambv according to #826 (comment) by @durin42, this issue will not be fix in #826.

@idgserpro
Copy link

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.

@durin42
Copy link
Contributor

durin42 commented Sep 3, 2019

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?)

@idgserpro
Copy link

In cases like:

data = {
    "test": "test"
}

flake8-commas complains of missing comma. It would be nice if Black added comma in this case.

@ambv
Copy link
Collaborator

ambv commented Mar 3, 2020

The remaining issues here will be handled in #1288.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: trailing comma Full of magic T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

8 participants