Black: Exclude formatter from running on specific lines with inline comment

Created on 14 Aug 2018  路  7Comments  路  Source: psf/black

Some of my code is just over the border of the line limit. Here is an example:

self.parser.add_argument("--co2", action="store_true", help="read co2")
self.parser.add_argument("--mode", type=int, help="set device mode 1-4")
self.parser.add_argument(
    "--status", action="store_true", help="read status register"
)
self.parser.add_argument(
    "--error", action="store_true", help="read error register"
)
self.parser.add_argument("--reset", action="store_true", help="resets sensor")

I'd really like it to look like this (or equivalent):

self.parser.add_argument("--co2", action="store_true", help="read co2")
self.parser.add_argument("--mode", type=int, help="set device mode 1-4")
self.parser.add_argument("--status", action="store_true", help="read status register") # nofmt
self.parser.add_argument("--error", action="store_true", help="read error register")  # nofmt
self.parser.add_argument("--reset", action="store_true", help="resets sensor")  # nofmt

Is this something that aligns with the black ideology?

Most helpful comment

@jakerye That doesn't work inline as the title of the issue required. I don't want to have to add two new lines if my goal is for black to ignore a single line. # fmt: off should really work inline too as in the example below:

self.parser.add_argument("--co2", action="store_true", help="read co2")  # fmt: off

All 7 comments

You can use # fmt: off and # fmt: on.

Excellent, thank you!

@jakerye That doesn't work inline as the title of the issue required. I don't want to have to add two new lines if my goal is for black to ignore a single line. # fmt: off should really work inline too as in the example below:

self.parser.add_argument("--co2", action="store_true", help="read co2")  # fmt: off

@ab-w: fmt: no might be clearer than fmt: off, to distinguish toggling formatting and disabling it for just one line

@eric-wieser It would be, but I don't see it as being necessary at all. As a case in point, consider pylint. It lets me disable analysis either for a line or for a block, both with the same pylint: disable=foo,bar comment. I don't see a need to add complexity for the user as it's already quite clear to the user. If we wanted to be redundantly clearer, we would for instance use a language that used braces instead of indents, or with mandated semicolons at the end of lines, but we don't.

Ref https://github.com/psf/black/issues/790#issuecomment-481154453 which discusses the desirability of an inline comment toggle and has a clever workaround for setting pdb breakpoints.

Doesn't work when used on jupyter magic commands:

%matplotlib inline # fmt: off

Was this page helpful?
0 / 5 - 0 ratings