Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add better error message for Python in signature #2930

Merged
merged 1 commit into from
Feb 3, 2023

Conversation

davidhewitt
Copy link
Member

Inspired by #2929, this just adds a better error message when Python arguments are accidentally included in the signature.

@davidhewitt davidhewitt added the CI-skip-changelog Skip checking changelog entry label Jan 29, 2023
"expected argument from function definition `{}` but got argument `{}`",
fn_arg.name.unraw(),
name.unraw(),
struct ArgsValidator<'b, 'a: 'b>(std::slice::IterMut<'b, FnArg<'a>>);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was the change from closure to named type required for this to work?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The simplest way I could see to implement this was to use recursion to skip over Pythonarguments, which makes a horrible mess of closures.

Not sure what I've come up with here is the prettiest either. Maybe give me another shot at this tomorrow and I'll try factor it out differently?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, yes recursion and closure would indeed be a mess. I am not opposed to the change, I just asked to make sure that I did not miss anything. (And indeed I did not see the recursion at a first glance.)

For such cases, I think loop might help, i.e. turn

fn func(iter: I) -> R {
  if let Some(val) = iter.next() {
    if condition(val) {
      return func(iter);
    }

    ...
  } else {
    ...
  }
}

into

fn func(iter: I) -> R {
  loop {
    if let Some(val) = iter.next() {
      if condition(val) {
        continue;
      }

      break ...;
    } else {
      break ...;
    }
  }
}

Copy link
Member

@mejrs mejrs Jan 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could be missing something, but this looks like an awful way to reinvent retain.

let mut err = None;
arguments.retain(|a| /* delete some things and/or set err */);
if let Some(err){
     return ...
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not exactly - we don't want to remove elements from arguments, just enrich them based off the attribute information. This might be able to be refactored better once deprecated #[args] is removed.

I'll have another go at tidying this up now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A for loop is certainly nicer than an open-coded loop loop. LGTM.

(The .by_ref() is required because adding move |... to the closure would move more than just the iterator into it?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .by_ref() is needed because I don't want to move the iterator into the closure - I check at the end of the function that we've consumed everything from it.

@davidhewitt davidhewitt force-pushed the signature-py branch 2 times, most recently from 17a4ff1 to b5dfe14 Compare February 3, 2023 06:55
@davidhewitt
Copy link
Member Author

bors r=adamreichold

@bors
Copy link
Contributor

bors bot commented Feb 3, 2023

Build succeeded:

@bors bors bot merged commit 864aee0 into PyO3:main Feb 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI-skip-changelog Skip checking changelog entry
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants