{"record":{"id":"d1511936aad452f0","repo":"astral-sh/ruff","slug":"expected-stmt-import-stmt-importfrom-d15119","errorCode":null,"errorMessage":"Expected Stmt::Import | Stmt::ImportFrom","messagePattern":"Expected Stmt::Import \\| Stmt::ImportFrom","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ruff_linter/src/rules/isort/annotate.rs","lineNumber":173,"sourceCode":"                        trailing.push(comment);\n                    }\n\n                    AnnotatedImport::ImportFrom {\n                        module: module.as_ref().map(|module| locator.slice(module)),\n                        names: aliases,\n                        level: *level,\n                        is_lazy: *is_lazy,\n                        trailing_comma: if split_on_trailing_comma {\n                            trailing_comma(import, tokens)\n                        } else {\n                            TrailingComma::default()\n                        },\n                        atop,\n                        inline,\n                        trailing,\n                    }\n                }\n                _ => panic!(\"Expected Stmt::Import | Stmt::ImportFrom\"),\n            }\n        })\n        .collect()\n}\n","sourceCodeStart":155,"sourceCodeEnd":178,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_linter/src/rules/isort/annotate.rs#L155-L178","documentation":"Internal panic in Ruff's isort rule annotation pass. `annotate_imports` walks statements that the import-block scanner has already classified as import statements, and only matches `Stmt::Import` and `Stmt::ImportFrom`; any other statement reaching it is considered impossible. The panic means the statement filter upstream produced a non-import statement — a Ruff bug or an internal API misuse.","triggerScenarios":"Calling `format_imports`/`annotate_imports` with a block of statements containing a non-import statement (e.g. because the traversal handed an `Expr` or `Assign` into the import annotator). Not triggerable by normal `ruff check` input unless there is a classification regression in the isort block scanner.","commonSituations":"Custom distributions of Ruff where a rule or transformer inserts/permutes statements before isort processing; a Ruff upgrade after which import-block boundaries are computed incorrectly; fuzzed/obfuscated Python that breaks block classification.","solutions":["Upgrade Ruff to the latest release and re-run; if it persists, minimize the file with `ruff check --isolated --select I001 <file>` and file an issue","Run with `--no-fix` or disable isort (`ignore = [\"I001\"]`) to continue linting while avoiding the crashing path","If you drive the isort APIs from Rust, ensure the statement list passed to `annotate_imports` contains only `Stmt::Import`/`Stmt::ImportFrom` (filter or match before calling)","Check for plugins/forks that modify the AST before sorting and remove them"],"exampleFix":"// before (caller side, Rust)\nformat_imports(stmts.iter(), ...)\n\n// after\nclassifier.filter(stmts.iter().filter(|stmt| matches!(stmt, Stmt::Import(_) | Stmt::ImportFrom(_))))\nformat_imports(filtered, ...)","handlingStrategy":"validation","validationCode":"// Rust: filter to import statements before annotating\nlet imports: Vec<&Stmt> = stmts\n    .iter()\n    .filter(|s| matches!(s, Stmt::Import(_) | Stmt::ImportFrom(_)))\n    .collect();","typeGuard":"fn is_import_stmt(stmt: &Stmt) -> bool {\n    matches!(stmt, Stmt::Import(_) | Stmt::ImportFrom(_))\n}","tryCatchPattern":"// isort runs in the linter process; guard the whole check invocation\nlet result = std::panic::catch_unwind(|| ruff_check_isort(path))\n    .map_err(|_| \"isort annotation panicked\");","preventionTips":["Never insert non-import statements into blocks passed to isort annotation APIs","Avoid AST-mutating plugins/forks that run before import sorting","Run `ruff check --select I001` on new Ruff versions in CI before rolling out","Report crashes with `--isolated` reproductions upstream"],"tags":["rust","panic","ruff","isort","internal-invariant"],"backgroundTag":"linter-internal-panic","analyzedSha":"26f38c119cac42e4d320ba08f09224fdec74af2c","analyzedAt":"2026-09-05T10:32:37.492Z","contentChangedAt":"2026-09-05T10:32:37.492Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}