{"record":{"id":"f0685d46145c6b13","repo":"astral-sh/ruff","slug":"expected-container-to-include-two-elements","errorCode":null,"errorMessage":"Expected container to include two elements","messagePattern":"Expected container to include two elements","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/ruff_linter/src/rules/flake8_comprehensions/fixes.rs","lineNumber":782,"sourceCode":"            tree = Expression::SetComp(Box::new(SetComp {\n                elt: lambda.body.clone(),\n                for_in: compfor,\n                lpar: vec![],\n                rpar: vec![],\n                lbrace: LeftCurlyBrace::default(),\n                rbrace: RightCurlyBrace::default(),\n            }));\n        }\n        ObjectType::Dict => {\n            let elements = match lambda.body.as_ref() {\n                Expression::Tuple(tuple) => &tuple.elements,\n                Expression::List(list) => &list.elements,\n                _ => {\n                    bail!(\"Expected tuple or list for dictionary comprehension\")\n                }\n            };\n            let [key, value] = elements.as_slice() else {\n                bail!(\"Expected container to include two elements\");\n            };\n            let Element::Simple { value: key, .. } = key else {\n                bail!(\"Expected container to use a key as the first element\");\n            };\n            let Element::Simple { value, .. } = value else {\n                bail!(\"Expected container to use a value as the second element\");\n            };\n\n            tree = Expression::DictComp(Box::new(DictComp {\n                for_in: compfor,\n                lpar: vec![],\n                rpar: vec![],\n                key: Box::new(key.clone()),\n                value: Box::new(value.clone()),\n                lbrace: LeftCurlyBrace::default(),\n                rbrace: RightCurlyBrace::default(),\n                whitespace_before_colon: ParenthesizableWhitespace::default(),\n                whitespace_after_colon: ParenthesizableWhitespace::SimpleWhitespace(","sourceCodeStart":764,"sourceCodeEnd":800,"githubUrl":"https://github.com/astral-sh/ruff/blob/26f38c119cac42e4d320ba08f09224fdec74af2c/crates/ruff_linter/src/rules/flake8_comprehensions/fixes.rs#L764-L800","documentation":"Also raised by the C417 `dict(map(lambda ...))` fix: after extracting the lambda body's tuple/list elements, exactly two (key, value) are required to build a `DictComp`. Tuples of any other length make the rewrite impossible and the fixer bails.","triggerScenarios":"`ruff check --fix` on `dict(map(lambda x: (a, b, c), items))` or `dict(map(lambda x: (a,), items))` — body container has one or three-plus elements.","commonSituations":"Refactored lambdas that grew extra tuple fields; datacarrying tuples reused for dict conversion; stale diagnostics after edits.","solutions":["Make the lambda body a two-element tuple/list, or slice out the two fields you actually need.","Rewrite as an explicit dict comprehension by hand and drop C417.","Suppress with `# noqa: C417` while keeping the longer tuple."],"exampleFix":"// before\nd = dict(map(lambda x: (x[0], x[1], x[2]), rows))\n// after\nd = {x[0]: x[1] for x in rows}","handlingStrategy":"validation","validationCode":"assert len(lam.body.elts) == 2, 'dict conversion needs exactly key and value'","typeGuard":"def is_two_element(body):\n    return isinstance(body, (ast.Tuple, ast.List)) and len(body.elts) == 2","tryCatchPattern":"try:\n    apply_autofix(diagnostic)\nexcept Exception:\n    slice_needed_fields_and_rewrite()","preventionTips":["Keep key/value lambdas to exactly two tuple fields","Write dict comprehensions directly for wider tuples","Refresh diagnostics before fixing"],"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"}