Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure Black doesn't crash when fmt:off is used before a closing paren #4363

Merged
merged 3 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@

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

- Fix crash when `# fmt: off` is used before a closing parenthesis or bracket. (#4363)

### Preview style

<!-- Changes that affect Black's preview style -->
Expand Down
24 changes: 12 additions & 12 deletions src/black/comments.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,24 +184,24 @@ def convert_one_fmt_off_pair(
for leaf in node.leaves():
previous_consumed = 0
for comment in list_comments(leaf.prefix, is_endmarker=False):
should_pass_fmt = comment.value in FMT_OFF or _contains_fmt_skip_comment(
comment.value, mode
)
if not should_pass_fmt:
is_fmt_off = comment.value in FMT_OFF
is_fmt_skip = _contains_fmt_skip_comment(comment.value, mode)
if (not is_fmt_off and not is_fmt_skip) or (
# Invalid use when `# fmt: off` is applied before a closing bracket.
is_fmt_off
and leaf.type in CLOSING_BRACKETS
):
previous_consumed = comment.consumed
continue
# We only want standalone comments. If there's no previous leaf or
# the previous leaf is indentation, it's a standalone comment in
# disguise.
if should_pass_fmt and comment.type != STANDALONE_COMMENT:
if comment.type != STANDALONE_COMMENT:
prev = preceding_leaf(leaf)
if prev:
if comment.value in FMT_OFF and prev.type not in WHITESPACE:
if is_fmt_off and prev.type not in WHITESPACE:
continue
if (
_contains_fmt_skip_comment(comment.value, mode)
and prev.type in WHITESPACE
):
if is_fmt_skip and prev.type in WHITESPACE:
continue

ignored_nodes = list(generate_ignored_nodes(leaf, comment, mode))
Expand All @@ -213,7 +213,7 @@ def convert_one_fmt_off_pair(
prefix = first.prefix
if comment.value in FMT_OFF:
first.prefix = prefix[comment.consumed :]
if _contains_fmt_skip_comment(comment.value, mode):
if is_fmt_skip:
first.prefix = ""
standalone_comment_prefix = prefix
else:
Expand All @@ -233,7 +233,7 @@ def convert_one_fmt_off_pair(
fmt_off_prefix = fmt_off_prefix.split("\n")[-1]
standalone_comment_prefix += fmt_off_prefix
hidden_value = comment.value + "\n" + hidden_value
if _contains_fmt_skip_comment(comment.value, mode):
if is_fmt_skip:
hidden_value += (
comment.leading_whitespace
if Preview.no_normalize_fmt_skip_whitespace in mode
Expand Down
13 changes: 13 additions & 0 deletions tests/data/cases/fmtonoff6.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Regression test for /~https://github.com/psf/black/issues/2478.
def foo():
arr = (
(3833567325051000, 5, 1, 2, 4229.25, 6, 0),
# fmt: off
)


# Regression test for /~https://github.com/psf/black/issues/3458.
dependencies = {
a: b,
# fmt: off
}
Loading