{"record":{"id":"563836868b2cadee","repo":"astral-sh/ruff","slug":"expected-each-argument-to-be-a-tuple-of-length-two","errorCode":null,"errorMessage":"Expected each argument to be a tuple of length two","messagePattern":"Expected each argument to be a tuple of length two","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/ruff_linter/src/rules/flake8_comprehensions/fixes.rs","lineNumber":196,"sourceCode":"                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(),\n                            whitespace_after_colon: ParenthesizableWhitespace::SimpleWhitespace(\n                                SimpleWhitespace(\" \"),\n                            ),\n                        });\n                    }\n                }\n            }\n            bail!(\"Expected each argument to be a tuple of length two\")\n        })\n        .collect::<Result<Vec<DictElement>>>()?;\n\n    tree = Expression::Dict(Box::new(Dict {\n        elements,\n        lbrace: LeftCurlyBrace {\n            whitespace_after: call.whitespace_before_args.clone(),\n        },\n        rbrace: RightCurlyBrace {\n            whitespace_before: arg.whitespace_after_arg.clone(),\n        },\n        lpar: vec![],\n        rpar: vec![],\n    }));\n\n    Ok(Edit::range_replacement(\n        pad_expression(\n            tree.codegen_stylist(stylist),","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_linter/src/rules/flake8_comprehensions/fixes.rs#L178-L214","documentation":"Raised during the C408 fix (`dict(...)` -> dict literal) when an element of the tuple/list argument is not a two-element (key, value) structure. The fixer maps each element to a `DictElement` and requires every element to be a pair; anything else aborts the fix.","triggerScenarios":"`ruff check --fix` on `dict([('a', 1, 'extra'), ('b', 2)])` or `dict([('a',)])` where a nested tuple/list element does not have exactly length two.","commonSituations":"Hand-written tuple lists with malformed entries; refactored code where a third field was added to pairs; data literals pasted from elsewhere that violate dict() semantics anyway.","solutions":["Ensure every inner tuple/list has exactly two elements; run the fixer again.","Convert to a dict literal directly, which sidesteps the fixer.","Suppress C408 for that line with `# noqa: C408` if the argument structure is intentionally irregular."],"exampleFix":"// before\ndict([(\"a\", 1, \"x\")])\n// after\ndict([(\"a\", 1)])  # or {\"a\": 1}","handlingStrategy":"validation","validationCode":"pairs = [(\"a\", 1), (\"b\", 2)]\nassert all(isinstance(p, (tuple, list)) and len(p) == 2 for p in pairs), 'each dict() arg element must be a pair'","typeGuard":"def is_pair(el):\n    return isinstance(el, (tuple, list)) and len(el) == 2","tryCatchPattern":"try:\n    apply_autofix(diagnostic)\nexcept Exception:\n    manual_rewrite()","preventionTips":["Ensure every inner tuple in dict(...) has exactly two elements","Prefer writing dict literals directly","Run ruff check --fix on freshly saved code"],"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-14T05:17:10.506Z"}