Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
- Fix crash while formatting expressions using the walrus operator in complex
`with` statements (#4630)
- Handle `# fmt: skip` followed by a comment at the end of file (#4635)
- Fix crash when a tuple appears in the `as` clause of a `with` statement
(#4634)

### Preview style

Expand Down
2 changes: 2 additions & 0 deletions src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
is_rpar_token,
is_stub_body,
is_stub_suite,
is_tuple,
is_tuple_containing_star,
is_tuple_containing_walrus,
is_type_ignore_comment_string,
Expand Down Expand Up @@ -1626,6 +1627,7 @@ def maybe_make_parens_invisible_in_atom(
node.type not in (syms.atom, syms.expr)
or is_empty_tuple(node)
or is_one_tuple(node)
or (is_tuple(node) and parent.type == syms.asexpr_test)
or (is_yield(node) and parent.type != syms.expr_stmt)
or (
# This condition tries to prevent removing non-optional brackets
Expand Down
11 changes: 11 additions & 0 deletions src/black/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,17 @@ def is_one_tuple(node: LN) -> bool:
)


def is_tuple(node: LN) -> bool:
"""Return True if `node` holds a tuple."""
if node.type != syms.atom:
return False
gexp = unwrap_singleton_parenthesis(node)
if gexp is None or gexp.type != syms.testlist_gexp:
return False

return True


def is_tuple_containing_walrus(node: LN) -> bool:
"""Return True if `node` holds a tuple that contains a walrus operator."""
if node.type != syms.atom:
Expand Down
10 changes: 10 additions & 0 deletions tests/data/cases/context_managers_39.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ async def func():
pass



# don't remove the brackets here, it changes the meaning of the code.
with (x, y) as z:
pass

# output


Expand Down Expand Up @@ -172,3 +177,8 @@ async def func():
some_other_function(argument1, argument2, argument3="some_value"),
):
pass


# don't remove the brackets here, it changes the meaning of the code.
with (x, y) as z:
pass
Loading