astral-sh/ruff · info
Cannot fix `{self}`
Error message
Cannot fix `{self}` What it means
This bail in `generate_assert` is the catch-all abort of the flake8-pytest-style unittest-assert-to-assert rewrite: the assertion method matched by PT009 has no corresponding libcst transformation, so no `assert` statement can be synthesized. The diagnostic is reported but the fix is marked unavailable.
Source
Thrown at crates/ruff_linter/src/rules/flake8_pytest_style/rules/unittest_assert.rs:464
node_index: ruff_python_ast::AtomicNodeIndex::NONE,
},
range_start: ruff_text_size::TextSize::default(),
node_index: ruff_python_ast::AtomicNodeIndex::NONE,
};
let re_search = node2.into();
if matches!(self, UnittestAssert::Regex | UnittestAssert::RegexpMatches) {
Ok(assert(&re_search, msg))
} else {
let node = ast::ExprUnaryOp {
op: UnaryOp::Not,
operand: Box::new(re_search),
range: TextRange::default(),
node_index: ruff_python_ast::AtomicNodeIndex::NONE,
};
Ok(assert(&node.into(), msg))
}
}
_ => bail!("Cannot fix `{self}`"),
}
}
}
View on GitHub (pinned to 15f3fe6b15)
Solutions
- Rewrite the assertion manually as a plain `assert` with a comparison appropriate to the method
- Suppress PT009 for the line with `# noqa: PT009` if the unittest style must stay
- Disable PT009 (or its fix) in configuration for codebases that intentionally keep unittest assertions
Example fix
// before self.assertGreater(len(items), 0) // after assert len(items) > 0
Defensive patterns
Strategy: try-catch
Validate before calling
// Prefer explicit rewrites for uncommon assertion methods: // treat self.assertX(...) calls not in the supported set (assertEqual, assertTrue, assertRaises, ...) as manual-migration cases.
Type guard
// In a pre-commit script: skip auto-fixing files containing unsupported methods: grep -nE 'assert(Not)?(DictContainsSubset|CountEqual|NoLogs|Logs)' tests/ && echo 'manual migration needed'
Try / catch
// run fix selectively and continue on unfixable findings:
$fix = ruff --fix --select PT009 .; if ($fix.unfixed > 0) { planManualMigration($fix.unfixedDiagnostics) } Prevention
- List target assertion methods before a unittest->pytest migration and hand-write mappings for the rare ones
- Treat PT009 as diagnostic-only (unfixable) in config if the codebase uses exotic assertion methods
- Do migrations in small batches so unfixable cases are reviewed
When it happens
Trigger: `ruff check --fix` on a unittest assertion call whose variant is not one of the supported rewrite shapes handled by the match arms above line 465 (e.g. less common assertion methods like `assertDictContainsSubset`-style or forms the rewriter doesn't model).
Common situations: Mixed unittest/pytest codebases hitting rarer assertion methods; calls built dynamically that pattern-match into the default branch of the rewrite.
Related errors
- Variable-length arguments are not supported
- Unknown keyword argument
- Missing argument `expr`
- Failed to collapse `with`: {err}
- Unable to fix multiline statement
AI-assisted analysis of astral-sh/ruff@15f3fe6b15 (2026-09-05).
Data as JSON: /api/errors/afab3bd49a157683.
Report an issue: GitHub.