Unverified Commit 40fae181 authored by Sergey Vartanov's avatar Sergey Vartanov Committed by GitHub
Browse files

Possible fix for issue with indentation and fmt: skip (#2281)

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.

Fixes #2254
parent c1c24183
Loading
Loading
Loading
Loading
+1 −0
Changes for CHANGES.md: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -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)
+3 −0
Changes for src/black/comments.py: 3 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -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
            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

tests/data/fmtskip6.py

0 → 100644
+13 −0
Changes for tests/data/fmtskip6.py: 13 added lines, 0 removed lines.
Original line number Diff line number Diff line
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
+1 −0
Changes for tests/test_format.py: 1 added line, 0 removed lines.
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@
    "fmtskip3",
    "fmtskip4",
    "fmtskip5",
    "fmtskip6",
    "fstring",
    "function",
    "function2",