diff --git a/CHANGES.md b/CHANGES.md index 02b3fdf75d5..2d2b3b4cf49 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,6 +4,7 @@ ### _Black_ +- Fix failure caused by `fmt: skip` and indentation (#2281) - Account for += assignment when deciding whether to split string (#2312) - Correct max string length calculation when there are string operators (#2292) - Fixed option usage when using the `--code` flag (#2259) diff --git a/src/black/comments.py b/src/black/comments.py index 415e391b2a7..c7513c21ef5 100644 --- a/src/black/comments.py +++ b/src/black/comments.py @@ -159,7 +159,10 @@ def convert_one_fmt_off_pair(node: Node) -> bool: first = ignored_nodes[0] # Can be a container node with the `leaf`. parent = first.parent prefix = first.prefix - first.prefix = prefix[comment.consumed :] + if comment.value in FMT_OFF: + first.prefix = prefix[comment.consumed :] + if comment.value in FMT_SKIP: + first.prefix = "" hidden_value = "".join(str(n) for n in ignored_nodes) if comment.value in FMT_OFF: hidden_value = comment.value + "\n" + hidden_value diff --git a/tests/data/fmtskip6.py b/tests/data/fmtskip6.py new file mode 100644 index 00000000000..0a779fcee00 --- /dev/null +++ b/tests/data/fmtskip6.py @@ -0,0 +1,13 @@ +class A: + def f(self): + for line in range(10): + if True: + pass # fmt: skip + +# output + +class A: + def f(self): + for line in range(10): + if True: + pass # fmt: skip diff --git a/tests/test_format.py b/tests/test_format.py index 5c78afe0ba6..fc9678ad27c 100644 --- a/tests/test_format.py +++ b/tests/test_format.py @@ -41,6 +41,7 @@ "fmtskip3", "fmtskip4", "fmtskip5", + "fmtskip6", "fstring", "function", "function2",