Skip to content

Remove remaining usages of arg_check! #1953

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

Merged
merged 4 commits into from
Jun 30, 2020
Merged

Remove remaining usages of arg_check! #1953

merged 4 commits into from
Jun 30, 2020

Conversation

limeburst
Copy link
Contributor

This pull request removes the remaining usages of arg_check! and closes #1714.

@limeburst
Copy link
Contributor Author

I'm planning to look into parameter-based arg checking. Any pointers are appreciated!

@limeburst
Copy link
Contributor Author

I've reproduced the CI failure on my machine, will look into it!

optional = [(val, None)]
);
let value = match val {
fn tp_new(vm: &VirtualMachine, mut args: PyFuncArgs) -> PyResult {
Copy link
Member

@youknowone youknowone Jun 2, 2020

Choose a reason for hiding this comment

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

Suggested change
fn tp_new(vm: &VirtualMachine, mut args: PyFuncArgs) -> PyResult {
fn tp_new(zelf: PyObjectRef, value: PyObjectRef, vm: &VirtualMachine) -> PyResult {

Unless there are keyword arguments, this form is more easy for most of cases. In this way, you don't need to unpack zelf and value from args yourself.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there another form I could use when I need to accept a zero-arguments case?

Copy link
Member

Choose a reason for hiding this comment

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

(args..., vm: &VirtualMachine) -> ReturnType form work for everything.

These examples are actual RustPython code for zero-argument:

fn builtin_globals(vm: &VirtualMachine) -> PyResult<PyDictRef> { }
fn repr(self, vm: &VirtualMachine) -> PyResult<String> { }

If you are looking for optional arguments that would allow zero-arguments for users, check for OptionalArg for positional-only argument

fn sys_exit(code: OptionalArg<PyObjectRef>, vm: &VirtualMachine) -> PyResult

and search for FromArgs for keyword arguments.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I tried using OptionalArg for tp_new, but it seems that it's not suitable for tp_new at the moment, since it allows keyword arguments to be passed as well. For example, when used in this form:

fn tp_new(zelf: PyObjectRef, x: OptionalArg<PyObjectRef>, vm: &VirtualMachine)

It fails the test test_keyword_args in test_bool.py.

Copy link
Member

@coolreader18 coolreader18 Jun 29, 2020

Choose a reason for hiding this comment

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

Oh, that's fine, just remove the unexpectedFailure annotation from test_keyword_args. I don't think it should actually allow keyword args, it uses take_positional under the hood.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@coolreader18 I'm not sure if I used OptionalArg correctly, but I've added the related commits!

Copy link
Member

@coolreader18 coolreader18 left a comment

Choose a reason for hiding this comment

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

Nice job!

@coolreader18
Copy link
Member

Thanks for contributing!

@coolreader18 coolreader18 merged commit e11285a into RustPython:master Jun 30, 2020
Comment on lines +719 to +723
let iterable = args.take_positional();
if iterable.is_none() {
return Err(vm.new_type_error("sorted expected 1 arguments, got 0".to_string()));
}
let items = vm.extract_elements(&iterable.unwrap())?;
Copy link
Member

@youknowone youknowone Jun 30, 2020

Choose a reason for hiding this comment

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

Though it is already merged, we conventionally prefer to use this form to avoid unwrap when possible:

Suggested change
let iterable = args.take_positional();
if iterable.is_none() {
return Err(vm.new_type_error("sorted expected 1 arguments, got 0".to_string()));
}
let items = vm.extract_elements(&iterable.unwrap())?;
let iterable = args.take_positional().unwrap_or_else(|| Err(vm.new_type_error("sorted expected 1 arguments, got 0".to_string())))?;
let items = vm.extract_elements(&iterable)?;

@limeburst limeburst deleted the remove-arg-check branch July 14, 2020 01:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Replace the remaining arg_check! calls with parameter-based arg checking
3 participants