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 a crash when a colon line is marked between # fmt: off and # fmt: on #3439

Merged
merged 2 commits into from Dec 15, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Expand Up @@ -10,6 +10,8 @@

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

- Fix a crash when a colon line is marked between `# fmt: off` and `# fmt: on` (#3439)

### Preview style

<!-- Changes that affect Black's preview style -->
Expand Down
12 changes: 11 additions & 1 deletion src/black/comments.py
Expand Up @@ -232,7 +232,7 @@ def generate_ignored_nodes(

# fix for fmt: on in children
if children_contains_fmt_on(container, preview=preview):
for child in container.children:
for index, child in enumerate(container.children):
if isinstance(child, Leaf) and is_fmt_on(child, preview=preview):
if child.type in CLOSING_BRACKETS:
# This means `# fmt: on` is placed at a different bracket level
Expand All @@ -241,6 +241,16 @@ def generate_ignored_nodes(
# The alternative is to fail the formatting.
yield child
return
if (
child.type == token.INDENT
and index < len(container.children) - 1
and children_contains_fmt_on(
container.children[index + 1], preview=preview
)
):
# This means `# fmt: on` is placed right after an indentation
# level, and we shouldn't swallow the previous INDENT token.
return
if children_contains_fmt_on(child, preview=preview):
return
yield child
Expand Down
22 changes: 20 additions & 2 deletions tests/data/simple_cases/fmtonoff5.py
Expand Up @@ -64,7 +64,7 @@ async def call(param):
print ( "This will be formatted" )


# Regression test for https://github.com/psf/black/issues/2985
# Regression test for https://github.com/psf/black/issues/2985.
class Named(t.Protocol):
# fmt: off
@property
Expand All @@ -75,6 +75,15 @@ def this_will_be_formatted ( self, **kwargs ) -> Named: ...
# fmt: on


# Regression test for https://github.com/psf/black/issues/3436.
if x:
return x
# fmt: off
elif unformatted:
# fmt: on
will_be_formatted ()


# output


Expand Down Expand Up @@ -144,7 +153,7 @@ async def call(param):
print("This will be formatted")


# Regression test for https://github.com/psf/black/issues/2985
# Regression test for https://github.com/psf/black/issues/2985.
class Named(t.Protocol):
# fmt: off
@property
Expand All @@ -156,3 +165,12 @@ def this_will_be_formatted(self, **kwargs) -> Named:
...

# fmt: on


# Regression test for https://github.com/psf/black/issues/3436.
if x:
return x
# fmt: off
elif unformatted:
# fmt: on
will_be_formatted()