{"record":{"id":"5ddb2269f598d8d9","repo":"astral-sh/ruff","slug":"expected-expression-tuple-expression-list","errorCode":null,"errorMessage":"Expected Expression::Tuple | Expression::List","messagePattern":"Expected Expression::Tuple \\| Expression::List","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/ruff_linter/src/rules/flake8_comprehensions/fixes.rs","lineNumber":170,"sourceCode":"    ))\n}\n\n/// (C406) Convert `dict([(1, 2)])` to `{1: 2}`.\npub(crate) fn fix_unnecessary_literal_dict(expr: &Expr, checker: &Checker) -> Result<Edit> {\n    let locator = checker.locator();\n    let stylist = checker.stylist();\n\n    // Expr(Call(List|Tuple)))) -> Expr(Dict)))\n    let module_text = locator.slice(expr);\n    let mut tree = match_expression(module_text)?;\n    let call = match_call_mut(&mut tree)?;\n    let arg = match_arg(call)?;\n\n    let elements = match &arg.value {\n        Expression::Tuple(inner) => &inner.elements,\n        Expression::List(inner) => &inner.elements,\n        _ => {\n            bail!(\"Expected Expression::Tuple | Expression::List\");\n        }\n    };\n\n    let elements: Vec<DictElement> = elements\n        .iter()\n        .map(|element| {\n            if let Element::Simple {\n                value: Expression::Tuple(tuple),\n                comma,\n            } = element\n            {\n                if let Some(Element::Simple { value: key, .. }) = tuple.elements.first() {\n                    if let Some(Element::Simple { value, .. }) = tuple.elements.get(1) {\n                        return Ok(DictElement::Simple {\n                            key: key.clone(),\n                            value: value.clone(),\n                            comma: comma.clone(),\n                            whitespace_before_colon: ParenthesizableWhitespace::default(),","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_linter/src/rules/flake8_comprehensions/fixes.rs#L152-L188","documentation":"This error is raised internally by ruff's flake8_comprehensions rule C408 (unnecessary dict literal) when generating an automatic fix. The fixer expects the single argument to `dict()` to be a tuple or list literal whose elements become dict entries; if the argument is any other expression kind, the fix cannot be safely constructed and the fix aborts with this message.","triggerScenarios":"Running `ruff check --fix` on code like `dict((('a', 1),))` or `dict(x)` where the argument to `dict()` is not an AST `Expression::Tuple` or `Expression::List` (e.g. a name, call, or generator expression).","commonSituations":"Fixing code where `dict()` receives a variable or a function call rather than a literal sequence; running autofix on dynamically generated code; rare because the diagnostic C408 normally only fires on tuple/list literals.","solutions":["Verify the argument inside `dict(...)` is a literal tuple or list of key/value pairs; rewrite it as a dict literal `{'a': 1}` manually if the fixer declines.","Skip autofix for this diagnostic (`ruff check --fix --extend-ignore C408` or per-line noqa) and refactor by hand.","Upgrade ruff; the matcher was tightened over time so unsupported shapes bail earlier and more predictably."],"exampleFix":"// before (fixer input)\ndict([(\"a\", 1), (\"b\", 2)])\n// after (what the fixer produces)\n{\"a\": 1, \"b\": 2}","handlingStrategy":"fallback","validationCode":"import ast\nnode = ast.parse('dict([(\"a\", 1)])').body[0].value\nok = isinstance(node.args[0], (ast.Tuple, ast.List)) if node.args else False","typeGuard":"def is_seq_literal(expr):\n    return isinstance(expr, (ast.Tuple, ast.List))","tryCatchPattern":"try:\n    apply_autofix(diagnostic)\nexcept Exception:\n    manual_rewrite()  # keep the diagnostic, fix by hand","preventionTips":["Pass only literal tuples/lists of pairs to dict() when targeting C408 autofix","Re-run ruff after edits so fixes see fresh ASTs","Keep C408 autofix on simple literal forms"],"tags":["rust","ruff","lint-autofix","flake8-comprehensions"],"backgroundTag":"autofix-unsupported-ast-shape","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"}