astral-sh/ruff · error

Missing argument `container`

Error message

Missing argument `container`

What it means

Counterpart to the `member` check: `assertIn`/`assertNotIn` need a second, `container`, argument so the rewrite can emit `assert member in container`. When `args_map` has no `"container"` entry, `generate_assert` aborts with this error.

Source

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

                    | UnittestAssert::FailIfEqual => CmpOp::NotEq,
                    UnittestAssert::Greater => CmpOp::Gt,
                    UnittestAssert::GreaterEqual => CmpOp::GtE,
                    UnittestAssert::Less => CmpOp::Lt,
                    UnittestAssert::LessEqual => CmpOp::LtE,
                    UnittestAssert::Is => CmpOp::Is,
                    UnittestAssert::IsNot => CmpOp::IsNot,
                    _ => unreachable!(),
                };
                let expr = compare(first, cmp_op, second);
                Ok(assert(&expr, msg))
            }
            UnittestAssert::In | UnittestAssert::NotIn => {
                let member = args
                    .get("member")
                    .ok_or_else(|| anyhow!("Missing argument `member`"))?;
                let container = args
                    .get("container")
                    .ok_or_else(|| anyhow!("Missing argument `container`"))?;
                let msg = args.get("msg").copied();
                let cmp_op = if matches!(self, UnittestAssert::In) {
                    CmpOp::In
                } else {
                    CmpOp::NotIn
                };
                let expr = compare(member, cmp_op, container);
                Ok(assert(&expr, msg))
            }
            UnittestAssert::IsNone | UnittestAssert::IsNotNone => {
                let expr = args
                    .get("expr")
                    .ok_or_else(|| anyhow!("Missing argument `expr`"))?;
                let msg = args.get("msg").copied();
                let cmp_op = if matches!(self, UnittestAssert::IsNone) {
                    CmpOp::Is
                } else {
                    CmpOp::IsNot

View on GitHub (pinned to 15f3fe6b15)

Solutions

  1. Supply the container: `self.assertIn(x, self.items)`.
  2. Suppress PT009 for that file if the assert method is not unittest's.
  3. When developing Ruff, note keyword lookup only fills names skipped positionally (`arg_spec.iter().skip(args.len())`); mismatched positional counts leave gaps.
  4. Re-run the fixer after correcting the test code.

Example fix

// before
self.assertIn(item)
// after
self.assertIn(item, container)
Defensive patterns

Strategy: validation

Validate before calling

def ensure_container(call_args, call_kwargs):
    if len(call_args) < 2 and 'container' not in call_kwargs:
        raise ValueError('assertIn requires (member, container)')

Type guard

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

Prevention

When it happens

Trigger: `UnittestAssert::In | NotIn.generate_assert` where the call provided `member` but no second positional arg and no `container=` keyword — e.g. `self.assertIn(x)`.

Common situations: Half-finished `assertIn(x)` calls during test refactors; non-unittest helpers named `assertIn` mistaken for unittest methods 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/300ad9baf1fd47fb. Report an issue: GitHub.