Skip to content

Commit

Permalink
Fix unnecessary nesting when wrapping long dict (#4135)
Browse files Browse the repository at this point in the history
Fixes #4129
  • Loading branch information
hauntsaninja committed Jan 20, 2024
1 parent 7f60f3d commit 995e4ad
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Expand Up @@ -23,6 +23,7 @@
blocks, except immediately before a docstring (#4130)
- For stubs, fix logic to enforce empty line after nested classes with bodies (#4141)
- Fix crash when using a walrus in a dictionary (#4155)
- Fix unnecessary parentheses when wrapping long dicts (#4135)

### Configuration

Expand Down
7 changes: 3 additions & 4 deletions src/black/linegen.py
Expand Up @@ -244,15 +244,14 @@ def visit_dictsetmaker(self, node: Node) -> Iterator[Line]:
if node.children[i - 1].type == token.COLON:
if (
child.type == syms.atom
and child.children[0].type == token.LPAR
and child.children[0].type in OPENING_BRACKETS
and not is_walrus_assignment(child)
):
if maybe_make_parens_invisible_in_atom(
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 995e4ad

Please sign in to comment.