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

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

Closed
matejcik opened this issue Oct 12, 2018 · 4 comments
Closed
Labels
T: bug Something isn't working

Comments

@matejcik
Copy link

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
@matejcik
Copy link
Author

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.

@ambv
Copy link
Collaborator

ambv commented Oct 17, 2018

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

@ambv ambv added the T: bug Something isn't working label Oct 17, 2018
@Carreau
Copy link

Carreau commented May 28, 2019

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.

@cooperlees
Copy link
Collaborator

PR merged. Thanks!

nyanpasu64 added a commit to corrscope/corrscope that referenced this issue Jun 30, 2021
The bug causing me to turn off Black formatting for the Click main
function (psf/black#560) has been resolved.
nyanpasu64 added a commit to corrscope/corrscope that referenced this issue Jun 30, 2021
The bug causing me to turn off Black formatting for the Click main
function (psf/black#560) has been resolved.
nyanpasu64 added a commit to corrscope/corrscope that referenced this issue Jun 30, 2021
The bug causing me to turn off Black formatting for the Click main
function (psf/black#560) has been resolved.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants