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

Trailing list/tuple literals in arguments #1010

Open
flying-sheep opened this issue Sep 9, 2019 · 7 comments
Open

Trailing list/tuple literals in arguments #1010

flying-sheep opened this issue Sep 9, 2019 · 7 comments
Labels
F: linebreak How should we split up lines? T: style What do we want Blackened code to look like?

Comments

@flying-sheep
Copy link

flying-sheep commented Sep 9, 2019

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.

@blueyed
Copy link
Contributor

blueyed commented Nov 18, 2019

Looks like #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

@flying-sheep
Copy link
Author

flying-sheep commented Nov 18, 2019

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. function([member1, member2])
  2. function(
        [member1, member2]
    )
  3. function([
        member1,
        member2,
    ])

and for a call with a trailing collection literal:

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

@obi1kenobi
Copy link

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

@pabelanger
Copy link

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)),}

@blueyed
Copy link
Contributor

blueyed commented Jan 16, 2020

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

@SpotlightKid
Copy link

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

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

@zsol
Copy link
Collaborator

zsol commented Jan 16, 2020

Yes, #1202

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
F: linebreak How should we split up lines? T: style What do we want Blackened code to look like?
Projects
None yet
Development

No branches or pull requests

7 participants