Skip to content

Commit

Permalink
Fix unnecessary nesting when wrapping long dict
Browse files Browse the repository at this point in the history
Fixes psf#4129
  • Loading branch information
hauntsaninja committed Dec 29, 2023
1 parent db9c592 commit 2c571ef
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
7 changes: 3 additions & 4 deletions src/black/linegen.py
Expand Up @@ -238,13 +238,12 @@ def visit_dictsetmaker(self, node: Node) -> Iterator[Line]:
if i == 0:
continue
if node.children[i - 1].type == token.COLON:
if child.type == syms.atom and child.children[0].type == token.LPAR:
if maybe_make_parens_invisible_in_atom(
if child.type == syms.atom and child.children[0].type in OPENING_BRACKETS:
maybe_make_parens_invisible_in_atom(
child,
parent=node,
remove_brackets_around_comma=False,
):
wrap_in_parentheses(node, child, visible=False)
)
else:
wrap_in_parentheses(node, child, visible=False)
yield from self.visit_default(node)
Expand Down
38 changes: 38 additions & 0 deletions tests/data/cases/preview_long_dict_values.py
Expand Up @@ -37,6 +37,26 @@
}


class Random:
def func():
random_service.status.active_states.inactive = (
make_new_top_level_state_from_dict(
{
"topLevelBase": {
"secondaryBase": {
"timestamp": 1234,
"latitude": 1,
"longitude": 2,
"actionTimestamp": Timestamp(
seconds=1530584000, nanos=0
).ToJsonString(),
}
},
}
)
)


# output


Expand Down Expand Up @@ -89,3 +109,21 @@
}
),
}


class Random:
def func():
random_service.status.active_states.inactive = (
make_new_top_level_state_from_dict({
"topLevelBase": {
"secondaryBase": {
"timestamp": 1234,
"latitude": 1,
"longitude": 2,
"actionTimestamp": (
Timestamp(seconds=1530584000, nanos=0).ToJsonString()
),
}
},
})
)

0 comments on commit 2c571ef

Please sign in to comment.