Skip to content

Commit

Permalink
Correctly handle fmt: skip comments without internal spaces (#2970)
Browse files Browse the repository at this point in the history
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
  • Loading branch information
siuryan and JelleZijlstra committed Apr 9, 2022
1 parent 75f99bd commit 431bd09
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Expand Up @@ -10,6 +10,9 @@

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

- Fix unstable formatting involving `# fmt: skip` comments without internal spaces
(#2970)

### Preview style

<!-- Changes that affect Black's preview style -->
Expand Down
7 changes: 5 additions & 2 deletions src/black/comments.py
Expand Up @@ -214,8 +214,11 @@ def generate_ignored_nodes(
container: Optional[LN] = container_of(leaf)
if comment.value in FMT_SKIP:
prev_sibling = leaf.prev_sibling
if comment.value in leaf.prefix and prev_sibling is not None:
leaf.prefix = leaf.prefix.replace(comment.value, "")
# Need to properly format the leaf prefix to compare it to comment.value,
# which is also formatted
comments = list_comments(leaf.prefix, is_endmarker=False, preview=preview)
if comments and comment.value == comments[0].value and prev_sibling is not None:
leaf.prefix = ""
siblings = [prev_sibling]
while (
"\n" not in prev_sibling.prefix
Expand Down
11 changes: 11 additions & 0 deletions tests/data/fmtskip7.py
@@ -0,0 +1,11 @@
a = "this is some code"
b = 5 #fmt:skip
c = 9 #fmt: skip
d = "thisisasuperlongstringthisisasuperlongstringthisisasuperlongstringthisisasuperlongstring" #fmt:skip

# output

a = "this is some code"
b = 5 # fmt:skip
c = 9 # fmt: skip
d = "thisisasuperlongstringthisisasuperlongstringthisisasuperlongstringthisisasuperlongstring" # fmt:skip
1 change: 1 addition & 0 deletions tests/test_format.py
Expand Up @@ -44,6 +44,7 @@
"fmtskip4",
"fmtskip5",
"fmtskip6",
"fmtskip7",
"fstring",
"function",
"function2",
Expand Down

0 comments on commit 431bd09

Please sign in to comment.