Black: # fmt: pass - ignore formatting for single statements

Created on 18 Nov 2019  路  5Comments  路  Source: psf/black

Feature request:
# fmt: pass would skip formatting for the single statement that immediately follows.

Is your feature request related to a problem? Please describe.

Not a response to a problem but it might be helpful for the following reasons:

  • Easier to use / less typing
  • Can't forget to turn it back on

    • Or accidentally insert code you do want formatted into an unformatted block

  • Encourages ignoring less code

    • One-offs are always isolated to one statement instead of potentially large chunks of code.

Describe the solution you'd like

z = "hello " + "world"

# fmt: pass
custom_formatting = [
    0,  1,  2,
    3,  4,  5,
    6,  7,  8,
]

regular_formatting = [
   0,  1,  2,  'this will be formatted'
]

The custom_formatting list would be ignored, everything else would be formatted.

Describe alternatives you've considered

  1. Current solution
# fmt: off
ugly_code=('but I want it this way')
even_more=('dont\'t judge me')
# fmt: on
  1. #fmt: off at end of line (#790)
    I believe # fmt: pass addresses the same concern while also being a bit more flexible.

    • a single statement could be multi-line

    • a long line would bury an inline comment

    • # fmt: pass is more readable & predictable as a separate line

  2. Disable formatting for certain expressions only. #798
  3. Deal with it

Additional context

Naming:

  • # fmt: pass reflects Python's existing pass statement. Hopefully this hints that you can use it to temporarily appease the formatter for one quick thing, but it's not something you want everywhere.

  • Naming could be anything, e.g. "pass", "skip", "ignore"

enhancement

Most helpful comment

I like this one..

but would like to extend it a bit more

foo = bar.<100 characters +> # fmt: pass

if i place at the end of the statement - it should work as well (not only before the statement)

All 5 comments

I like this one..

but would like to extend it a bit more

foo = bar.<100 characters +> # fmt: pass

if i place at the end of the statement - it should work as well (not only before the statement)

I would love to see this feature merged!

I like this one..

but would like to extend it a bit more

foo = bar.<100 characters +> # fmt: pass

if i place at the end of the statement - it should work as well (not only before the statement)

Expanding on @vitalik's ask. It would be great if this feature would be compatible with other inline comment options.

def example(self): #noqa - squelch some lint check # fmt: pass

The reason why I uninstalled black :(

We definitly need something like this.

I have ansible vars in my python script
which require me to write something like
{{ variable_name }} which black will turn into {{variable_name}}
which afaik isnt valid anymore.

turning the line into

# fmt: off
my_var = {{ variable_name }}
# fmt: on

looks very ugly in my opinion.
this however:
my_var = {{ variable_name }} # fmt: skip
would look quite okay.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kindjacket picture kindjacket  路  21Comments

mhham picture mhham  路  22Comments

ambv picture ambv  路  51Comments

underyx picture underyx  路  22Comments

lig picture lig  路  84Comments