Yapf: Add knob for forcing a comma on last element of multi-line collection literals

Created on 11 Aug 2017  路  3Comments  路  Source: google/yapf

I think it is valuable to require, in a coding style, that multi-line collection literals have a comma after the trailing entry. This is beneficial since it means that future additions to the collection require only changing the lines that were added, without changing the previous last entry. This makes the output of tools like git blame more useful.

Furthermore, it would be nice if yapf could enforce this style, so it wouldn't be left to code reviews.

I understand this may conflict with the use of comma as a signal to split or not.

Example:

a = {
    "hi": 1,
    "bye": 2,
    "why": 3,
    "try": 4,
    "fill the sky": 5,
    "what do you use to ply": 6,
    "i'm a gruntled guy": 7,
    "she's a small fish to fry": 8
}

would be transformed to

a = {
    "hi": 1,
    "bye": 2,
    "why": 3,
    "try": 4,
    "fill the sky": 5,
    "what do you use to ply": 6,
    "i'm a gruntled guy": 7,
    "she's a small fish to fry": 8,
}

Most helpful comment

I would this feature and I'm totally 馃憤 to enable it by a knob (otherwise I would be against). I happens a lot to me that I create a dictionary with each key/value in a different line and I miss the last comma so yapf re-format it and makes me go back to the code and add the trailling comma. This is a ver common workflow in the team that I used to work.

So, having the ability to enable this feature by a knob it would be something that I would try, at least.

All 3 comments

Hi, I'm new to YAPF and I found this task interesting. I don't see any comment here if this issue has been approved to do, so I want to ask you for it before starting work?

This appears to be a very popular request, but I really really really recommend against it. Modifying the token stream is very non-trivial and takes a lot of care to get it correct.

However, if you want to try it, I'll let it come in, but it needs to be behind a knob. Perhaps the best way to do this would be to add a new pass that modifies the token stream (see comment_splicer.py for an example of how to do this).

I would this feature and I'm totally 馃憤 to enable it by a knob (otherwise I would be against). I happens a lot to me that I create a dictionary with each key/value in a different line and I miss the last comma so yapf re-format it and makes me go back to the code and add the trailling comma. This is a ver common workflow in the team that I used to work.

So, having the ability to enable this feature by a knob it would be something that I would try, at least.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hayd picture hayd  路  8Comments

jamesqo picture jamesqo  路  5Comments

prokher picture prokher  路  7Comments

tschijnmo picture tschijnmo  路  6Comments

bukzor picture bukzor  路  3Comments