psf / black

The uncompromising Python code formatter
https://black.readthedocs.io/en/stable/
MIT License
38.71k stars 2.43k forks source link

Remove optional brackets around unpacking #3203

Open TomFryers opened 2 years ago

TomFryers commented 2 years ago

Remove optional brackets around unpacking

Since #2945, Black removes brackets in the following situation:

for (x, y) in stuff:
    ...

Perhaps this should be extended to include unpacking assignments. Examples in the current Black style

(x, y) = point
(first, *rest, last) = things
[a, b] = foo  # Not touched with for at the moment

Desired style

x, y = point
first, *rest, last = things
a, b = foo  # Not touched with for at the moment

Additional context

The current style is unstable in situations like the following:

x, y = loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
# Modify
x, y = reallyloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
# Blacken
(
    x,
    y,
) = reallyloooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
# Try to revert
(
    x,
    y,
) = loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
# Blacken (with -C, or remove the trailing comma manually)
(x, y) = loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong
ichard26 commented 2 years ago

I agree, cross-linking with https://github.com/psf/black/issues/3080.

johannesrld commented 1 year ago

this would be great to include, it can be quite annoying having code that looks like this:

version, = struct.unpack("<f", field.read(4))

into this

(version,) = struct.unpack("<f", field.read(4))

(edit: although im gonna assume that some people would think the former is less readable)