Skip to content

Commit 5309e8c

Browse files
authored
Fix invalid args count msg (#5960)
1 parent 5957f5d commit 5309e8c

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

derive-impl/src/from_args.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,24 +180,32 @@ fn generate_field((i, field): (usize, &Field)) -> Result<TokenStream> {
180180
}
181181

182182
pub fn impl_from_args(input: DeriveInput) -> Result<TokenStream> {
183-
let fields = match input.data {
184-
Data::Struct(syn::DataStruct { fields, .. }) => fields
185-
.iter()
186-
.enumerate()
187-
.map(generate_field)
188-
.collect::<Result<TokenStream>>()?,
183+
// TODO: Set lower arity bound dynamicly
184+
let (fields, arity) = match input.data {
185+
Data::Struct(syn::DataStruct { fields, .. }) => (
186+
fields
187+
.iter()
188+
.enumerate()
189+
.map(generate_field)
190+
.collect::<Result<TokenStream>>()?,
191+
fields.len(),
192+
),
189193
_ => bail_span!(input, "FromArgs input must be a struct"),
190194
};
191195

192196
let name = input.ident;
193197
let (impl_generics, ty_generics, where_clause) = input.generics.split_for_impl();
194198
let output = quote! {
195199
impl #impl_generics ::rustpython_vm::function::FromArgs for #name #ty_generics #where_clause {
200+
fn arity() -> ::std::ops::RangeInclusive<usize> {
201+
0..=#arity
202+
}
203+
196204
fn from_args(
197205
vm: &::rustpython_vm::VirtualMachine,
198206
args: &mut ::rustpython_vm::function::FuncArgs
199207
) -> ::std::result::Result<Self, ::rustpython_vm::function::ArgumentError> {
200-
Ok(#name { #fields })
208+
Ok(Self { #fields })
201209
}
202210
}
203211
};

vm/src/function/argument.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ impl FuncArgs {
213213

214214
if !self.args.is_empty() {
215215
Err(vm.new_type_error(format!(
216-
"Expected at most {} arguments ({} given)",
216+
"expected at most {} arguments, got {}",
217217
T::arity().end(),
218218
given_args,
219219
)))
@@ -263,12 +263,12 @@ impl ArgumentError {
263263
) -> PyBaseExceptionRef {
264264
match self {
265265
Self::TooFewArgs => vm.new_type_error(format!(
266-
"Expected at least {} arguments ({} given)",
266+
"expected at least {} arguments, got {}",
267267
arity.start(),
268268
num_given
269269
)),
270270
Self::TooManyArgs => vm.new_type_error(format!(
271-
"Expected at most {} arguments ({} given)",
271+
"expected at most {} arguments, got {}",
272272
arity.end(),
273273
num_given
274274
)),

0 commit comments

Comments
 (0)