Black (Python) Ignore Rule
Asked Answered
A

2

15

I feel Black is doing something not compliant (with my Organisation), so I am trying to ignore certain rules.

Example below and a related link

PEP 8: whitespace before ':'

My Organisation (Coding Standards) does not give priority to what Black feels is right, but wants a way to customise black configurations.

I dont see any mentioned of Ignoring a Rule in Black documentation https://github.com/psf/black#command-line-options.

They have given examples to ignore Flake8 rules, but dont seem to have any documentation for their own product.

Applaud answered 12/2, 2020 at 13:25 Comment(0)
G
12

You can't customize black. From the readme:

Black reformats entire files in place. It is not configurable.

Goodkin answered 12/2, 2020 at 13:59 Comment(1)
The Uncompromising Code Formatter. Indeed.Intermarriage
C
21

While you can't cherry-pick certain rules to disable, you can skip formatting for individual lines (using # fmt: skip at the end of the line), or for blocks of code (start with # fmt: off and end with # fmt: on)

https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html#code-style

And if using PyCharm, here's a guide for skipping certain lines without using the fmt comments: https://godatadriven.com/blog/partial-python-code-formatting-with-black-pycharm/

Edit: implement @kgadek correction

Camarillo answered 21/11, 2021 at 15:19 Comment(2)
Small correction: you need to wrap a block of code with # fmt: off …code-block-here… # fmt: onPhytophagous
You can also use black as library and compile it. Like that : ` def format_python_code_black(code: str) -> str: """ Format python code using black. """ return black.format_str(code, mode=black.FileMode()) `Puncheon
G
12

You can't customize black. From the readme:

Black reformats entire files in place. It is not configurable.

Goodkin answered 12/2, 2020 at 13:59 Comment(1)
The Uncompromising Code Formatter. Indeed.Intermarriage

© 2022 - 2024 — McMap. All rights reserved.