psf / black

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

`hug_parens_with_braces_and_square_brackets`: Recursively hugging parens decrease readability #4098

Open MichaReiser opened 11 months ago

MichaReiser commented 11 months ago

This is feedback related to the hug_parens_with_braces_and_square_brackets preview style.

Describe the style change

Only collapse the first set of parens rather than all parens recursively. Having too many opening/closing parens on a single line makes it harder to distinguish the individual parentheses (especially if they are all of the same kind).

Examples in the current Black style

Here an example from Ruff's shade after implementing the hug_parens_with_braces_and_square_brackets preview style recursively.

                                     )
                                 )
                                 if commented_entity:
-                                    entity_map = CommentedMap(
-                                        [(entity["entity"], entity["value"])]
-                                    )
+                                    entity_map = CommentedMap([(
+                                        entity["entity"],
+                                        entity["value"],
+                                    )])
                                     entity_map.yaml_add_eol_comment(
                                         commented_entity, entity["entity"]
                                     )
                                     entities.append(entity_map)
                                 else:
                                     entities.append(
-                                        OrderedDict(
-                                            [(entity["entity"], entity["value"])]
-                                        )
+                                        OrderedDict([(
+                                            entity["entity"],
+                                            entity["value"],
+                                        )])
                                     )
                     else:
                         entities.append(

Desired style

                                     )
                                 )
                                 if commented_entity:
                                    entity_map = CommentedMap([
                                        (entity["entity"], entity["value"])
                                     ])
                                     entity_map.yaml_add_eol_comment(
                                         commented_entity, entity["entity"]
                                     )
                                     entities.append(entity_map)
                                 else:
                                     entities.append(
                                         OrderedDict([
                                             (entity["entity"], entity["value"])
                                         ])
                                     )
                     else:
                         entities.append(

Additional context

This issue is split out from my stable style 2024 comment.

Ruff implements hug_parens_with_braces_and_square_brackets in preview style but we intentionally haven't implemented recursive hugging yet.

tolomea commented 1 week ago

I don't know how black views this, but we've always viewed tuples are being more akin to objects than collections. In examples like above I'd want to keep the tuple on one line and the list multi line. so absent other context ([(, I'd favor grouping as ([ and ( but [(( I'd favor grouping as [ and ((