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

Support for postional only args in dynamic library api #4701

Merged
merged 5 commits into from
Apr 3, 2023

Conversation

aaltat
Copy link
Contributor

@aaltat aaltat commented Mar 24, 2023

Fixes #4660

summary:

copilot:summary

@Snooz82
Copy link
Member

Snooz82 commented Mar 25, 2023

Walk Through

copilot:walkthrough

@aaltat aaltat force-pushed the positional_only branch 2 times, most recently from 0ec7c8b to a510179 Compare March 27, 2023 19:26
Copy link
Member

@pekkaklarck pekkaklarck left a comment

Choose a reason for hiding this comment

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

Looks very good but looking for the actual / value should be moved to DynamicArgumentParser from the common base class. I also need to look at tests and docs in a bit more detail later.

for arg in argspec:
arg = self._validate_arg(arg)
if arg == "/":
positional_only = False
continue
Copy link
Member

Choose a reason for hiding this comment

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

ArgumentSpecParser is a common base class for UserKeywordArgumentParser and DynamicArgumentParser. Checking for actual values, in this case /, should be done in them. I believe it would be best to add new _is_positional_only_separator method and then use it in the for arg in argspec loop. The usage could probably be something like this (untested):

if self._is_positional_only_separator(arg):
    if positional_only_separator_seen:
        self._report_error(...)
    spec.positional_only, spec.positional_or_named = spec.positional_or_named, []
    positional_only_separator_seen = True

The positional_only_separator_seen would need to be initialized in the same place as named_only.

Copy link
Contributor Author

@aaltat aaltat Apr 1, 2023

Choose a reason for hiding this comment

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

I agree, this will result cleaner code. I made the changes.

@@ -165,6 +174,8 @@ def _validate_arg(self, arg):
if len(arg) == 1:
return arg[0]
return arg
if "/" in arg and len(arg) > 1:
self._report_error(f'Invalid argument "{arg}".')
Copy link
Member

Choose a reason for hiding this comment

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

I understand the idea to make /arg invalid syntax, but this check makes using / anywhere in the argument name invalid. That could be a good idea as well, but it's also to some extend backwards incompatible. Although the risk that this change would actually cause issues is small, it may still not be worth the effort. On the other hand, we could consider limiting argument names also otherwise, but that would require a separate issue. We can discuss all this further in Slack.

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 am happy to going either way, I do not feel strongly on the matter should /arg or a/rg be invalid or not. Lets discuss in Slack.

In latest commit, did leave it as is, but it is easy to change once agreement is reached.

Copy link
Member

@pekkaklarck pekkaklarck left a comment

Choose a reason for hiding this comment

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

Looks great! There are few minor things to be changed (for example, this shouldn't require Python 3.8) but I can take care of that myself.

@pekkaklarck pekkaklarck merged commit 72ef3c7 into robotframework:master Apr 3, 2023
@aaltat aaltat deleted the positional_only branch April 3, 2023 13:55
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.

Dynamic API: Support positional-only arguments
3 participants