Skip to content

Commit 60e568d

Browse files
committed
Don't split on pragma markers
1 parent 58f31a7 commit 60e568d

File tree

6 files changed

+52
-14
lines changed

6 files changed

+52
-14
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
### Stable style
1010

1111
<!-- Changes that affect Black's stable style -->
12+
- Treat lines marked with common pragma markers (e.g. `# noqa`) the same as `# type: ignore` -- don't split them, but allow merging (#4039)
1213

1314
### Preview style
1415

src/black/comments.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from black.mode import Mode, Preview
77
from black.nodes import (
88
CLOSING_BRACKETS,
9+
PRAGMA_COMMENT_MARKER,
910
STANDALONE_COMMENT,
1011
WHITESPACE,
1112
container_of,
@@ -333,7 +334,7 @@ def contains_pragma_comment(comment_list: List[Leaf]) -> bool:
333334
pylint).
334335
"""
335336
for comment in comment_list:
336-
if comment.value.startswith(("# type:", "# noqa", "# pylint:")):
337+
if comment.value.startswith(PRAGMA_COMMENT_MARKER):
337338
return True
338339

339340
return False

src/black/linegen.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@
4747
is_name_token,
4848
is_one_sequence_between,
4949
is_one_tuple,
50+
is_pragma_comment_string,
5051
is_rpar_token,
5152
is_stub_body,
5253
is_stub_suite,
5354
is_tuple_containing_walrus,
54-
is_type_ignore_comment_string,
5555
is_vararg,
5656
is_walrus_assignment,
5757
is_yield,
@@ -1521,7 +1521,7 @@ def maybe_make_parens_invisible_in_atom(
15211521
if (
15221522
# If the prefix of `middle` includes a type comment with
15231523
# ignore annotation, then we do not remove the parentheses
1524-
not is_type_ignore_comment_string(middle.prefix.strip())
1524+
not is_pragma_comment_string(middle.prefix.strip())
15251525
):
15261526
first.value = ""
15271527
last.value = ""

src/black/lines.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
is_multiline_string,
3030
is_one_sequence_between,
3131
is_type_comment,
32-
is_type_ignore_comment,
32+
is_pragma_comment,
3333
is_with_or_async_with_stmt,
3434
replace_child,
3535
syms,
@@ -285,8 +285,7 @@ def contains_uncollapsable_type_comments(self) -> bool:
285285
for comment in comments:
286286
if is_type_comment(comment):
287287
if comment_seen or (
288-
not is_type_ignore_comment(comment)
289-
and leaf_id not in ignored_ids
288+
not is_pragma_comment(comment) and leaf_id not in ignored_ids
290289
):
291290
return True
292291

@@ -322,7 +321,7 @@ def contains_unsplittable_type_ignore(self) -> bool:
322321
# line.
323322
for node in self.leaves[-2:]:
324323
for comment in self.comments.get(id(node), []):
325-
if is_type_ignore_comment(comment):
324+
if is_pragma_comment(comment):
326325
return True
327326

328327
return False

src/black/nodes.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@
134134
BRACKETS: Final = OPENING_BRACKETS | CLOSING_BRACKETS
135135
ALWAYS_NO_SPACE: Final = CLOSING_BRACKETS | {token.COMMA, STANDALONE_COMMENT}
136136

137+
PRAGMA_COMMENT_MARKER: Final = ("# type:", "# noqa", "# pylint:")
138+
137139
RARROW = 55
138140

139141

@@ -848,17 +850,17 @@ def is_type_comment(leaf: Leaf) -> bool:
848850
return t in {token.COMMENT, STANDALONE_COMMENT} and v.startswith("# type:")
849851

850852

851-
def is_type_ignore_comment(leaf: Leaf) -> bool:
852-
"""Return True if the given leaf is a type comment with ignore annotation."""
853+
def is_pragma_comment(leaf: Leaf) -> bool:
854+
"""Return True if the given leaf is a pragma comment. Pragma comments cause lines to
855+
be unsplittable, but mergeable."""
853856
t = leaf.type
854857
v = leaf.value
855-
return t in {token.COMMENT, STANDALONE_COMMENT} and is_type_ignore_comment_string(v)
858+
return t in {token.COMMENT, STANDALONE_COMMENT} and is_pragma_comment_string(v)
856859

857860

858-
def is_type_ignore_comment_string(value: str) -> bool:
859-
"""Return True if the given string match with type comment with
860-
ignore annotation."""
861-
return value.startswith("# type: ignore")
861+
def is_pragma_comment_string(value: str) -> bool:
862+
"""Return True if the given string matches a known pragma comment."""
863+
return value.startswith(PRAGMA_COMMENT_MARKER)
862864

863865

864866
def wrap_in_parentheses(parent: Node, child: LN, *, visible: bool = True) -> None:

tests/data/cases/comments6.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,38 @@ def func(
116116
)
117117

118118
aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*items))) # type: ignore[arg-type]
119+
120+
121+
def func(
122+
a=some_list[0], # type: int
123+
): # type: () -> int
124+
c = call(
125+
0.0123,
126+
0.0456,
127+
0.0789,
128+
0.0123,
129+
0.0456,
130+
0.0789,
131+
0.0123,
132+
0.0456,
133+
0.0789,
134+
a[-1], # noqa
135+
)
136+
137+
c = call(
138+
"aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa" # noqa
139+
)
140+
141+
142+
result = ( # aaa
143+
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
144+
)
145+
146+
AAAAAAAAAAAAA = [AAAAAAAAAAAAA] + SHARED_AAAAAAAAAAAAA + USER_AAAAAAAAAAAAA + AAAAAAAAAAAAA # noqa
147+
148+
call_to_some_function_asdf(
149+
foo,
150+
[AAAAAAAAAAAAAAAAAAAAAAA, AAAAAAAAAAAAAAAAAAAAAAA, AAAAAAAAAAAAAAAAAAAAAAA, BBBBBBBBBBBB], # noqa
151+
)
152+
153+
aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*items))) # noqa: A000

0 commit comments

Comments
 (0)