Black: "fmt: on" does not work when crossing block boundaries and decorators

Created on 12 Oct 2018  路  4Comments  路  Source: psf/black

Operating system: Linux Mint 19.0
Python version: 3.6.6
Black version: 18.9b0
Does also happen on master: yes

Simplest reproducer: the following is left unchanged. In fact, it behaves as if # fmt: off is true until end of file.

# fmt: off
if (x and y):
# fmt: on
    pass

if (x and y):
    pass

The soonest # fmt: on takes effect is at end of the indented if block.

Ran into this with a series of decorators:

# fmt: off
@click.command()
@click.option("-a", "--align",      help="aligned help texts")
@click.option("-b", "--big-option", help="should stay aligned")
# fmt: on
def foo(align,    big_option):
    pass

Simple workaround for me is keep the formatter commands _within_ a "block" - the following works perfectly fine:

@click.command()
# fmt: off
@click.option("-a", "--align",      help="aligned help texts")
@click.option("-b", "--big-option", help="should stay aligned")
# fmt: on
def foo(align, big_option):
    pass
bug

Most helpful comment

Thanks for your report, we'll tackle this for the next release.

All 4 comments

I don't know if this is even fixable in the general case.

What I find problematic, though, is that a # fmt: on directive is silently ignored. In case black can't recover from a weirdly placed # fmt: on, that should be a loud error, IMHO.

Thanks for your report, we'll tackle this for the next release.

Got same issue but for a flipped on/off:

# fmt: off
... Whatever
# fmt: on
@dec
# fmt: off
@dec2
def fun():
    g(1,2,3) #don't reformat me

def f2(a,b,c):pass

Where I only want to reformat _some_ part of the code., and all the end of the file got reformatted.

PR merged. Thanks!

Was this page helpful?
0 / 5 - 0 ratings