{"record":{"id":"d02bb5b021c70e19","repo":"astral-sh/ruff","slug":"missing-argument-expr","errorCode":null,"errorMessage":"Missing argument `expr`","messagePattern":"Missing argument `expr`","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/ruff_linter/src/rules/flake8_pytest_style/rules/unittest_assert.rs","lineNumber":289,"sourceCode":"                }\n            }) {\n                args_map.insert(arg_name, value);\n            }\n        }\n\n        Ok(args_map)\n    }\n\n    pub(crate) fn generate_assert(self, args: &[Expr], keywords: &[Keyword]) -> Result<Stmt> {\n        let args = self.args_map(args, keywords)?;\n        match self {\n            UnittestAssert::True\n            | UnittestAssert::False\n            | UnittestAssert::FailUnless\n            | UnittestAssert::FailIf => {\n                let expr = *args\n                    .get(\"expr\")\n                    .ok_or_else(|| anyhow!(\"Missing argument `expr`\"))?;\n                let msg = args.get(\"msg\").copied();\n                Ok(\n                    if matches!(self, UnittestAssert::False | UnittestAssert::FailIf) {\n                        assert(\n                            &Expr::UnaryOp(ast::ExprUnaryOp {\n                                op: UnaryOp::Not,\n                                operand: Box::new(expr.clone()),\n                                range: TextRange::default(),\n                                node_index: ruff_python_ast::AtomicNodeIndex::NONE,\n                            }),\n                            msg,\n                        )\n                    } else {\n                        assert(expr, msg)\n                    },\n                )\n            }\n            UnittestAssert::Equal","sourceCodeStart":271,"sourceCodeEnd":307,"githubUrl":"https://github.com/astral-sh/ruff/blob/15f3fe6b15a5f00172f34b0f542f8ea277f5a586/crates/ruff_linter/src/rules/flake8_pytest_style/rules/unittest_assert.rs#L271-L307","documentation":"The flake8-pytest-style `unittest_assert` generator reconstructs calls like `self.assertTrue(expr)` from recorded arguments. Variants `assertTrue`/`assertFalse`/`failUnless`/`failIf` require an `expr` argument; if the args map lacks it, generation cannot build a valid call and fails with this internal error.","triggerScenarios":"Calling `UnittestAssert::generate_assert` with an `args` map missing the `\"expr\"` key for one of the `True`, `False`, `FailUnless`, or `FailIf` variants.","commonSituations":"Fixer/refactor code paths (e.g. translating between pytest and unittest assertion styles) constructing the args map incorrectly, or upstream callers passing assertion data captured without the expression.","solutions":["Ensure the caller inserts the expression under the `expr` key before calling `generate_assert`.","Check the variant: `assertTrue`-family asserts always need an expression; use `Fail`/argument-less variants for message-only assertions.","If you hit this from Ruff itself, capture the file and report it as a fixer bug upstream."],"exampleFix":"// before\nlet mut args = FxHashMap::default();\nargs.insert(\"msg\", msg); // expr missing\nunittest_assert.generate_assert(&args)?\n\n// after\nlet mut args = FxHashMap::default();\nargs.insert(\"expr\", expr);\nargs.insert(\"msg\", msg);\nunittest_assert.generate_assert(&args)?","handlingStrategy":"validation","validationCode":"// validate args before generate_assert\nif (!args.has('expr') && ['True','False','FailUnless','FailIf'].includes(variant)) {\n  throw new Error(`${variant} requires an expr argument`);\n}","typeGuard":"fn has_expr(args: &FxHashMap<&str, &str>) -> bool { args.contains_key(\"expr\") }","tryCatchPattern":"match assert.generate_assert(&args) {\n    Err(e) if e.to_string() == \"Missing argument `expr`\" => {\n        eprintln!(\"Bug: {} assertions need an expr; check the caller that builds args\", variant);\n    }\n    other => other?,\n}","preventionTips":["Always populate the `expr` key when constructing args for assertTrue/assertFalse/failUnless/failIf variants.","Centralize args-map construction in one helper that asserts required keys.","Add a debug_assert!/unit test for required keys per UnittestAssert variant."],"tags":["autofix","flake8-pytest-style","internal","ruff"],"backgroundTag":"missing-required-argument","analyzedSha":"15f3fe6b15a5f00172f34b0f542f8ea277f5a586","analyzedAt":"2026-09-05T10:32:37.492Z","contentChangedAt":"2026-09-05T10:32:37.492Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}