Skip to content

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

Closed
@matejcik

Description

@matejcik

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

Activity

matejcik

matejcik commented on Oct 12, 2018

@matejcik
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

ambv commented on Oct 17, 2018

@ambv
Collaborator

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

Carreau

Carreau commented on May 28, 2019

@Carreau

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.

changed the title [-]"fmt: on" does not work when crossing block boundaries[/-] [+]"fmt: on" does not work when crossing block boundaries and decorators[/+] on Aug 16, 2019
cooperlees

cooperlees commented on May 9, 2020

@cooperlees
Collaborator

PR merged. Thanks!

8 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    T: bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ambv@Carreau@matejcik@cooperlees

        Issue actions

          "fmt: on" does not work when crossing block boundaries and decorators · Issue #560 · psf/black