black icon indicating copy to clipboard operation
black copied to clipboard

Trailing comma gives newline on wrong expressions

Open jlubcke opened this issue 3 years ago • 4 comments

Describe the bug

Black in some cases put the wrong parts on separate lines when having a trailing comma.

For example this:

assert foo(1, 2, 3,)[
    0
] == {'bar': 'baz'}

Expected result was something like:

assert foo( 
   1,
   2,
   3,
)[0] == {'bar': 'baz'}

To Reproduce

Minimal example:

a = [1,][2]()
b = [1,][2](3)

Expected:

a = [
    1,
][2]()
b = [
    1,
][2](3)

Actual result:

a = [
    1,
][2]()
b = [1,][
    2
](3)

Environment

  • Black's version: 22.8.0 (and main)
  • OS and Python version: Mac

jlubcke avatar Sep 12 '22 06:09 jlubcke

I'll take a look at the issue. It also comes up for me in Python 3.8 (Windows) on Black 22.10.0

KindaOK avatar Nov 09 '22 05:11 KindaOK

Draft Pull request at #3377

KindaOK avatar Nov 16 '22 06:11 KindaOK

Is this related to #4254?

cobaltt7 avatar May 09 '24 12:05 cobaltt7