Skip to content
Closed
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 @@ -30,6 +30,8 @@
- Standardize type comments to form `# type: <value>` (#4645)
- Fix `fix_fmt_skip_in_one_liners` preview feature to respect `# fmt: skip` for compound
statements with semicolon-separated bodies (#4800)
- Fix `fix_fmt_skip_in_one_liners` crashing on `with` statements and annotated
parameters (#4822)

### Configuration

Expand Down
17 changes: 15 additions & 2 deletions src/black/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -587,6 +587,7 @@ def _generate_ignored_nodes_from_fmt_skip(
comments = list_comments(leaf.prefix, is_endmarker=False, mode=mode)
if not comments or comment.value != comments[0].value:
return

if prev_sibling is not None:
leaf.prefix = leaf.prefix[comment.consumed :]

Expand Down Expand Up @@ -632,10 +633,22 @@ def _generate_ignored_nodes_from_fmt_skip(
current_node.prefix = ""
break

# Special case for with expressions
# Without this, we can stuck inside the asexpr_test's children's children
parent = current_node.parent
if (
parent
and parent.type == syms.asexpr_test
and parent.parent
and parent.parent.type == syms.with_stmt
):
current_node = parent

ignored_nodes.insert(0, current_node)

if current_node.prev_sibling is None and current_node.parent is not None:
current_node = current_node.parent
if current_node.prev_sibling is None and parent is not None:
current_node = parent

# Special handling for compound statements with semicolon-separated bodies
if Preview.fix_fmt_skip_in_one_liners in mode and isinstance(parent, Node):
body_node = _find_compound_statement_context(parent)
Expand Down
3 changes: 1 addition & 2 deletions src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -440,8 +440,7 @@ def foo(a: int, b: float = 7): ...

def foo(a: (int), b: (float) = 7): ...
"""
assert len(node.children) == 3
if maybe_make_parens_invisible_in_atom(
if len(node.children) == 3 and maybe_make_parens_invisible_in_atom(
node.children[2], parent=node, mode=self.mode, features=self.features
):
wrap_in_parentheses(node, node.children[2], visible=False)
Expand Down
1 change: 1 addition & 0 deletions tests/data/cases/fmtskip10.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ def foo(): return "mock" # fmt: skip
if True: print("this"); print("that") # fmt: skip
while True: print("loop"); break # fmt: skip
for x in [1, 2]: print(x); print("done") # fmt: skip
def f(x: int): return x # fmt: skip

j = 1 # fmt: skip
while j < 10: j += 1 # fmt: skip
Expand Down
21 changes: 21 additions & 0 deletions tests/data/cases/fmtskip12.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# flags: --preview

with open("file.txt") as f: content = f.read() # fmt: skip

# Ideally, only the last line would be ignored
# But ignoring only part of the asexpr_test causes a parse error
# Same with ignoring the asexpr_test without also ignoring the entire with_stmt
with open (
"file.txt" ,
) as f: content = f.read() # fmt: skip

# output

with open("file.txt") as f: content = f.read() # fmt: skip

# Ideally, only the last line would be ignored
# But ignoring only part of the asexpr_test causes a parse error
# Same with ignoring the asexpr_test without also ignoring the entire with_stmt
with open (
"file.txt" ,
) as f: content = f.read() # fmt: skip
Loading