astral-sh/ruff · error

Missing argument `regex`

Error message

Missing argument `regex`

What it means

Same regex-assert rewrite: after `text`, the pattern argument `regex` is required to build `re.search(regex, text)`. When `args_map` contains no `"regex"` entry, `generate_assert` fails with this error instead of emitting the pytest-style fix.

Source

Thrown at crates/ruff_linter/src/rules/flake8_pytest_style/rules/unittest_assert.rs:425

                        op: UnaryOp::Not,
                        operand: Box::new(isinstance),
                        range: TextRange::default(),
                        node_index: ruff_python_ast::AtomicNodeIndex::NONE,
                    };
                    let expr = node.into();
                    Ok(assert(&expr, msg))
                }
            }
            UnittestAssert::Regex
            | UnittestAssert::RegexpMatches
            | UnittestAssert::NotRegex
            | UnittestAssert::NotRegexpMatches => {
                let text = args
                    .get("text")
                    .ok_or_else(|| anyhow!("Missing argument `text`"))?;
                let regex = args
                    .get("regex")
                    .ok_or_else(|| anyhow!("Missing argument `regex`"))?;
                let msg = args.get("msg").copied();
                let node = ast::ExprName {
                    id: Name::new_static("re"),
                    ctx: ExprContext::Load,
                    range: TextRange::default(),
                    node_index: ruff_python_ast::AtomicNodeIndex::NONE,
                };
                let node1 = ast::ExprAttribute {
                    value: Box::new(node.into()),
                    attr: Identifier::new("search".to_string(), TextRange::default()),
                    ctx: ExprContext::Load,
                    range: TextRange::default(),
                    node_index: ruff_python_ast::AtomicNodeIndex::NONE,
                };
                let node2 = ast::ExprCall {
                    func: Box::new(node1.into()),
                    arguments: Arguments {
                        args: [(**regex).clone(), (**text).clone()].into(),

View on GitHub (pinned to 15f3fe6b15)

Solutions

  1. Provide the pattern: `self.assertRegex(text, r'expected')`.
  2. Exclude PT009 for the file if the method is custom and should not be converted.
  3. When working on Ruff, re-check `args_map`'s positional-name zip for the `['text', 'regex', 'msg']` spec.
  4. Re-run `ruff check --fix` after correcting the call.

Example fix

// before
self.assertRegex(text)
// after
self.assertRegex(text, r'\d{4}-\d{2}-\d{2}')
Defensive patterns

Strategy: validation

Validate before calling

def valid_regex_assert(call_args, call_kwargs):
    return (len(call_args) >= 2) or (len(call_args) == 1 and 'regex' in call_kwargs)

Type guard

def has_regex(args: tuple, kwargs: dict) -> bool:
    return len(args) >= 2 or 'regex' in kwargs

Prevention

When it happens

Trigger: `Regex`-family `generate_assert` where the call supplied `text` but no second positional pattern and no `regex=` keyword — e.g. `self.assertRegex(log_output)`.

Common situations: Regex assertions whose pattern was removed or left as a placeholder during test edits; non-unittest `assertRegex` helpers with different arity being auto-fixed by PT009.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of astral-sh/ruff@15f3fe6b15 (2026-09-05). Data as JSON: /api/errors/940e91cb838077cc. Report an issue: GitHub.