astral-sh/ruff · error

Expected import bindings

Error message

Expected import bindings

What it means

The F401 (unused import) fixer that removes or rewrites imports requires at least one binding name to operate on. When `fix_by_removing_imports` is handed an empty set of import bindings, there is nothing to remove, so it bails rather than emitting a no-op or wrong edit.

Source

Thrown at crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs:578

    }
}

/// Generate a [`Fix`] to remove unused imports from a statement.
fn fix_by_removing_imports<'a>(
    checker: &Checker,
    node_id: NodeId,
    imports: impl Iterator<Item = &'a ImportBinding<'a>>,
    in_init: bool,
) -> Result<Fix> {
    let statement = checker.semantic().statement(node_id);
    let parent = checker.semantic().parent_statement(node_id);

    let member_names: Vec<Cow<'_, str>> = imports
        .map(|ImportBinding { import, .. }| import)
        .map(Imported::member_name)
        .collect();
    if member_names.is_empty() {
        bail!("Expected import bindings");
    }

    let edit = fix::edits::remove_unused_imports(
        member_names.iter().map(AsRef::as_ref),
        statement,
        parent,
        checker.locator(),
        checker.stylist(),
        checker.indexer(),
    )?;

    // It's unsafe to remove things from `__init__.py` because it can break public interfaces.
    let applicability = if in_init {
        Applicability::Unsafe
    } else {
        Applicability::Safe
    };

View on GitHub (pinned to 26f38c119c)

Solutions

  1. Update Ruff; if reproducible, file an issue with the import statement
  2. Remove the unused import manually
  3. Ignore F401 for the statement if the import is intentional (e.g. re-export) using `# noqa: F401`

Example fix

# before (unused import)
import os
# after
# (import removed)
Defensive patterns

Strategy: try-catch

Prevention

When it happens

Trigger: `unused_import` delegated to `fix_by_removing_imports`, and the collected `imports` iterator produced zero `ImportBinding`s — e.g. the import statement's bindings were all filtered out before fix generation.

Common situations: Running `ruff --fix` on imports with unusual forms (star imports, `__all__` interactions) or edge-case module layouts; often indicates a fixer-path bug for that import shape.

Related errors


AI-assisted analysis of astral-sh/ruff@26f38c119c (2026-09-05). Data as JSON: /api/errors/19a95eb0c5c80f9f. Report an issue: GitHub.