From 2e4ac3e93fdf8b2e91abc2084aa4ab8bf99c0957 Mon Sep 17 00:00:00 2001 From: Sergey Vartanov Date: Sun, 30 May 2021 21:04:17 +0300 Subject: [PATCH 1/4] Fix issue with fmt: skip (#2254) Not sure the fix is right. Here is what I found: issue is connected with line first.prefix = prefix[comment.consumed :] in `comments.py`. `first.prefix` is a prefix of the line, that ends with `# fmt: skip`, but `comment.consumed` is the length of the `" # fmt: skip"` string. If prefix length is greater than 14, `first.prefix` will grow every time we apply formatting. --- src/black/comments.py | 5 ++++- tests/data/fmtskip6.py | 5 +++++ 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 tests/data/fmtskip6.py 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..cf829dbdb11 --- /dev/null +++ b/tests/data/fmtskip6.py @@ -0,0 +1,5 @@ +class A: + def f(self): + for line in range(10): + if True: + pass # fmt: skip From 8ec7e327dfb17298cdd50caaec0da62f6461e997 Mon Sep 17 00:00:00 2001 From: Sergey Vartanov Date: Sun, 30 May 2021 21:55:05 +0300 Subject: [PATCH 2/4] Add test for fmt: skip Test for fmt: skip comment when indentation needs more than 14 spaces. --- tests/test_format.py | 1 + 1 file changed, 1 insertion(+) 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", From c64768c23d1f873ec8de0f678f91a7793e21e75f Mon Sep 17 00:00:00 2001 From: Sergey Vartanov Date: Sun, 30 May 2021 22:07:41 +0300 Subject: [PATCH 3/4] Add pull request description to CHANGES.md --- CHANGES.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES.md b/CHANGES.md index 81a6d9f6668..e9ca501efb4 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,6 +4,7 @@ ### _Black_ +- Fix failure caused by `fmt: skip` and indentation (#2281) - Correct max string length calculation when there are string operators (#2292) ## 21.5b2 From 4d9565b37facc4daa97d663d74fb6a81bcdb2aa0 Mon Sep 17 00:00:00 2001 From: Sergey Vartanov Date: Thu, 3 Jun 2021 01:12:06 +0300 Subject: [PATCH 4/4] Add output to test --- tests/data/fmtskip6.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/data/fmtskip6.py b/tests/data/fmtskip6.py index cf829dbdb11..0a779fcee00 100644 --- a/tests/data/fmtskip6.py +++ b/tests/data/fmtskip6.py @@ -3,3 +3,11 @@ 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