Black formatter - Ignore specific multi-line code
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Black is intentionally opinionated: it formats entire files so teams do not argue about style. That design means you usually cannot tell Black to leave one complicated expression alone unless you opt out at the line or block level.
What Black Can and Cannot Ignore
Black is not built around many custom formatting rules. In practice, you get three useful choices:
- Let Black format the code normally.
- Skip one line with
# fmt: skip. - Skip a whole region with
# fmt: offand# fmt: on.
That is the important mental model. If you want Black to preserve a hand-aligned multi-line expression inside an otherwise normal block, there is no fine-grained setting for that pattern. You either accept Black's output or mark a line or region as excluded.
Here is a small example. Black will normally collapse and re-wrap this call according to its own rules:
If you truly need to preserve a custom layout, wrap the region with formatter directives:
Black will leave that block alone when it runs.
Using # fmt: skip for a Single Line
For isolated cases, # fmt: skip is the smallest escape hatch. It is useful when a single assignment or literal is clearer in a custom layout and you do not want to exclude a larger block.
Be careful with this pattern. It only applies to the line where the comment appears. If the code spans several physical lines, # fmt: skip is usually not enough, and a fmt: off block is clearer.
Using # fmt: off and # fmt: on for Multi-Line Code
A block directive is the standard answer for generated code, carefully aligned tables, or rare formatting that communicates structure better than Black's default output.
This approach works well when the layout itself carries meaning. It should still be rare. Once a file contains many ignored regions, the team loses the consistency Black was meant to provide.
Prefer Refactoring Over Opting Out
In many cases, the need to preserve formatting is a hint that the code should be reorganized. Black often exposes overly dense expressions, long argument lists, or data that belongs in a separate structure.
For example, instead of freezing a hard-to-read constructor call, move the data into a dictionary or helper variable:
This is usually easier to maintain than protecting a custom arrangement with formatter directives.
You can also configure Black at the project level for the parts it does support, such as line length:
A slightly wider line length sometimes removes the urge to bypass formatting in the first place.
When Ignoring Code Is Reasonable
A small number of exceptions are defensible:
- Generated code that should not be edited by hand.
- Data tables where vertical alignment improves scanning.
- Embedded examples copied from documentation or standards.
- Temporary migrations where readability depends on stable column layout.
Even then, keep the ignored region narrow. A short protected block is easier to review than an entire file hidden behind fmt: off.
Common Pitfalls
The most common mistake is expecting Black to support selective formatting rules like "leave this one multi-line call unchanged." It does not work that way. The fix is to use # fmt: off and # fmt: on, or to restructure the code so Black's output is acceptable.
Another problem is forgetting to turn formatting back on. If # fmt: on is missing, a much larger part of the file may stop being formatted. Keep the off-region as short as possible and review the diff carefully.
Teams also overuse formatter escapes for style preferences that do not matter. If the code is not generated and the layout does not carry real meaning, accepting Black's default output is usually the better choice.
Summary
- Black does not offer detailed per-pattern ignore rules for multi-line code.
- Use
# fmt: skipfor a single line and# fmt: offwith# fmt: onfor a block. - Prefer refactoring dense code before opting out of formatting.
- Keep ignored regions small so the rest of the file stays consistent.
- Project settings like line length can reduce the need for formatter escapes.
Related reading
- Book for Django + Celery + RabbitMQ?
- Boring Factorials in python
- boto3 client NoRegionError You must specify a region error only sometimes
- boto3 client NoRegionError You must specify a region error only sometimes
- boto3 equivalent to boto.utils.get_instance_metadata?
- Boto3 get credentials dynamically?
- Boto3 grabbing only selected objects from the S3 resource
- boto3 how to create object with metadata?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.