astral-sh/ruff · error

Expected assert statement to be a composite condition

Error message

Expected assert statement to be a composite condition

What it means

An anyhow bail! in the PT018 composite-condition fixer: the assert statement's test does not decompose into the composite (and/or with not) condition shapes the fixer knows how to split into individual conditions. The fix is aborted rather than producing a wrong rewrite.

Solutions

  1. Extend the condition extraction to cover the encountered expression shape
  2. Restrict the rule to condition shapes the fixer can decompose
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at crates/ruff_linter/src/rules/flake8_pytest_style/rules/assertion.rs:792 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of astral-sh/ruff@26f38c119c (2026-09-05). Data as JSON: /api/errors/f0563e0f08a4ade5. Report an issue: GitHub.

Appendix: source

Thrown at crates/ruff_linter/src/rules/flake8_pytest_style/rules/assertion.rs:792

    };

    // Extract the individual conditions.
    let mut conditions: Vec<Expression> = Vec::with_capacity(2);
    match &assert_statement.test {
        Expression::UnaryOperation(op) => {
            if matches!(op.operator, libcst_native::UnaryOp::Not { .. }) {
                if let Expression::BooleanOperation(boolean_operation) = &*op.expression {
                    if matches!(boolean_operation.operator, BooleanOp::Or { .. }) {
                        conditions.push(negate(&parenthesize(
                            &boolean_operation.left,
                            &op.expression,
                        )));
                        conditions.push(negate(&parenthesize(
                            &boolean_operation.right,
                            &op.expression,
                        )));
                    } else {
                        bail!("Expected assert statement to be a composite condition");
                    }
                } else {
                    bail!("Expected assert statement to be a composite condition");
                }
            }
        }
        Expression::BooleanOperation(op) => {
            if matches!(op.operator, BooleanOp::And { .. }) {
                conditions.push(parenthesize(&op.left, &assert_statement.test));
                conditions.push(parenthesize(&op.right, &assert_statement.test));
            } else {
                bail!("Expected assert statement to be a composite condition");
            }
        }
        _ => bail!("Expected assert statement to be a composite condition"),
    }

    // For each condition, create an `assert condition` statement.

View on GitHub (pinned to 26f38c119c)