psf / black

The uncompromising Python code formatter

Home Page:https://black.readthedocs.io/en/stable/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

matejcik opened this issue · comments

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

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!