Skip to content

Commit a8bfcc1

Browse files
Fix fmt: skip skipping the line after instead of the line it's on (#4855)
* Fix `fmt: skip` skipping the line after instead of the line it's on (#3364, #4366) Signed-off-by: cobalt <61329810+cobaltt7@users.noreply.github.com> * Apply suggestion from @cobaltt7 * Add tests Signed-off-by: cobalt <61329810+cobaltt7@users.noreply.github.com> * Fix error on preview when only the closing bracket was ignored Signed-off-by: cobalt <61329810+cobaltt7@users.noreply.github.com> * Fix merge conflict Signed-off-by: cobalt <61329810+cobaltt7@users.noreply.github.com> * If i fully gate it under fix_fmt_skip_in_one_liners it works Signed-off-by: cobalt <61329810+cobaltt7@users.noreply.github.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Fix should-be final issues Signed-off-by: cobalt <61329810+cobaltt7@users.noreply.github.com> --------- Signed-off-by: cobalt <61329810+cobaltt7@users.noreply.github.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 3649771 commit a8bfcc1

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

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

25+
- Fix `fmt: skip` skipping the line after instead of the line it's on (#4855)
2526
- Remove unnecessary parentheses from the left-hand side of assignments while preserving
2627
magic trailing commas and intentional multiline formatting (#4865)
2728
- Fix `fix_fmt_skip_in_one_liners` crashing on `with` statements (#4853)

src/black/comments.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,10 @@ def _generate_ignored_nodes_from_fmt_skip(
605605
comments = list_comments(leaf.prefix, is_endmarker=False, mode=mode)
606606
if not comments or comment.value != comments[0].value:
607607
return
608+
609+
if Preview.fix_fmt_skip_in_one_liners in mode and not prev_sibling and parent:
610+
prev_sibling = parent.prev_sibling
611+
608612
if prev_sibling is not None:
609613
leaf.prefix = leaf.prefix[comment.consumed :]
610614

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

653+
if (
654+
current_node.type in CLOSING_BRACKETS
655+
and current_node.parent
656+
and current_node.parent.type == syms.atom
657+
):
658+
current_node = current_node.parent
659+
649660
if current_node.type in (token.NEWLINE, token.INDENT):
650661
current_node.prefix = ""
651662
break
652663

664+
if current_node.type == token.DEDENT:
665+
break
666+
653667
# Special case for with expressions
654668
# Without this, we can stuck inside the asexpr_test's children's children
655669
if (

tests/data/cases/fmtskip10.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,12 @@ def f(x: int): return x # fmt: skip
1111
while j < 10: j += 1 # fmt: skip
1212

1313
b = [c for c in "A very long string that would normally generate some kind of collapse, since it is this long"] # fmt: skip
14+
15+
v = (
16+
foo_dict # fmt: skip
17+
.setdefault("a", {})
18+
.setdefault("b", {})
19+
.setdefault("c", {})
20+
.setdefault("d", {})
21+
.setdefault("e", {})
22+
)

tests/data/cases/fmtskip13.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# flags: --preview
2+
3+
t = (
4+
{"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"}, # fmt: skip
5+
{"foo": "bar"},
6+
)
7+
8+
t = (
9+
{
10+
"foo": "very long string",
11+
"bar": "another very long string",
12+
"baz": "we should run out of space by now",
13+
}, # fmt: skip
14+
{"foo": "bar"},
15+
)
16+
17+
18+
t = (
19+
{"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"}, # fmt: skip
20+
{"foo": "bar",},
21+
)
22+
23+
t = (
24+
{
25+
"foo": "very long string",
26+
"bar": "another very long string",
27+
"baz": "we should run out of space by now",
28+
}, # fmt: skip
29+
{"foo": "bar",},
30+
)
31+
32+
# output
33+
t = (
34+
{"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"}, # fmt: skip
35+
{"foo": "bar"},
36+
)
37+
38+
t = (
39+
{
40+
"foo": "very long string",
41+
"bar": "another very long string",
42+
"baz": "we should run out of space by now",
43+
}, # fmt: skip
44+
{"foo": "bar"},
45+
)
46+
47+
48+
t = (
49+
{"foo": "very long string", "bar": "another very long string", "baz": "we should run out of space by now"}, # fmt: skip
50+
{
51+
"foo": "bar",
52+
},
53+
)
54+
55+
t = (
56+
{
57+
"foo": "very long string",
58+
"bar": "another very long string",
59+
"baz": "we should run out of space by now",
60+
}, # fmt: skip
61+
{
62+
"foo": "bar",
63+
},
64+
)

0 commit comments

Comments
 (0)