astral-sh/ruff · error

expected python code

Error message

expected python code

What it means

Panics in SourceKind::expect_python because the wrapped source is a Markdown document or an ipynb notebook rather than Python source. The method encodes an assumption that the caller already knows which variant it holds; using it on another kind is a programming error.

Source

Thrown at crates/ruff_linter/src/source_kind.rs:61

        match self {
            SourceKind::Python { code, .. } => Some(code),
            SourceKind::Markdown(_) => None,
            SourceKind::IpyNotebook(_) => None,
        }
    }

    pub fn as_markdown(&self) -> Option<&str> {
        match self {
            SourceKind::Markdown(code) => Some(code),
            SourceKind::Python { .. } => None,
            SourceKind::IpyNotebook(_) => None,
        }
    }

    pub fn expect_python(self) -> String {
        match self {
            SourceKind::Python { code, is_stub: _ } => code,
            _ => panic!("expected python code"),
        }
    }

    pub fn expect_ipy_notebook(self) -> Notebook {
        match self {
            SourceKind::IpyNotebook(notebook) => *notebook,
            _ => panic!("expected ipy notebook"),
        }
    }

    pub fn expect_markdown(self) -> String {
        match self {
            SourceKind::Markdown(code) => code,
            _ => panic!("expected markdown text"),
        }
    }

    pub fn py_source_type(&self) -> PySourceType {

View on GitHub (pinned to 26f38c119c)

Solutions

  1. Check as_python() (or match on the variant) before calling expect_python
  2. Pass the Python source through a code path that guarantees the Python variant
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at crates/ruff_linter/src/source_kind.rs:61 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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