{"record":{"id":"8fbad7b8d0a6855e","repo":"astral-sh/ruff","slug":"expected-a-ty-file-found-a-ruff-sourcefile","errorCode":null,"errorMessage":"Expected a ty `File`, found a ruff `SourceFile`","messagePattern":"Expected a ty `File`, found a ruff `SourceFile`","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ruff_db/src/diagnostic/mod.rs","lineNumber":1277,"sourceCode":"    }\n\n    /// Returns a new `Span` with the given `range` attached to it.\n    pub fn with_range(self, range: TextRange) -> Span {\n        self.with_optional_range(Some(range))\n    }\n\n    /// Returns a new `Span` with the given optional `range` attached to it.\n    pub fn with_optional_range(self, range: Option<TextRange>) -> Span {\n        Span { range, ..self }\n    }\n\n    /// Returns the [`File`] attached to this [`Span`].\n    ///\n    /// Panics if the file is a [`UnifiedFile::Ruff`] instead of a [`UnifiedFile::Ty`].\n    pub fn expect_ty_file(&self) -> File {\n        match self.file {\n            UnifiedFile::Ty(file) => file,\n            UnifiedFile::Ruff(_) => panic!(\"Expected a ty `File`, found a ruff `SourceFile`\"),\n        }\n    }\n\n    /// Returns the [`SourceFile`] attached to this [`Span`].\n    ///\n    /// Panics if the file is a [`UnifiedFile::Ty`] instead of a [`UnifiedFile::Ruff`].\n    fn expect_ruff_file(&self) -> &SourceFile {\n        self.as_ruff_file()\n            .expect(\"Expected a ruff `SourceFile`, found a ty `File`\")\n    }\n\n    /// Returns the [`SourceFile`] attached to this [`Span`].\n    pub fn as_ruff_file(&self) -> Option<&SourceFile> {\n        match &self.file {\n            UnifiedFile::Ty(_) => None,\n            UnifiedFile::Ruff(file) => Some(file),\n        }\n    }","sourceCodeStart":1259,"sourceCodeEnd":1295,"githubUrl":"https://github.com/astral-sh/ruff/blob/d1087a4b9e03d253a88703f34e0869ee4b805456/crates/ruff_db/src/diagnostic/mod.rs#L1259-L1295","documentation":"ruff_db's Span unifies the two file kinds — UnifiedFile::Ty(File) for ty and UnifiedFile::Ruff(SourceFile) for ruff lint code — so shared diagnostic machinery can render both. expect_ty_file() unwraps the ty variant and panics when the span actually wraps a ruff SourceFile. Hitting it means a ty-only code path (ty rendering or semantic APIs) received a span produced on ruff's side.","triggerScenarios":"Calling span.expect_ty_file() (or a helper that does) on a Span built from a ruff SourceFile — typically when shared printer/renderer code in ruff_db is extended and the ruff branch flows into a ty-only function, or ruff lint diagnostics are fed into ty rendering.","commonSituations":"Refactoring the unified diagnostics layer so a path that previously only saw ty Files now also receives ruff SourceFiles; wiring ruff lint output through the same renderer as ty diagnostics.","solutions":["Branch on the variant first: inspect span.file() / as_ruff_file() and only call ty logic for the ty variant, handling the ruff case separately.","If a function genuinely requires a ty File, change its signature to accept File directly so misuse becomes a compile error instead of a runtime panic.","When touching shared rendering code, audit call sites of expect_ty_file() in the same change."],"exampleFix":"// before\nlet file = span.expect_ty_file(); // panics: Expected a ty `File`, found a ruff `SourceFile`\n\n// after\nmatch span.file() {\n    UnifiedFile::Ty(file) => render_ty(file, span.range()),\n    UnifiedFile::Ruff(source) => render_ruff(source, span.range()),\n}","handlingStrategy":"type-guard","validationCode":"debug_assert!(\n    matches!(span.file(), UnifiedFile::Ty(_)),\n    \"ty-only path received a ruff span\"\n);","typeGuard":"fn is_ty_span(span: &Span) -> bool {\n    matches!(span.file(), UnifiedFile::Ty(_))\n}","tryCatchPattern":null,"preventionTips":["Never call expect_ty_file() on spans from shared entry points; match on span.file() instead.","Type module boundaries on File (ty) or SourceFile (ruff) directly so mixing is a compile error.","Add unit tests that push both file kinds through shared rendering code."],"tags":["diagnostics","ty","ruff","panic","file-handling"],"backgroundTag":"unwrap-on-wrong-enum-variant","analyzedSha":"d1087a4b9e03d253a88703f34e0869ee4b805456","analyzedAt":"2026-08-20T16:33:49.445Z","contentChangedAt":"2026-08-20T16:33:49.445Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}