{"record":{"id":"19a95eb0c5c80f9f","repo":"astral-sh/ruff","slug":"expected-import-bindings","errorCode":null,"errorMessage":"Expected import bindings","messagePattern":"Expected import bindings","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs","lineNumber":578,"sourceCode":"    }\n}\n\n/// Generate a [`Fix`] to remove unused imports from a statement.\nfn fix_by_removing_imports<'a>(\n    checker: &Checker,\n    node_id: NodeId,\n    imports: impl Iterator<Item = &'a ImportBinding<'a>>,\n    in_init: bool,\n) -> Result<Fix> {\n    let statement = checker.semantic().statement(node_id);\n    let parent = checker.semantic().parent_statement(node_id);\n\n    let member_names: Vec<Cow<'_, str>> = imports\n        .map(|ImportBinding { import, .. }| import)\n        .map(Imported::member_name)\n        .collect();\n    if member_names.is_empty() {\n        bail!(\"Expected import bindings\");\n    }\n\n    let edit = fix::edits::remove_unused_imports(\n        member_names.iter().map(AsRef::as_ref),\n        statement,\n        parent,\n        checker.locator(),\n        checker.stylist(),\n        checker.indexer(),\n    )?;\n\n    // It's unsafe to remove things from `__init__.py` because it can break public interfaces.\n    let applicability = if in_init {\n        Applicability::Unsafe\n    } else {\n        Applicability::Safe\n    };\n","sourceCodeStart":560,"sourceCodeEnd":596,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_linter/src/rules/pyflakes/rules/unused_import.rs#L560-L596","documentation":"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.","triggerScenarios":"`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.","commonSituations":"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.","solutions":["Update Ruff; if reproducible, file an issue with the import statement","Remove the unused import manually","Ignore F401 for the statement if the import is intentional (e.g. re-export) using `# noqa: F401`"],"exampleFix":"# before (unused import)\nimport os\n# after\n# (import removed)","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Remove unused imports manually for unusual import forms","Use `# noqa: F401` for intentional re-exports","Keep import statements simple and standard"],"tags":["ruff","autofix","pyflakes","f401","unused-import"],"backgroundTag":"autofix-failed-to-apply","analyzedSha":"26f38c119cac42e4d320ba08f09224fdec74af2c","analyzedAt":"2026-09-05T10:32:37.492Z","contentChangedAt":"2026-09-05T10:32:37.492Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}