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 an infinite loop when using #fmt: on/off in the middle of an expression or code block #3158

Merged
merged 9 commits into from Jul 20, 2022
3 changes: 3 additions & 0 deletions CHANGES.md
Expand Up @@ -10,6 +10,9 @@

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

- Fix an infinite loop when using `#fmt: on/off` in the middle of an expression or code
yilei marked this conversation as resolved.
Show resolved Hide resolved
block (#3158)

### Preview style

<!-- Changes that affect Black's preview style -->
Expand Down
1 change: 1 addition & 0 deletions src/black/comments.py
Expand Up @@ -241,6 +241,7 @@ def generate_ignored_nodes(
if contains_fmt_on_at_column(child, leaf.column, preview=preview):
return
yield child
return
else:
yield container
container = container.next_sibling
Expand Down
37 changes: 37 additions & 0 deletions tests/data/simple_cases/fmtonoff5.py
@@ -0,0 +1,37 @@
# Regression test for https://github.com/psf/black/issues/3129.
setup(
entry_points={
# fmt: off
"console_scripts": [
"foo-bar"
"=foo.bar.:main",
# fmt: on
]
},
)


# Regression test for https://github.com/psf/black/issues/3129.
run(
# fmt: off
[
"ls",
"-la",
]
# fmt: on
+ path
,
ichard26 marked this conversation as resolved.
Show resolved Hide resolved
check=True,
)


# Regression test for https://github.com/psf/black/issues/3026.
def test_func():
# yapf: disable
if a:
return True
# yapf: enable
elif b:
return True

return False