Skip to content

Commit 2b13ea7

Browse files
17X61xtremepre-commit-ci[bot]cobaltt7
authored
Preserve multiline headers with fmt skip (#5117)
* Preserve multiline headers with fmt skip * Add changelog entry for fmt skip fix * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Expand fmt skip multiline header tests * Add more test cases from #5114 and #5123 * Update comments.py --------- Co-authored-by: xtreme <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: cobalt <[email protected]>
1 parent aef52e0 commit 2b13ea7

3 files changed

Lines changed: 70 additions & 22 deletions

File tree

‎CHANGES.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
<!-- Changes that affect Black's stable style -->
1515

16+
- Preserve multiline compound statement headers when `# fmt: skip` is placed on the
17+
colon line (#5117)
1618
- Fix `# fmt: skip` being ignored in nested `if` expressions with parenthesized `in`
1719
clauses (#4903)
1820
- Fix crash when an f-string follows a `# fmt: off` comment inside brackets (#5097)

‎src/black/comments.py‎

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,27 @@ def _generate_ignored_nodes_from_fmt_skip(
669669
if prev_sibling is None and comment.type == token.COMMENT:
670670
prev_sibling = _find_closest_previous_sibling(leaf)
671671

672-
if prev_sibling is not None:
672+
if parent is not None and parent.type == syms.suite and leaf.type == token.NEWLINE:
673+
# The `# fmt: skip` is on the colon line of the if/while/def/class/...
674+
# statements. The ignored nodes should be previous siblings of the
675+
# parent suite node. Do this before the generic "same physical line"
676+
# logic so multiline headers are preserved as a whole.
677+
leaf.prefix = ""
678+
parent_sibling = parent.prev_sibling
679+
while parent_sibling is not None and parent_sibling.type != syms.suite:
680+
ignored_nodes.insert(0, parent_sibling)
681+
parent_sibling = parent_sibling.prev_sibling
682+
# Special case for `async_stmt` where the ASYNC token is on the
683+
# grandparent node.
684+
grandparent = parent.parent
685+
if (
686+
grandparent is not None
687+
and grandparent.prev_sibling is not None
688+
and grandparent.prev_sibling.type == token.ASYNC
689+
):
690+
ignored_nodes.insert(0, grandparent.prev_sibling)
691+
yield from iter(ignored_nodes)
692+
elif prev_sibling is not None:
673693
# Generates the nodes to be ignored by `fmt: skip`.
674694

675695
# Nodes to ignore are the ones on the same line as the
@@ -775,27 +795,6 @@ def _generate_ignored_nodes_from_fmt_skip(
775795
leaf.prefix = leaf.prefix[comment.consumed :]
776796

777797
yield from ignored_nodes
778-
elif (
779-
parent is not None and parent.type == syms.suite and leaf.type == token.NEWLINE
780-
):
781-
# The `# fmt: skip` is on the colon line of the if/while/def/class/...
782-
# statements. The ignored nodes should be previous siblings of the
783-
# parent suite node.
784-
leaf.prefix = ""
785-
parent_sibling = parent.prev_sibling
786-
while parent_sibling is not None and parent_sibling.type != syms.suite:
787-
ignored_nodes.insert(0, parent_sibling)
788-
parent_sibling = parent_sibling.prev_sibling
789-
# Special case for `async_stmt` where the ASYNC token is on the
790-
# grandparent node.
791-
grandparent = parent.parent
792-
if (
793-
grandparent is not None
794-
and grandparent.prev_sibling is not None
795-
and grandparent.prev_sibling.type == token.ASYNC
796-
):
797-
ignored_nodes.insert(0, grandparent.prev_sibling)
798-
yield from iter(ignored_nodes)
799798

800799

801800
def is_fmt_on(container: LN, mode: Mode) -> bool:
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
class Body(model.BaseBody[
2+
"Keyword", "For", "While", "Group", "If", "Try", "Var", "Return", "Continue",
3+
"Break", "model.Message", "Error"
4+
]): # fmt: skip
5+
__slots__ = ()
6+
7+
8+
class Foo(Base[\
9+
"A", "B"\
10+
]): # fmt: skip
11+
12+
def a(self): ...
13+
14+
15+
def make_result(
16+
first : int,
17+
second: str,
18+
): # fmt: skip
19+
return first, second
20+
21+
22+
async def fetch_result(
23+
client,
24+
*,
25+
timeout = 1,
26+
): # fmt: skip
27+
return await client.fetch(timeout=timeout)
28+
29+
30+
if (
31+
has_permission( user )
32+
and is_ready( item )
33+
): # fmt: skip
34+
process(item)
35+
36+
match (method, *path.split("/")):
37+
case ("GET", "parent", _, "resource", resource_id) \
38+
| ("GET", "resource", resource_id): # fmt: skip
39+
pass
40+
case _:
41+
pass
42+
43+
match x:
44+
case a \
45+
| b \
46+
| c: # fmt: skip
47+
pass

0 commit comments

Comments
 (0)