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

Use a uv project with Ruff installed. In `pyproject.toml`, merge these rules into any existing `extend-select` list before running the examples:

```toml {filename="pyproject.toml"}
[tool.ruff.lint]
extend-select = ["E501", "E741", "RUF103", "RUF104"]
```

## 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 in the same order and match the `disable` comment's indentation within the same logical block.

```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[CODE]` with no matching `# ruff: enable[CODE]` creates an implicit range and reports `RUF104` when that rule is enabled. 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. See [how to suppress a Ruff warning on one line](https://pydevtools.com/handbook/how-to/how-to-suppress-a-ruff-warning-on-one-line.md). |
| A block | `# ruff: disable[E501]` ... `# ruff: enable[E501]` | A contiguous region you cannot fix yet. |
| Whole file | `# ruff: noqa: E501` | Generated or vendored files. |

For a directory or filename pattern, use [per-file ignores](https://pydevtools.com/handbook/how-to/how-to-ignore-a-ruff-rule-for-a-directory.md).

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 >}}
