question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected trailing comma left when wrapping up the list parameter inside a function call

See original GitHub issue

Describe the bug Unexpected trailing comma left when wrapping up the list parameter inside a function call.

To Reproduce

Run black on the following snippet:

foo(
    [
        "list",
        "of",
        "values",   
    ]
)

It results in the following output:

foo(
    ["list", "of", "values",]
)

So it left the trailing comma at the end of the list.

Expected behavior According to the docs regarding trailing commas - “Unnecessary trailing commas are removed if an expression fits in one line” and yet there is trailing comma left at the end. Or maybe I misunderstood the docs?

This wouldn’t bug me a lot except for the fact that it triggers a pep8 checker to point it out (pep8 explicitly discourages this behavior here. And since black is strict subset of PEP 8, that should be taken into account, right?

Environment:

  • Version: lack, version 19.10b0
  • OS and Python version: MacOS/Python 3.7.5

Does this bug also happen on master? Yep, checked with https://black.now.sh/?version=master

Additional context Thanks for providing awesome formatter! 😄 I’d be willing to help fixing that (if you consider that a bug) if someone could point me into the right direction

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:7
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ichard26commented, Sep 20, 2020

Resolved by #1288 which make the “magic trailing comma” feature generally usable in almost all situations. Now nested trailing commas (and I guess Black thought function parameters/arguments count as nested too) will actually make Black explode the collections.

(black) ichard26@acer-ubuntu:~/programming/oss/black$ git checkout 20.8b1
Note: switching to '20.8b1'.
HEAD is now at 2354126 v20.8b1
(black) ichard26@acer-ubuntu:~/programming/oss/black$ cat temp.py
foo(
    [
        "list",
        "of",
        "values",
    ]
)


def foo(a, b):
    return a + b


something = foo(
    a=120,
    b=240,
)
(black) ichard26@acer-ubuntu:~/programming/oss/black$ black temp.py --diff --color --config tests/empty.toml
All done! ✨ 🍰 ✨
1 file would be left unchanged.

This fix is available in the latest version, 20.8b1. If you the reader are confused by what the “magic trailing comma” feature is, please read the documentation on it.


Environment:

  • Black version: 20.8b1
  • Python version: CPython 3.8.5
  • OS version: Ubuntu 20.04.01 LTS
1reaction
vemelcommented, Nov 20, 2019

Fixed in PR

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is a trailing comma in a function call not a syntax error?
Most languages accept trailing commas in comma-separated lists like argument lists or tuple or array literals. This is helpful if you split ...
Read more >
Trailing commas - JavaScript - MDN Web Docs
JavaScript allows trailing commas wherever a comma-separated list of values is accepted and more values may be expected after the last item.
Read more >
PHP 8.0: Trailing commas are allowed in parameter lists and ...
PHP 8.0 syntax allows to leave a trailing-comma in parameter lists and closure use lists. function foo(string $foo, string $bar,) {} function() use...
Read more >
Rules - ESLint - Pluggable JavaScript Linter
A pluggable and configurable linter tool for identifying and reporting on patterns in JavaScript. Maintain your code quality with ease.
Read more >
Groovy Language Documentation
Groovy uses a comma-separated list of values, surrounded by square ... 3, In the second case, the left operand is false and so...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found