Skip to content

Commit 712cbd2

Browse files
committed
Report a missing argument before converting any
FuncArgs::bind_for counted surplus positional arguments before converting any, but a missing required argument was found only when FromArgs::from_args reached it, after converting the arguments ahead of it. A conversion can run user code, so itertools.compress(it) raised whatever it.__iter__() raised instead of the TypeError for the missing selectors argument. When fewer positional arguments are given than the lower arity bound, walk T::PARAMS for the first required positional parameter that no argument fills and report it before from_args runs. Assisted-by: Claude Code:claude-opus-5-5
1 parent 713e14c commit 712cbd2

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

‎crates/vm/src/function/argument.rs‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,15 @@ impl FuncArgs {
322322
return Err(callee.given_by_name_and_position(name, pos, vm));
323323
}
324324

325+
// A missing required argument is reported before the arguments ahead
326+
// of it are converted, for the same reason.
327+
if num_given < *arity.start()
328+
&& let Some(params) = T::PARAMS
329+
&& let Some(missing) = missing_positional(params, num_given, &self.kwargs)
330+
{
331+
return Err(missing.into_exception(&arity, num_given, callee, vm));
332+
}
333+
325334
let bound = T::from_args(vm, &mut self)
326335
.map_err(|e| e.into_exception(&arity, num_given, callee, vm))?;
327336

@@ -534,6 +543,44 @@ fn duplicate_name_and_position<'a>(
534543
walk(params, nargs, kwargs, &mut 0)
535544
}
536545

546+
/// First required positional parameter that neither a positional argument nor
547+
/// a keyword fills, as the error binding it would raise.
548+
fn missing_positional(params: &[Param], nargs: usize, kwargs: &KwArgs) -> Option<ArgumentError> {
549+
// `Err(None)` ends the walk without finding one.
550+
fn walk(
551+
params: &[Param],
552+
nargs: usize,
553+
kwargs: &KwArgs,
554+
pos: &mut usize,
555+
) -> Result<(), Option<ArgumentError>> {
556+
for param in params {
557+
match param.kind {
558+
ParamKind::Flatten(Some(inner)) => walk(inner, nargs, kwargs, pos)?,
559+
ParamKind::Flatten(None) => *pos += 1,
560+
ParamKind::PositionalOnly => {
561+
*pos += 1;
562+
if *pos > nargs && param.default.is_none() {
563+
return Err(Some(ArgumentError::TooFewArgs));
564+
}
565+
}
566+
ParamKind::PositionalOrKeyword => {
567+
*pos += 1;
568+
if *pos > nargs && param.default.is_none() && !kwargs.contains_key(param.name) {
569+
return Err(Some(ArgumentError::MissingRequiredArgument {
570+
name: param.name.to_owned(),
571+
pos: *pos,
572+
}));
573+
}
574+
}
575+
ParamKind::VarPositional => return Err(None),
576+
ParamKind::KeywordOnly | ParamKind::VarKeyword => {}
577+
}
578+
}
579+
Ok(())
580+
}
581+
walk(params, nargs, kwargs, &mut 0).err().flatten()
582+
}
583+
537584
/// The branch of _PyArg_UnpackKeywords that names a parameter it didn't get.
538585
pub(crate) fn missing_argument_message(name: Option<&str>, keyword: &str, pos: usize) -> String {
539586
let (name, parens) = call_form(name, "function");

‎extra_tests/snippets/vm_argument_errors.py‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,28 @@ def __index__(self):
167167
hex(FailingIndex(), 1)
168168

169169

170+
def test_a_missing_argument_is_reported_before_any_is_converted():
171+
check(
172+
TypeError,
173+
"compress() missing required argument 'selectors' (pos 2)",
174+
itertools.compress,
175+
TestFailingIter(),
176+
)
177+
check(
178+
TypeError,
179+
"compress() missing required argument 'selectors' (pos 2)",
180+
itertools.compress,
181+
data=TestFailingIter(),
182+
)
183+
check(
184+
TypeError,
185+
"compress() missing required argument 'selectors' (pos 2)",
186+
itertools.compress,
187+
TestFailingIter(),
188+
sel=1,
189+
)
190+
191+
170192
test_the_message_names_the_function()
171193
test_a_method_does_not_count_its_instance()
172194
test_one_argument_is_singular()
@@ -176,3 +198,4 @@ def __index__(self):
176198
test_a_slot_that_counts_its_own_arguments_says_the_same_thing()
177199
test_a_keyword_check_of_its_own_says_the_same_thing()
178200
test_too_many_arguments_are_counted_before_any_is_converted()
201+
test_a_missing_argument_is_reported_before_any_is_converted()

0 commit comments

Comments
 (0)