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

Exclude formatter from running on specific lines with inline comment #451

Closed
jakerye opened this issue Aug 14, 2018 · 13 comments
Closed

Exclude formatter from running on specific lines with inline comment #451

jakerye opened this issue Aug 14, 2018 · 13 comments

Comments

@jakerye
Copy link

jakerye commented Aug 14, 2018

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?

@JelleZijlstra
Copy link
Collaborator

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

@jakerye
Copy link
Author

jakerye commented Aug 14, 2018

Excellent, thank you!

@jakerye jakerye closed this as completed Aug 14, 2018
@datarods-svc
Copy link

@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

@eric-wieser
Copy link

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

@datarods-svc
Copy link

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

@dandavison
Copy link
Contributor

dandavison commented Feb 21, 2020

Ref #790 (comment) which discusses the desirability of an inline comment toggle and has a clever workaround for setting pdb breakpoints.

@contang0
Copy link

Doesn't work when used on jupyter magic commands:

%matplotlib inline # fmt: off

@geier1993
Copy link

geier1993 commented Jun 2, 2021

@juliuscrn
Magic commands are also discussed here:

#774

@AustinScola
Copy link

For anyone who stumbles upon this GitHub Issue: it looks like Black now supports skipping a single line by adding # fmt: skip at the end of a line:

foo = 1  # fmt: skip

@ricardo-dematos

This comment was marked as off-topic.

@ricardo-dematos
Copy link

ricardo-dematos commented Dec 19, 2022

Found out that leaving just #fmt: skip as the solely comment in the end of the line, black honors the directive.

@FSpanhel
Copy link

FSpanhel commented Jan 4, 2023

Found out that leaving just #fmt:skip as the solely comment in the end of the line, black honors the directive.

This is also my observation in #3450.

@HydrogenSulfate
Copy link

For anyone who stumbles upon this GitHub Issue: it looks like Black now supports skipping a single line by adding # fmt: skip at the end of a line:

foo = 1  # fmt: skip

Only this indeed works rather than # fmt: off or # fmt: no with black 23.12.0, thanks a lot.

@psf psf locked as resolved and limited conversation to collaborators Mar 4, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests