astral-sh/ruff · error

Missing argument `text`

Error message

Missing argument `text`

What it means

`generate_assert` rewrites `assertRegex`/`assertRegexpMatches`/`assertNotRegex`/`assertNotRegexpMatches` into `re.search(regex, text)` wrapped in an `assert`. The `text` (first) argument is mandatory; if the argument map has no `"text"` entry, the rewrite aborts with this error.

Source

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

                    Ok(assert(&isinstance, msg))
                } else {
                    let node = ast::ExprUnaryOp {
                        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 {

View on GitHub (pinned to 15f3fe6b15)

Solutions

  1. Fix the test call to pass the text first: `self.assertRegex(output, r'\d+')`.
  2. Use `# noqa: PT009` or per-file ignores when the call should not be rewritten.
  3. If developing Ruff, confirm `arg_spec = ['text', 'regex', 'msg']` and positional insertion of `text` in `args_map`.
  4. Run `ruff check --no-fix` to see the diagnostic and pinpoint the call.

Example fix

// before
self.assertRegex(pattern=r'abc')
// after
self.assertRegex(output, r'abc')
Defensive patterns

Strategy: validation

Validate before calling

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

Type guard

def has_text(args: tuple, kwargs: dict) -> bool:
    return len(args) >= 1 or 'text' in kwargs

Prevention

When it happens

Trigger: `UnittestAssert::Regex | RegexpMatches | NotRegex | NotRegexpMatches.generate_assert` where the source call has no first positional argument and no `text=` keyword — e.g. `self.assertRegex()` or a call passing only `regex=`/`msg=`.

Common situations: Incomplete regex assertions bulk-fixed via `ruff --fix` (PT009); legacy `assertRegexpMatches` aliases whose argument lists were edited down; dynamic methods matching the names without unittest's signature.

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/1686e761e7e55bae. Report an issue: GitHub.