# How to Disable Ruff Rules for a Block of Code


[Ruff](https://pydevtools.com/handbook/reference/ruff.md) 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.

```python
# 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:

```bash
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.

```python
# 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.

```python
# 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](https://pydevtools.com/handbook/reference/ruff.md) lists all three levels side by side.

{{< callout type="info" >}}
These comments suppress the **linter**. To stop the **formatter** from reformatting a block, see [how to disable the Ruff formatter for a block of code](https://pydevtools.com/handbook/how-to/how-to-disable-the-ruff-formatter-for-a-block-of-code.md).
{{< /callout >}}

## Learn More

- [Ruff error suppression](https://docs.astral.sh/ruff/linter/#error-suppression)
- [RUF104: unmatched-suppression-comment](https://docs.astral.sh/ruff/rules/unmatched-suppression-comment)
- [How to configure recommended Ruff defaults](https://pydevtools.com/handbook/how-to/how-to-configure-recommended-ruff-defaults.md)
- [Ruff: a complete guide](https://pydevtools.com/handbook/explanation/ruff-complete-guide.md)
