Black: Black incorrectly preserves trailing comma on one-line collection literals and function declarations

Created on 12 Dec 2019  路  3Comments  路  Source: psf/black

The trailing comma is unnecessarily preserved when collapsing an inline dict that is a function argument, though not when the dict is in an assignment. Furthermore, a dict with a trailing comma isn't being collapsed when it could fit onto a single line (I'm not familiar enough to know if this part is a real bug, but it seemed weird). See MWE below.

$ python --version
Python 3.6.0
$ black --version
black, version 19.10b0
$ black --code $'run({\n    1: 2,\n})'
run(
    {1: 2,}
)
$ black --code $'a = {\n    1:2,\n}'
a = {
    1: 2,
}
$ black --code $'a = {\n    1:2\n}'
a = {1: 2}

I couldn't find this anywhere else, apologies if this is a duplicate. Possibly related #925 -- at least, the behavior seemed OK then, anyway.

bug trailing comma

Most helpful comment

As mentioned in #1010, this triggers the E231 flake8 error.

All 3 comments

This is a bug unfortunately. There are two acceptable behaviors for black:

  1. expand all collection literals with trailing commas, not just top level ones (this would make the first example format to
run(
    {
        1: 2,
    }
)
  1. If a collection literal is not expanded, remove the trailing comma. This would make the first example format to
run(
    {1: 2}
)

As mentioned in #1010, this triggers the E231 flake8 error.

1288 will address this issue, it's going to get fixed as part of the next (stable) release

Was this page helpful?
0 / 5 - 0 ratings

Related issues

dusty-phillips picture dusty-phillips  路  21Comments

stefanoborini picture stefanoborini  路  54Comments

jonadaly picture jonadaly  路  23Comments

altendky picture altendky  路  41Comments

sfermigier picture sfermigier  路  43Comments