astral-sh/ruff · warning
Expected one argument in outer function call
Error message
Expected one argument in outer function call
What it means
Raised by the C411/unnecessary-call-around-sorted fix (e.g. `sorted(list(...))` -> `sorted(...)`) when the outer call to `sorted` does not have exactly one argument. The fixer rewrites only single-argument forms; anything else cannot be transformed safely.
Source
Thrown at crates/ruff_linter/src/rules/flake8_comprehensions/fixes.rs:429
expr.range(),
))
}
/// (C413) Convert `list(sorted([2, 3, 1]))` to `sorted([2, 3, 1])`.
/// (C413) Convert `reversed(sorted([2, 3, 1]))` to `sorted([2, 3, 1],
/// reverse=True)`.
pub(crate) fn fix_unnecessary_call_around_sorted(
expr: &Expr,
locator: &Locator,
stylist: &Stylist,
) -> Result<Edit> {
let module_text = locator.slice(expr);
let mut tree = match_expression(module_text)?;
let outer_call = match_call_mut(&mut tree)?;
let inner_call = match &outer_call.args[..] {
[arg] => match_call(&arg.value)?,
_ => {
bail!("Expected one argument in outer function call");
}
};
let inner_needs_parens = matches!(
inner_call.whitespace_after_func,
ParenthesizableWhitespace::ParenthesizedWhitespace(_)
);
if let Expression::Name(outer_name) = &*outer_call.func {
if outer_name.value == "list" {
tree = Expression::Call(Box::new((*inner_call).clone()));
if inner_needs_parens {
tree = tree.with_parens(LeftParen::default(), RightParen::default());
}
} else {
// If the `reverse` argument is used...
let args = if inner_call.args.iter().any(|arg| {
matches!(View on GitHub (pinned to 26f38c119c)
Solutions
- Re-run `ruff check --fix` so diagnostics match current code (stale ranges often cause shape mismatches).
- Rewrite manually: keep `sorted(x, key=..., reverse=...)` as-is; the rule only simplifies `sorted(list(x))` forms.
- Disable the fix (use the diagnostic without `--fix`) if the call shape is intentionally complex.
Example fix
// before sorted(list(items), reverse=True) // after sorted(items, reverse=True)
Defensive patterns
Strategy: validation
Validate before calling
assert len(sorted_call.args) == 1, 'C411 fix needs exactly one argument in sorted(...)'
Type guard
def single_arg_call(expr):
return isinstance(expr, ast.Call) and len(expr.args) == 1 Try / catch
try:
apply_autofix(diagnostic)
except Exception:
keep_code_as_is() Prevention
- Re-run ruff after editing sorted(...) arguments
- Apply key=/reverse= only where the rule is suppressed
- Avoid applying stale cached diagnostics
When it happens
Trigger: `ruff check --fix` on `sorted(a, b)` or `sorted()` — the outer `sorted(...)` call has zero or multiple arguments instead of one.
Common situations: Code where extra arguments (`key=`, `reverse=`) were added after the diagnostic was computed; stale diagnostics applied to edited code; hand-edited autofix inputs.
Related errors
- Expected Expression::Tuple | Expression::List
- Expected each argument to be a tuple of length two
- Expected at least one argument in outer function call
- Expected Expression::ListComp | Expression:SetComp | Express
- Expected two arguments
AI-assisted analysis of astral-sh/ruff@26f38c119c (2026-09-05).
Data as JSON: /api/errors/5fa117c954ae62ba.
Report an issue: GitHub.