Fix a crash when a colon line is marked between # fmt: off
and # fmt: on
(#3439)
This commit is contained in:
parent
7d062ecd5f
commit
a2821815af
@ -10,6 +10,8 @@
|
|||||||
|
|
||||||
<!-- Changes that affect Black's stable style -->
|
<!-- 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
|
### Preview style
|
||||||
|
|
||||||
<!-- Changes that affect Black's preview style -->
|
<!-- Changes that affect Black's preview style -->
|
||||||
|
@ -232,7 +232,7 @@ def generate_ignored_nodes(
|
|||||||
|
|
||||||
# fix for fmt: on in children
|
# fix for fmt: on in children
|
||||||
if children_contains_fmt_on(container, preview=preview):
|
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 isinstance(child, Leaf) and is_fmt_on(child, preview=preview):
|
||||||
if child.type in CLOSING_BRACKETS:
|
if child.type in CLOSING_BRACKETS:
|
||||||
# This means `# fmt: on` is placed at a different bracket level
|
# This means `# fmt: on` is placed at a different bracket level
|
||||||
@ -241,6 +241,16 @@ def generate_ignored_nodes(
|
|||||||
# The alternative is to fail the formatting.
|
# The alternative is to fail the formatting.
|
||||||
yield child
|
yield child
|
||||||
return
|
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):
|
if children_contains_fmt_on(child, preview=preview):
|
||||||
return
|
return
|
||||||
yield child
|
yield child
|
||||||
|
@ -64,7 +64,7 @@ async def call(param):
|
|||||||
print ( "This will be formatted" )
|
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):
|
class Named(t.Protocol):
|
||||||
# fmt: off
|
# fmt: off
|
||||||
@property
|
@property
|
||||||
@ -75,6 +75,15 @@ def this_will_be_formatted ( self, **kwargs ) -> Named: ...
|
|||||||
# fmt: on
|
# 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
|
# output
|
||||||
|
|
||||||
|
|
||||||
@ -144,7 +153,7 @@ async def call(param):
|
|||||||
print("This will be formatted")
|
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):
|
class Named(t.Protocol):
|
||||||
# fmt: off
|
# fmt: off
|
||||||
@property
|
@property
|
||||||
@ -156,3 +165,12 @@ def this_will_be_formatted(self, **kwargs) -> Named:
|
|||||||
...
|
...
|
||||||
|
|
||||||
# fmt: on
|
# 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()
|
||||||
|
Loading…
Reference in New Issue
Block a user