13

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.

milanbalazs
  • 4,811
  • 4
  • 23
  • 45
Srinath Ganesh
  • 2,496
  • 2
  • 30
  • 60

2 Answers2

17

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

joepitz1
  • 194
  • 2
  • 12
djangomachine
  • 619
  • 4
  • 15
  • 2
    Small correction: you need to wrap a block of code with `# fmt: off` …code-block-here… `# fmt: on` – kgadek May 19 '22 at 16:13
9

You can't customize black. From the readme:

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

Patrick
  • 674
  • 1
  • 8
  • 22