Skip to content
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 `fmt: skip` skipping the line after instead of the line it's on (#4855)
- Remove unnecessary parentheses from the left-hand side of assignments while preserving
magic trailing commas and intentional multiline formatting (#4865)
- Fix `fix_fmt_skip_in_one_liners` crashing on `with` statements (#4853)
Expand Down
14 changes: 14 additions & 0 deletions src/black/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,10 @@ 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 Preview.fix_fmt_skip_in_one_liners in mode and not prev_sibling and parent:
prev_sibling = parent.prev_sibling

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

Expand Down Expand Up @@ -646,10 +650,20 @@ def _generate_ignored_nodes_from_fmt_skip(
leaf_nodes = list(current_node.prev_sibling.leaves())
current_node = leaf_nodes[-1] if leaf_nodes else current_node

if (
current_node.type in CLOSING_BRACKETS
and current_node.parent
and current_node.parent.type == syms.atom
):
current_node = current_node.parent

if current_node.type in (token.NEWLINE, token.INDENT):
current_node.prefix = ""
break

if current_node.type == token.DEDENT:
break

# Special case for with expressions
# Without this, we can stuck inside the asexpr_test's children's children
if (
Expand Down
9 changes: 9 additions & 0 deletions tests/data/cases/fmtskip10.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,12 @@ def f(x: int): return x # fmt: skip
while j < 10: j += 1 # fmt: skip

b = [c for c in "A very long string that would normally generate some kind of collapse, since it is this long"] # fmt: skip

v = (
foo_dict # fmt: skip
.setdefault("a", {})
.setdefault("b", {})
.setdefault("c", {})
.setdefault("d", {})
.setdefault("e", {})
)
64 changes: 64 additions & 0 deletions tests/data/cases/fmtskip13.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# flags: --preview

t = (
{"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"}, # fmt: skip
{"foo": "bar"},
)

t = (
{
"foo": "very long string",
"bar": "another very long string",
"baz": "we should run out of space by now",
}, # fmt: skip
{"foo": "bar"},
)


t = (
{"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"}, # fmt: skip
{"foo": "bar",},
)

t = (
{
"foo": "very long string",
"bar": "another very long string",
"baz": "we should run out of space by now",
}, # fmt: skip
{"foo": "bar",},
)

# output
t = (
{"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"}, # fmt: skip
{"foo": "bar"},
)

t = (
{
"foo": "very long string",
"bar": "another very long string",
"baz": "we should run out of space by now",
}, # fmt: skip
{"foo": "bar"},
)


t = (
{"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"}, # fmt: skip
{
"foo": "bar",
},
)

t = (
{
"foo": "very long string",
"bar": "another very long string",
"baz": "we should run out of space by now",
}, # fmt: skip
{
"foo": "bar",
},
)