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

Fix for "# fmt: on" with decorators #1324

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 26 additions & 2 deletions black.py
Expand Up @@ -3118,9 +3118,33 @@ def generate_ignored_nodes(leaf: Leaf) -> Iterator[LN]:
if is_fmt_on:
return

yield container
# fix for fmt: on in children
if contains_fmt_on_at_column(container, leaf.column):
for child in container.children:
if not contains_fmt_on_at_column(child, leaf.column):
yield child
return
else:
yield container
container = container.next_sibling


def contains_fmt_on_at_column(container, column):
for child in container.children:
if (isinstance(child, Node) and first_leaf_column(child) == column
or isinstance(child, Leaf) and child.column == column):
for comment in list_comments(child.prefix, is_endmarker=False):
if comment.value in FMT_ON:
return True

container = container.next_sibling
return False


def first_leaf_column(node):
for child in node.children:
if isinstance(child, Leaf):
return child.column
return None


def maybe_make_parens_invisible_in_atom(node: LN, parent: LN) -> bool:
Expand Down
31 changes: 31 additions & 0 deletions tests/data/fmtonoff4.py
@@ -0,0 +1,31 @@
# fmt: off
@test([
1, 2,
3, 4,
])
# fmt: on
def f(): pass

@test([
1, 2,
3, 4,
])
def f(): pass

# output

# fmt: off
@test([
1, 2,
3, 4,
])
# fmt: on
def f():
pass


@test(
[1, 2, 3, 4,]
)
def f():
pass
8 changes: 8 additions & 0 deletions tests/test_black.py
Expand Up @@ -632,6 +632,14 @@ def test_fmtonoff3(self) -> None:
black.assert_equivalent(source, actual)
black.assert_stable(source, actual, black.FileMode())

@patch("black.dump_to_file", dump_to_stderr)
def test_fmtonoff4(self) -> None:
source, expected = read_data("fmtonoff4")
actual = fs(source)
self.assertFormatEqual(expected, actual)
black.assert_equivalent(source, actual)
black.assert_stable(source, actual, black.FileMode())

@patch("black.dump_to_file", dump_to_stderr)
def test_remove_empty_parentheses_after_class(self) -> None:
source, expected = read_data("class_blank_parentheses")
Expand Down