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

List of list ver 19 VS ver 20 #1941

Closed
guillemmr opened this issue Jan 24, 2021 · 3 comments
Closed

List of list ver 19 VS ver 20 #1941

guillemmr opened this issue Jan 24, 2021 · 3 comments
Labels
T: style What do we want Blackened code to look like?

Comments

@guillemmr
Copy link

After downloading the last black version, notice some style differences in arrays of arrays:

In ver 19.10b0:

my_list = [
        [12, 30, 1,],
        [13, 35, 2,],
        [14, 40, 3,],
        [15, 45, 4,],
        [16, 50, 5,],
        [17, 55, 6,],
        [18, 0, 7,],
    ]

In ver 20.8b1 (blacking the previous list)

    my_list = [
        [
            12,
            30,
            1,
        ],
        [
            13,
            35,
            2,
        ],
        [
            14,
            40,
            3,
        ],
        [
            15,
            45,
            4,
        ],
        [
            16,
            50,
            5,
        ],
        [
            17,
            55,
            6,
        ],
        [
            18,
            0,
            7,
        ],
    ]

Is this expected?
Black docs are explicit about this types:
image

Thanks in advance!

@guillemmr guillemmr added the T: style What do we want Blackened code to look like? label Jan 24, 2021
@xrisk
Copy link
Contributor

xrisk commented Jan 24, 2021

It's because of your trailing commas. They signal to Black that more elements may be added to the list in the future, and thus it tries to put each element onto a separate line.

You can pass --skip-magic-trailing-comma, or remove the trailing commas.

In your case it would be like:

my_list = [
        [12, 30, 1],
        [13, 35, 2],
        [14, 40, 3],
        [15, 45, 4],
        [16, 50, 5],
        [17, 55, 6],
        [18, 0, 7],
    ]

See #826 and #1288

@ichard26
Copy link
Collaborator

Black used to remove the trailing comma if the expression fits in a single line, but this was changed by #826 and #1288. Now a trailing comma tells Black to always explode the expression. This change was made mostly for the cases where you know a collection or whatever will grow in the future. Having it always exploded as one element per line reduces diff noise when adding elements. Before the "magic trailing comma" feature, you couldn't anticipate a collection's growth reliably since collections that fitted in one line were ruthlessly collapsed regardless of your intentions. One of Black's goals is reducing diff noise, so this was a good pragmatic change.

So no, this is not a bug, but an intended feature. The reason why you're filing this issue is probably since we say the following in the (outdated) style documentation:

Unnecessary trailing commas are removed if an expression fits in one line. This makes it 1% more likely that your line won't exceed the allotted line length limit. Moreover, in this scenario, if you added another argument to your call, you'd probably fit it in the same line anyway. That doesn't make diffs any larger.

We missed that this paragraph became incorrect when the "magic trailing comma" feature was introduced. It was eventually fixed in commit 6b935a3, but that was after the stable documentation was released alongside Black 20.8b1.

Anyway, here's the documentation on the "magic trailing comma". Hopefully that helps and sorry for the possible confusion.

@guillemmr
Copy link
Author

All clear,
Thank you @xrisk and @ichard26!

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

No branches or pull requests

4 participants