Skip to content

Commit e389390

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

File tree

6 files changed

+51
-14
lines changed

6 files changed

+51
-14
lines changed

CHANGES.md

+1
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

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
FMT_OFF: Final = {"# fmt: off", "# fmt:off", "# yapf: disable"}
2323
FMT_SKIP: Final = {"# fmt: skip", "# fmt:skip"}
2424
FMT_ON: Final = {"# fmt: on", "# fmt:on", "# yapf: enable"}
25+
PRAGMA_COMMENT_MARKER: Final = ("# type:", "# noqa", "# pylint:")
2526

2627
COMMENT_EXCEPTIONS = " !:#'"
2728
_COMMENT_PREFIX = "# "
@@ -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

+2-2
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

+3-4
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

+8-7
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from mypy_extensions import mypyc_attr
1414

1515
from black.cache import CACHE_DIR
16+
from black.comments import PRAGMA_COMMENT_MARKER
1617
from black.mode import Mode, Preview
1718
from black.strings import get_string_prefix, has_triple_quotes
1819
from blib2to3 import pygram
@@ -848,17 +849,17 @@ def is_type_comment(leaf: Leaf) -> bool:
848849
return t in {token.COMMENT, STANDALONE_COMMENT} and v.startswith("# type:")
849850

850851

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

857859

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")
860+
def is_pragma_comment_string(value: str) -> bool:
861+
"""Return True if the given string matches a known pragma comment."""
862+
return value.startswith(PRAGMA_COMMENT_MARKER)
862863

863864

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

tests/data/cases/comments6.py

+35
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)