Skip to content

Commit

Permalink
Don't split on pragma markers
Browse files Browse the repository at this point in the history
  • Loading branch information
zsol committed Nov 10, 2023
1 parent 58f31a7 commit 60e568d
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Stable style

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

### Preview style

Expand Down
3 changes: 2 additions & 1 deletion src/black/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from black.mode import Mode, Preview
from black.nodes import (
CLOSING_BRACKETS,
PRAGMA_COMMENT_MARKER,
STANDALONE_COMMENT,
WHITESPACE,
container_of,
Expand Down Expand Up @@ -333,7 +334,7 @@ def contains_pragma_comment(comment_list: List[Leaf]) -> bool:
pylint).
"""
for comment in comment_list:
if comment.value.startswith(("# type:", "# noqa", "# pylint:")):
if comment.value.startswith(PRAGMA_COMMENT_MARKER):
return True

return False
Expand Down
4 changes: 2 additions & 2 deletions src/black/linegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@
is_name_token,
is_one_sequence_between,
is_one_tuple,
is_pragma_comment_string,
is_rpar_token,
is_stub_body,
is_stub_suite,
is_tuple_containing_walrus,
is_type_ignore_comment_string,
is_vararg,
is_walrus_assignment,
is_yield,
Expand Down Expand Up @@ -1521,7 +1521,7 @@ def maybe_make_parens_invisible_in_atom(
if (
# If the prefix of `middle` includes a type comment with
# ignore annotation, then we do not remove the parentheses
not is_type_ignore_comment_string(middle.prefix.strip())
not is_pragma_comment_string(middle.prefix.strip())
):
first.value = ""
last.value = ""
Expand Down
7 changes: 3 additions & 4 deletions src/black/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
is_multiline_string,
is_one_sequence_between,
is_type_comment,
is_type_ignore_comment,
is_pragma_comment,
is_with_or_async_with_stmt,
replace_child,
syms,
Expand Down Expand Up @@ -285,8 +285,7 @@ def contains_uncollapsable_type_comments(self) -> bool:
for comment in comments:
if is_type_comment(comment):
if comment_seen or (
not is_type_ignore_comment(comment)
and leaf_id not in ignored_ids
not is_pragma_comment(comment) and leaf_id not in ignored_ids
):
return True

Expand Down Expand Up @@ -322,7 +321,7 @@ def contains_unsplittable_type_ignore(self) -> bool:
# line.
for node in self.leaves[-2:]:
for comment in self.comments.get(id(node), []):
if is_type_ignore_comment(comment):
if is_pragma_comment(comment):
return True

return False
Expand Down
16 changes: 9 additions & 7 deletions src/black/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@
BRACKETS: Final = OPENING_BRACKETS | CLOSING_BRACKETS
ALWAYS_NO_SPACE: Final = CLOSING_BRACKETS | {token.COMMA, STANDALONE_COMMENT}

PRAGMA_COMMENT_MARKER: Final = ("# type:", "# noqa", "# pylint:")

RARROW = 55


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


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


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


def wrap_in_parentheses(parent: Node, child: LN, *, visible: bool = True) -> None:
Expand Down
35 changes: 35 additions & 0 deletions tests/data/cases/comments6.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,38 @@ def func(
)

aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*items))) # type: ignore[arg-type]


def func(
a=some_list[0], # type: int
): # type: () -> int
c = call(
0.0123,
0.0456,
0.0789,
0.0123,
0.0456,
0.0789,
0.0123,
0.0456,
0.0789,
a[-1], # noqa
)

c = call(
"aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa", "aaaaaaaa" # noqa
)


result = ( # aaa
"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
)

AAAAAAAAAAAAA = [AAAAAAAAAAAAA] + SHARED_AAAAAAAAAAAAA + USER_AAAAAAAAAAAAA + AAAAAAAAAAAAA # noqa

call_to_some_function_asdf(
foo,
[AAAAAAAAAAAAAAAAAAAAAAA, AAAAAAAAAAAAAAAAAAAAAAA, AAAAAAAAAAAAAAAAAAAAAAA, BBBBBBBBBBBB], # noqa
)

aaaaaaaaaaaaa, bbbbbbbbb = map(list, map(itertools.chain.from_iterable, zip(*items))) # noqa: A000

0 comments on commit 60e568d

Please sign in to comment.