How to Disable Ruff Rules for a Block of Code
Ruff suppresses a rule on a single line with # noqa, but a legacy region that trips the same rule on twenty consecutive lines needs twenty comments. Range suppression comments fix this: # ruff: disable[CODE] and # ruff: enable[CODE] quarantine a contiguous block while you adopt Ruff incrementally, without touching every line or ignoring the whole file.
This works in Ruff 0.15.0 and later, where range suppression and its validation rules (RUF103, RUF104) became stable.
Wrap the Block in disable and enable Comments
Put a # ruff: disable[CODE] comment on its own line before the block and a matching # ruff: enable[CODE] after it. Ruff suppresses the listed rule for every line in between.
# ruff: disable[E501]
SQL_TEMPLATE = "SELECT id, name, email, created_at, updated_at FROM users WHERE tenant_id = %s AND status = 'active' ORDER BY created_at DESC LIMIT 100"
LOG_FORMAT = "%(asctime)s %(levelname)-8s %(name)s:%(lineno)d %(message)s request_id=%(request_id)s trace_id=%(trace_id)s"
# ruff: enable[E501]Run the linter to confirm the block is clean:
uv run ruff check .The two long lines no longer report E501. Any line outside the disable/enable pair still does.
Suppress Several Rules in One Range
List the codes inside the brackets, separated by commas. The enable comment must repeat the same codes to close the range.
# ruff: disable[E501, E741]
l = "SELECT id, name, email, created_at FROM accounts WHERE tenant_id = %s AND status = 'active'"
# ruff: enable[E501, E741]Here E741 (ambiguous variable name l) and E501 (line too long) are both suppressed for the block. Range comments require at least one code, so there is no blanket form that silences every rule. A bare # ruff: disable reports RUF103 and is ignored.
Close Every Range You Open
A # ruff: disable with no matching # ruff: enable does not error out. It suppresses the listed rules to the end of the current indentation scope, which at module level is the rest of the file.
# ruff: disable[E501]
TEMPLATE = "a very long line that exceeds the configured line length limit and then some more"
# Forgetting the enable comment suppresses E501 for everything below, too.Ruff catches this with RUF104 (unmatched-suppression-comment), which flags any disable that lacks a closing enable. Keep RUF104 enabled so an unbalanced comment fails the lint run instead of silently widening the suppressed region.
Pick the Right Suppression Scope
Match the comment to how much code the rule should ignore:
| Scope | Syntax | Use when |
|---|---|---|
| One line | # noqa: E501 |
A single violation on one line. |
| A block | # ruff: disable[E501] … # ruff: enable[E501] |
A contiguous region you cannot fix yet. |
| Whole file | # ruff: noqa: E501 |
Generated or vendored files. |
The Ruff reference lists all three levels side by side.