{"record":{"id":"e8aa222393fdc313","repo":"astral-sh/ruff","slug":"cannot-insert-type-checking-block-inline","errorCode":null,"errorMessage":"Cannot insert `TYPE_CHECKING` block inline","messagePattern":"Cannot insert `TYPE_CHECKING` block inline","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ruff_linter/src/importer/mod.rs","lineNumber":514,"sourceCode":"            comma: aliases.last().and_then(|alias| alias.comma.clone()),\n        });\n        Ok(Edit::range_replacement(\n            statement.codegen_stylist(self.stylist),\n            stmt.range(),\n        ))\n    }\n\n    /// Add a `TYPE_CHECKING` block to the given module.\n    fn add_type_checking_block(&self, content: &str, at: TextSize) -> Result<Edit> {\n        let insertion = if let Some(stmt) = self.preceding_import(at) {\n            // Insert after the last top-level import.\n            Insertion::end_of_statement(stmt, self.source, self.stylist)\n        } else {\n            // Insert at the start of the file.\n            Insertion::start_of_file(self.python_ast, self.source, self.stylist, None)\n        };\n        if insertion.is_inline() {\n            Err(anyhow::anyhow!(\n                \"Cannot insert `TYPE_CHECKING` block inline\"\n            ))\n        } else {\n            Ok(insertion.into_edit(content))\n        }\n    }\n\n    /// Add an import statement to an existing `TYPE_CHECKING` block.\n    fn add_to_type_checking_block(&self, content: &str, at: TextSize) -> Edit {\n        Insertion::start_of_block(at, self.source, self.stylist, self.tokens).into_edit(content)\n    }\n\n    /// Return the import statement that precedes the given position, if any.\n    fn preceding_import(&self, at: TextSize) -> Option<&'a Stmt> {\n        self.runtime_imports\n            .partition_point(|stmt| stmt.start() < at)\n            .checked_sub(1)\n            .map(|idx| self.runtime_imports[idx])","sourceCodeStart":496,"sourceCodeEnd":532,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_linter/src/importer/mod.rs#L496-L532","documentation":"The importer builds an edit to insert an `if TYPE_CHECKING:` block plus the typing import when a fix moves an import into a type-checking block. If the computed insertion point is inline (attached to an existing statement rather than start-of-file or a standalone line), the fixer cannot place the block correctly and returns this error instead of producing a broken edit.","triggerScenarios":"A fix using `TypingImportEdit::add_type_checking_block` where the target file's context yields an inline insertion (e.g. the file starts mid-statement or the only insertion point is inline).","commonSituations":"Files beginning with a line-continuation/backslash continuation, obscure start-of-file constructs, or autofix attempts on syntactically unusual prologues where a top-level block cannot be inserted cleanly.","solutions":["Apply the fix without requiring the TYPE_CHECKING block (disable the specific rule or fix) and move the import manually.","Normalize the file so it starts with plain statements (remove exotic continuation constructs at the top) and re-run `ruff check --fix`.","Manually add `from typing import TYPE_CHECKING` and the `if TYPE_CHECKING:` block, then re-run to let other fixes apply."],"exampleFix":"// before (file starts with a backslash continuation; fixer cannot insert inline)\n\\\nimport foo\n\n// after (manual restructure so the block can be inserted)\nfrom typing import TYPE_CHECKING\nif TYPE_CHECKING:\n    import foo","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"match fixer.apply_fix() {\n    Err(e) if e.to_string().contains(\"Cannot insert `TYPE_CHECKING` block inline\") => {\n        eprintln!(\"Apply this fix manually or restructure the file's top statements\");\n    }\n    other => other?,\n}","preventionTips":["Keep files starting with plain top-level statements so import fixes have a clean insertion point.","Avoid backslash/continuation constructs at the start of files.","Add `from typing import TYPE_CHECKING` blocks manually when autofix reports this error."],"tags":["autofix","imports","typing","ruff"],"backgroundTag":"autofix-cannot-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"}