Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 431bd09

Browse files
Correctly handle fmt: skip comments without internal spaces (psf#2970)
Co-authored-by: Jelle Zijlstra <[email protected]>
1 parent 75f99bd commit 431bd09

4 files changed

Lines changed: 20 additions & 2 deletions

File tree

CHANGES.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010

1111
<!-- Changes that affect Black's stable style -->
1212

13+
- Fix unstable formatting involving `# fmt: skip` comments without internal spaces
14+
(#2970)
15+
1316
### Preview style
1417

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

src/black/comments.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,11 @@ def generate_ignored_nodes(
214214
container: Optional[LN] = container_of(leaf)
215215
if comment.value in FMT_SKIP:
216216
prev_sibling = leaf.prev_sibling
217-
if comment.value in leaf.prefix and prev_sibling is not None:
218-
leaf.prefix = leaf.prefix.replace(comment.value, "")
217+
# Need to properly format the leaf prefix to compare it to comment.value,
218+
# which is also formatted
219+
comments = list_comments(leaf.prefix, is_endmarker=False, preview=preview)
220+
if comments and comment.value == comments[0].value and prev_sibling is not None:
221+
leaf.prefix = ""
219222
siblings = [prev_sibling]
220223
while (
221224
"\n" not in prev_sibling.prefix

tests/data/fmtskip7.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
a = "this is some code"
2+
b = 5 #fmt:skip
3+
c = 9 #fmt: skip
4+
d = "thisisasuperlongstringthisisasuperlongstringthisisasuperlongstringthisisasuperlongstring" #fmt:skip
5+
6+
# output
7+
8+
a = "this is some code"
9+
b = 5 # fmt:skip
10+
c = 9 # fmt: skip
11+
d = "thisisasuperlongstringthisisasuperlongstringthisisasuperlongstringthisisasuperlongstring" # fmt:skip

tests/test_format.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"fmtskip4",
4545
"fmtskip5",
4646
"fmtskip6",
47+
"fmtskip7",
4748
"fstring",
4849
"function",
4950
"function2",

0 commit comments

Comments
 (0)