{"record":{"id":"cc5e6814fd045a7f","repo":"astral-sh/ruff","slug":"expected-scopekind-function","errorCode":null,"errorMessage":"Expected ScopeKind::Function","messagePattern":"Expected ScopeKind::Function","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ruff_linter/src/rules/pep8_naming/rules/invalid_first_argument_name.rs","lineNumber":213,"sourceCode":"\n    const fn rule(self) -> Rule {\n        match self {\n            Self::Method => Rule::InvalidFirstArgumentNameForMethod,\n            Self::ClassMethod => Rule::InvalidFirstArgumentNameForClassMethod,\n        }\n    }\n}\n\n/// N804, N805\npub(crate) fn invalid_first_argument_name(checker: &Checker, scope: &Scope) {\n    let ScopeKind::Function(ast::StmtFunctionDef {\n        name,\n        parameters,\n        decorator_list,\n        ..\n    }) = &scope.kind\n    else {\n        panic!(\"Expected ScopeKind::Function\")\n    };\n\n    let semantic = checker.semantic();\n\n    let Some(parent_scope) = semantic.first_non_type_parent_scope(scope) else {\n        return;\n    };\n\n    let ScopeKind::Class(parent) = parent_scope.kind else {\n        return;\n    };\n\n    let function_type = match function_type::classify(\n        name,\n        decorator_list,\n        parent_scope,\n        semantic,\n        &checker.settings().pep8_naming.classmethod_decorators,","sourceCodeStart":195,"sourceCodeEnd":231,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_linter/src/rules/pep8_naming/rules/invalid_first_argument_name.rs#L195-L231","documentation":"Internal panic in Ruff's pep8-naming N804/N805 rule (`invalid_first_argument_name`). The rule is only scheduled for function scopes, so the `let-else` on `ScopeKind::Function` treats any other scope kind as unreachable. Seeing this panic means the visitor was invoked with a non-function scope — a Ruff regression or API misuse, not a problem with the analyzed Python code.","triggerScenarios":"Running `ruff check --select N804,N805` where the rule visitor reaches a module, class, or lambda scope instead of a function scope; or calling `invalid_first_argument_name` directly from the ruff_linter API with a wrong `Scope`. Practically only via a Ruff bug, a fork, or a plugin hooking the wrong traversal point.","commonSituations":"After upgrading Ruff and linting code with metaclasses/methods (the rules' main domain); patched builds that changed the scope stack; running an old Ruff against newly supported syntax that misclassifies scopes.","solutions":["Update Ruff to the latest version and retry; search issues for 'Expected ScopeKind::Function' N804/N805 panic and attach your minimized snippet","Isolate with `ruff check --isolated --select N804,N805 <file>` to confirm, then report upstream if reproducible","Temporarily disable the rules: `ignore = [\"N804\", \"N805\"]` in `[tool.ruff.lint]`","If calling the rule from Rust, only invoke it for scopes where `matches!(scope.kind, ScopeKind::Function(_))`"],"exampleFix":"# before\n[tool.ruff.lint]\nselect = [\"N\"]\n\n# after (workaround: drop just the crashing rules)\n[tool.ruff.lint]\nselect = [\"N\"]\nignore = [\"N804\", \"N805\"]","handlingStrategy":"validation","validationCode":"// Rust caller-side guard\nif !matches!(scope.kind, ScopeKind::Function(_)) {\n    return;\n}","typeGuard":"fn is_function_scope(scope: &Scope) -> bool {\n    matches!(scope.kind, ScopeKind::Function(_))\n}","tryCatchPattern":null,"preventionTips":["Invoke N804/N805 only from the function-scope visit hook","Keep pep8-naming decorator settings (classmethod/staticmethod decorators) syntactically valid to avoid classifier drift","Upgrade Ruff promptly; search issues for the exact panic text","Keep a CI job that runs the linter on a canary corpus to catch version regressions before rollout"],"tags":["rust","panic","ruff","pep8-naming","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-14T00:17:10.932Z"}