Black: Brackets break a line when code is in multiple lines

Created on 10 Apr 2018  路  2Comments  路  Source: psf/black

Python version: 3.6.4
Black version: 18.4a0
Does also happen on master: yes

It's a little difficult to explain my problem. But I think the example below should be clear.

what black outputs:

a_function_who_return_a_list(
    "a really really really really really long string with one line"
)[
    0
]

a_function_who_return_a_function(
    """a string with
    two lines"""
)(
    "argument"
)

what I expect:

a_function_who_return_a_list(
    "a really really really really really long string with one line"
)[0]

a_function_who_return_a_function(
    """a string with
    two lines"""
)("argument")
design

Most helpful comment

This is fixed by 3eea3aad864b83af3b6c477c32f15eb821fe9341! 馃嵒

All 2 comments

This is an effect of Black's algorithm which looks for the right-most bracket first and splits by that. If that didn't help, it looks further left and splits by those brackets. And so on, and so on.

I agree your hand-formatted examples look nicer but I don't quite know what simple and deterministic algorithm to use to split nicer. Note that "splitting from the left" also doesn't work because that would end up with strange-looking formatting like:

some_dict[
    'key'
] = some.very.long.value()

This is fixed by 3eea3aad864b83af3b6c477c32f15eb821fe9341! 馃嵒

Was this page helpful?
0 / 5 - 0 ratings

Related issues

craigmac picture craigmac  路  3Comments

decibyte picture decibyte  路  3Comments

asottile picture asottile  路  3Comments

testvinder picture testvinder  路  3Comments

JelleZijlstra picture JelleZijlstra  路  3Comments