Skip to content

Commit a282181

Browse files
authored
Fix a crash when a colon line is marked between # fmt: off and # fmt: on (#3439)
1 parent 7d062ec commit a282181

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

CHANGES.md

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

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

13+
- Fix a crash when a colon line is marked between `# fmt: off` and `# fmt: on` (#3439)
14+
1315
### Preview style
1416

1517
<!-- Changes that affect Black's preview style -->

src/black/comments.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ def generate_ignored_nodes(
232232

233233
# fix for fmt: on in children
234234
if children_contains_fmt_on(container, preview=preview):
235-
for child in container.children:
235+
for index, child in enumerate(container.children):
236236
if isinstance(child, Leaf) and is_fmt_on(child, preview=preview):
237237
if child.type in CLOSING_BRACKETS:
238238
# This means `# fmt: on` is placed at a different bracket level
@@ -241,6 +241,16 @@ def generate_ignored_nodes(
241241
# The alternative is to fail the formatting.
242242
yield child
243243
return
244+
if (
245+
child.type == token.INDENT
246+
and index < len(container.children) - 1
247+
and children_contains_fmt_on(
248+
container.children[index + 1], preview=preview
249+
)
250+
):
251+
# This means `# fmt: on` is placed right after an indentation
252+
# level, and we shouldn't swallow the previous INDENT token.
253+
return
244254
if children_contains_fmt_on(child, preview=preview):
245255
return
246256
yield child

tests/data/simple_cases/fmtonoff5.py

+20-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ async def call(param):
6464
print ( "This will be formatted" )
6565

6666

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

7777

78+
# Regression test for https://github.com/psf/black/issues/3436.
79+
if x:
80+
return x
81+
# fmt: off
82+
elif unformatted:
83+
# fmt: on
84+
will_be_formatted ()
85+
86+
7887
# output
7988

8089

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

146155

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

158167
# fmt: on
168+
169+
170+
# Regression test for https://github.com/psf/black/issues/3436.
171+
if x:
172+
return x
173+
# fmt: off
174+
elif unformatted:
175+
# fmt: on
176+
will_be_formatted()

0 commit comments

Comments
 (0)