Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle fmt skip comments without internal spaces #2970

Merged
merged 4 commits into from
Apr 9, 2022
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
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 = ""
siuryan marked this conversation as resolved.
Show resolved Hide resolved
siblings = [prev_sibling]
while (
"\n" not in prev_sibling.prefix
Expand Down
11 changes: 11 additions & 0 deletions tests/data/fmtskip7.py
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"fmtskip4",
"fmtskip5",
"fmtskip6",
"fmtskip7",
"fstring",
"function",
"function2",
Expand Down