Black: Trailing list/tuple literals in arguments

Created on 9 Sep 2019  Â·  7Comments  Â·  Source: psf/black

One case where I consider black’s output clearly uglier than my hand-formatted output is code that looks like tree DSLs, e.g.:

thing = node("span", [
    "Some important",
    node("em", ["text"]),
    ".",
])

or similar code:

if isinstance(thing, (
    long.ClassName,
    another.long.ClassName,
)):
    ...

Both of which black would blow up into the space-consuming and in no way more clear versions with one short parameter on its own line and then the list/tuple literal after, with one more indentation level than in my version.

Most helpful comment

(resulting in flake8 complaining about "E231 missing whitespace after ','")
black, version 19.10b1.dev5+gde6f4b1

Somewhat tangentially, is triggering E231 here considered a bug in Black, or is it intended behavior? I'm not sure if I should configure my flake8 to ignore E231 or fix it manually and wait for an updated black with the fix.

All 7 comments

Looks like https://github.com/psf/black/pull/826 should also handle function arguments (correctly)? /cc @durin42

Note that it turns:

f(
    [
        1,
        2,
    ]
)

Into

f(
    [1, 2,]
)

(resulting in flake8 complaining about "E231 missing whitespace after ','")
black, version 19.10b1.dev5+gde6f4b1

I think it makes sense to collapse a list/tuple/… literal like this (sans trailing comma). I think the precedence depending on line length should be (most preferred to least preferred):

  1. py function([member1, member2])
  2. py function( [member1, member2] )
  3. py function([ member1, member2, ])

and for a call with a trailing collection literal:

  1. py function(argument1, [member1, member2])
  2. py function( argument1, [member1, member2] )
  3. py function(argument1, [ member1, member2, ])
  4. py function( argument1, [ member1, member2, ], )

(resulting in flake8 complaining about "E231 missing whitespace after ','")
black, version 19.10b1.dev5+gde6f4b1

Somewhat tangentially, is triggering E231 here considered a bug in Black, or is it intended behavior? I'm not sure if I should configure my flake8 to ignore E231 or fix it manually and wait for an updated black with the fix.

I too am hitting this, while I can setup flake8 to ignore, I'm curious which formatter is correct here.

2020-01-16 01:20:16.197638 | centos-8 | ./plugins/modules/vyos_command.py:216:72: E231 missing whitespace after ','
2020-01-16 01:20:16.197646 | centos-8 |         {"stdout": responses, "stdout_lines": list(to_lines(responses)),}

@pabelanger

{"stdout": responses, "stdout_lines": list(to_lines(responses)),}

As far as I can tell that's a bug in black, and you should remove it manually there (black will not re-add it then).

As far as I can tell that's a bug in black

Is there a separate ticket for this? It sure is very annoying.

Yes, #1202

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mhham picture mhham  Â·  22Comments

altendky picture altendky  Â·  41Comments

Lukas0907 picture Lukas0907  Â·  66Comments

sfermigier picture sfermigier  Â·  43Comments

stefanoborini picture stefanoborini  Â·  54Comments