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
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

<!-- Changes that affect Black's preview style -->

- Fix `fix_fmt_skip_in_one_liners` crashing on `with` statements (#4853)
- Fix `fix_fmt_skip_in_one_liners` crashing on annotated parameters (#4854)

### Configuration
Expand Down
11 changes: 11 additions & 0 deletions src/black/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,10 +650,21 @@ 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
if (
current_node.parent
and current_node.parent.type == syms.asexpr_test
and current_node.parent.parent
and current_node.parent.parent.type == syms.with_stmt
):
current_node = 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

# 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
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