astral-sh/ruff · error
{value:?} cannot be converted to EmptyStringCmpOp
Error message
{value:?} cannot be converted to EmptyStringCmpOp What it means
Pylint's PRM-style rule checking comparisons against empty strings ('' or "") only understands a subset of comparison operators (is, is not, ==, !=). The `TryFrom<&CmpOp>` conversion bails for any other operator, indicating the rule's checker reached a comparison with an unsupported operator — a diagnostic/fix invariant violation.
Source
Thrown at crates/ruff_linter/src/rules/pylint/rules/compare_to_empty_string.rs:132
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
enum EmptyStringCmpOp {
Is,
IsNot,
Eq,
NotEq,
}
impl TryFrom<&CmpOp> for EmptyStringCmpOp {
type Error = anyhow::Error;
fn try_from(value: &CmpOp) -> Result<Self, Self::Error> {
match value {
CmpOp::Is => Ok(Self::Is),
CmpOp::IsNot => Ok(Self::IsNot),
CmpOp::Eq => Ok(Self::Eq),
CmpOp::NotEq => Ok(Self::NotEq),
_ => bail!("{value:?} cannot be converted to EmptyStringCmpOp"),
}
}
}
impl EmptyStringCmpOp {
fn into_unary(self) -> &'static str {
match self {
Self::Is | Self::Eq => "not ",
Self::IsNot | Self::NotEq => "",
}
}
}
impl std::fmt::Display for EmptyStringCmpOp {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let repr = match self {
Self::Is => "is",
Self::IsNot => "is not",View on GitHub (pinned to 15f3fe6b15)
Solutions
- Update Ruff to the latest release
- Rewrite the comparison to use `==`/`!=` against '' if the diagnostic is intended
- File an issue with the offending comparison if reproducible
Example fix
# before
if s == '':
pass
# after
if not s:
pass Defensive patterns
Strategy: validation
Validate before calling
# Only ==/!=/is/is not comparisons against '' are supported by the rule:
if value == '':
... # flagged; rewrite as `if not value:` Prevention
- Use truthiness (`if not s:` / `if s:`) instead of comparing to ''
- Restrict comparisons against literals to ==/!=/is/is not
- Keep Ruff updated
When it happens
Trigger: `try_from` was called with a `CmpOp` other than Is/IsNot/Eq/NotEq (e.g. `<`, `>=`, `in`) during PLC0202/E710-ish empty-string comparison analysis.
Common situations: Effectively unreachable with stock Ruff since the rule only matches supported operators; appears with modified builds or AST-level changes.
Related errors
- `else` is expected to be on its own line
- indented block to start with indentation
- Empty `else` clause
- Compound statement cannot be inlined
- Failed to collapse `with`: {err}
AI-assisted analysis of astral-sh/ruff@15f3fe6b15 (2026-09-05).
Data as JSON: /api/errors/05fe19337553de47.
Report an issue: GitHub.