To make Black ignore a specific portion of your code, you can use the # fmt: off
and # fmt: on
comments.
Follow these steps:
- Add
# fmt: off
before the section you want Black to ignore. - Write your code (the portion you don’t want Black to format).
- Add
# fmt: on
after the section you want Black to ignore.
Example:
import numpy as np
# fmt: off
matrix = np.array([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
])
# fmt: on
def some_function():
pass
In this example, the matrix array will not be formatted by Black, while the rest of the code will follow Black’s formatting rules.