diff --git a/CHANGES.md b/CHANGES.md index e168e24d76e..b21c319d5e0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -10,6 +10,9 @@ +- Fix unstable formatting involving `# fmt: skip` comments without internal spaces + (#2970) + ### Preview style diff --git a/src/black/comments.py b/src/black/comments.py index 455326469f0..23bf87fca7c 100644 --- a/src/black/comments.py +++ b/src/black/comments.py @@ -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 diff --git a/tests/data/fmtskip7.py b/tests/data/fmtskip7.py new file mode 100644 index 00000000000..15ac0ad7080 --- /dev/null +++ b/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 diff --git a/tests/test_format.py b/tests/test_format.py index 51d8fb0a103..fd5f596b6d5 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -44,6 +44,7 @@ "fmtskip4", "fmtskip5", "fmtskip6", + "fmtskip7", "fstring", "function", "function2",