black
black copied to clipboard
fix: Remove parenthesis around sole list items
Fixes #3545, but it affects more places as well.
Description
I added this to the hug_parens_with_braces_and_square_brackets unstable style since there was a lot of overlapping code. I renamed it to concise_nested_brackets to reflect this. That is a breaking change if anyone was using --enable-unstable-feature hug_parens_with_braces_and_square_brackets, but to my knowlege, the existence of certain unstale features is not gurenteed to be stable. I can add the old name back as a (deprecated) alias or revert the rename altogether if so desired.
This does introduce more places where #4036 would be triggered:
items = [ # type: ignore
( # type: ignore
{"key1": "val1", "key2": "val2", "key3": "val3"}
if some_var == "longstring"
else { "key": "val" }
)
]
Checklist - did you ...
- [y] Add an entry in
CHANGES.mdif necessary? - [y] Add / update tests if necessary?
- [y] Add new / update outdated documentation?
diff-shades reports zero changes comparing this PR (8c4519f80632dda09dbce5d20758c17acaf4c861) to main (b677a643c5322b6af2dbca1f8f7a4f8c98e732aa).
Apologies for the delay @JelleZijlstra, I've had other priorities recently.
The main issue was hug_parens_with_braces_and_square_brackets was required for the parens to be removed around multi-line expressions. I tried to identify exactly what parts it depended on, but I don't think it would be an easy refactor. I settled on just documenting this limitation instead.
I put remove_lone_list_item_parens in the unstable style due to this limitation, although it does work on its own for simple cases. If desired, I could move it to the preview style so preview users could get the benefits in those cases, or we could wait for hug_parens_with_braces_and_square_brackets. The original issue #3545 was for the more complex cases.