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.

Black 19.10b0 no longer fixes short argument lists to a single line

See original GitHub issue

Describe the bug

In previous versions of Black, short argument lists would be wrapped to a single line. In new versions of Black, this is not always the case.

If the argument list starts off as a single line, it’ll remain a single line. If the argument list starts as multiple lines, it’ll remain multiple lines. See test case below.

To Reproduce

test.py

my_func(
    "a",
    "b",
)

With Black 19.3b0

@@ -1,5 +1,2 @@
-my_func(
-    "a",
-    "b",
-)
+my_func("a", "b")

With Black 19.10b0

@@ -1,5 +1,4 @@
 my_func(
-    "a",
-    "b",
+    "a", "b",
 )

Expected behavior

I expect the behavior experienced in 19.3b0.

Environment

  • Version: 19.10b0 (also master)
  • OS and Python version: Fedora 30 Python 3.7.5

Does this bug also happen on master?

Yes.

https://black.now.sh/?version=master&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4ABjAEZdAD2IimZxl1N_WmBpTZzyXdYgGAumRPJ9fzc8wrFrsw5q41oEoZU7ytbygjdQqX-b0UdsSVEi1fbjjwWIr_X0wjnhZyweKwAAAABPNX1pNP1yrQABYmRPGZI9H7bzfQEAAAAABFla

Issue Analytics

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

github_iconTop GitHub Comments

16reactions
jdufresnecommented, Nov 5, 2019

FWIW, as an end user, I find this new beahvior quite suprising and a move away from the opinionated philosophy I really love about Black. When I write Python code, I almost never think about formatting anymore because I know Black will take of it later. With this change, it means I’m back to thinking about formatting. “Should I add a trailing comma or remove it?”, is now a question I’ll be asking before submitting code.

I normally blindly add a trailing comma to all code. Not because I want to keep arguments across multiple lines, but just out of habit. I don’t think about it much more than that and let Black figure it out later.

The PR that introduce this change is all about preserving multiple lines in collections. So what does that have to do with argument lists of function calls? Are you intentionally grouping argument lists in with collections here, or is that an unintended side effect of the implementation?

I think there is another problem with this change. Consider a very long argument list:

my_func("abcabcabc123123", "abcabcabc123123", "abcabcabc123123", "abcabcabc123123", "abcabcabc123123")

After formatting using master, this becomes (notice a trailing comma was added):

my_func(
    "abcabcabc123123",
    "abcabcabc123123",
    "abcabcabc123123",
    "abcabcabc123123",
    "abcabcabc123123",
)

Now – perhaps due to refactoring – the last 2 arguments are removed and reformatted:

my_func(
    "abcabcabc123123", "abcabcabc123123", "abcabcabc123123",
)

Now, because of this new change and the added trailing comma, Black doesn’t reformat the line back to a single line.

0reactions
JelleZijlstracommented, May 29, 2021

This is intended behavior of the magic trailing comma. As of recent releases, you can use --skip-magic-trailing-comma if you don’t want this behavior.

Read more comments on GitHub >

github_iconTop Results From Across the Web

black 19.10b0 - PyPI
use a single blank line between top-level class definitions, or none if the classes are very small. Black enforces the above rules. There...
Read more >
Formatter black is not working on my VSCode...but why?
Just run from the command line if you need to format a lot of files at once. First, check if you have this...
Read more >
ChangeLog - babeltrace - Gitiles - Code Review - LTTng
Fix : output non-LTTng CTF trace with same relative path as input. * cli: don't include version.h in babeltrace2-cfg-cli-params-arg.c.
Read more >
How to format files on save using black with neovim and coc
Something unusual that does happen is that my buffer for a python file is constantly in need of saving. By which I mean...
Read more >
Contributing to pandas — pandas 1.1.3 documentation
To the new user, working with Git is one of the more daunting aspects of ... You should use a black version >=...
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