astral-sh/ruff · error
argument index should not be out of range
Error message
argument index should not be out of range
What it means
When reporting a call-binding error, get_argument_node maps an argument index back to syntax: it takes the nth argument of a Call node in source order, or of a ClassDef's keyword arguments (with `metaclass` excluded). The expect fires when the index is past the arguments actually present in the node - the inference-side index and the AST have drifted apart.
Source
Thrown at crates/ty_python_semantic/src/types/call/bind.rs:9510
if let Some(builder) = context.report_lint(&UNKNOWN_ARGUMENT, range) {
let mut diag = builder.into_diagnostic(format_args!(
"Unpacked argument may contain keyword arguments that do not match any known parameter{}",
callable_description
.map(|description| format!(" of {description}"))
.unwrap_or_default()
));
if let Some(compound_diag) = compound_diag {
compound_diag.add_context(db, env, &mut diag);
} else if let Some(spans) = callable_ty.function_spans(context.db()) {
let mut sub = SubDiagnostic::new(
SubDiagnosticSeverity::Info,
format_args!("{callable_kind} signature here"),
);
sub.annotate(Annotation::primary(spans.signature));
diag.sub(sub);
}
}
}
Self::PositionalOnlyParameterAsKwarg {
argument_index,
parameter,
} => {
let range = context.get_range(node, *argument_index);
if let Some(builder) =
context.report_lint(&POSITIONAL_ONLY_PARAMETER_AS_KWARG, range)
{
let mut diag = builder.into_diagnostic(format_args!(
"Positional-only parameter {parameter} passed as keyword argument{}",
callable_description
.map(|description| format!(" of {description}"))
.unwrap_or_default()
));
if let Some(compound_diag) = compound_diag {
compound_diag.add_context(db, env, &mut diag);
} else if let Some(spans) = callable_ty.function_spans(context.db()) {View on GitHub (pinned to 15f3fe6b15)
Solutions
- Capture the exact expression and the diagnostic being emitted, and file a ty issue: this is a diagnostic-to-AST mapping bug, not a wrong type result
- As a contributor: use the Option from nth() and fall back to the whole-call (or class) span instead of expecting
- Check the keyword-only counting path in the ClassDef arm (metaclass exclusion) against the matcher's index
Example fix
// before
let arg = call_node.arguments.iter_source_order().nth(argument_index).expect("argument index should not be out of range");
// after
let arg = call_node.arguments.iter_source_order().nth(argument_index); // Option; caller falls back to the call span Defensive patterns
Strategy: validation
Validate before calling
// Before mapping an index to a node, confirm the node has that many source arguments:
let count = match node {
ast::AnyNodeRef::ExprCall(call) => call.arguments.iter_source_order().count(),
ast::AnyNodeRef::StmtClassDef(class) => class.arguments.as_deref()
.map(|a| a.iter_source_order().filter_map(ArgOrKeyword::as_keyword).count())
.unwrap_or(0),
_ => 0,
};
if argument_index.map_or(true, |i| i < count) { /* safe to map */ } Prevention
- Treat index-to-node mapping as fallible: use the Option from nth() and fall back to the enclosing expression's span
- Keep argument-index accounting identical between the matcher and the reporter, including class keyword and metaclass exclusions
- When adding diagnostics for new call shapes, test them against calls with fewer source arguments than parameters
When it happens
Trigger: An argument_index derived from signature matching (counting parameters with no corresponding source argument, or class keyword filtering miscounting `__init_subclass__`/metaclass cases) used against a Call/ClassDef node with fewer source arguments; also stale nodes after edits.
Common situations: Diagnostics for calls with implicit arguments, *unpacking, class keyword arguments, or newly changed argument-index accounting between the matcher and the reporter.
Related errors
- should be set because `extract_if` only yields elements with
- argument index should be valid
- `parsed_module` should have assigned a node index
- argument index should be valid
- extra use-def data should have been retained
AI-assisted analysis of astral-sh/ruff@15f3fe6b15 (2026-08-20).
Data as JSON: /api/errors/62d2d158ca1734cb.
Report an issue: GitHub.