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

starlark: tweak how UnpackArgs enforces optional args #545

Merged
merged 1 commit into from
May 7, 2024

Conversation

nicks
Copy link
Contributor

@nicks nicks commented May 6, 2024

Fixes #544

Copy link
Collaborator

@adonovan adonovan left a comment

Choose a reason for hiding this comment

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

Thanks for the fix!

"x", &x, "y?", &y, "z", &z); err != nil {
t.Errorf("UnpackArgs failed: %v", err)
}
if x != 1 {
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can achieve higher vertical density (and, in this case, readability) in both the code and the test log by combining all three in one line:

got := fmt.Sprint("x=%d y=%d z=%d", x, y, z)
want := "x=1 y=2 z=0"
...assert...

starlark/unpack.go Show resolved Hide resolved
starlark/unpack.go Show resolved Hide resolved
@@ -164,12 +163,15 @@ kwloop:
}

// Check that all non-optional parameters are defined.
// (We needn't check the first len(args).)
for i := len(args); i < nparams; i++ {
for i := 0; i < nparams; i++ {
name := pairs[2*i].(string)
if strings.HasSuffix(name, "?") {
break // optional
Copy link
Collaborator

Choose a reason for hiding this comment

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

The doc comment says "If a parameter is marked optional, then all following parameters are implicitly optional whether or not they are marked." In hindsight we should probably have disallowed non-optionals after optionals, but that would be a breaking change now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

agreed

Fixes google#544

Signed-off-by: Nick Santos <nick.santos@docker.com>
Copy link
Collaborator

@adonovan adonovan left a comment

Choose a reason for hiding this comment

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

Hi Nick, thanks for sending this fix. LGTM.

@adonovan adonovan merged commit 35fe9f2 into google:master May 7, 2024
13 checks passed
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.

UnpackArgs has different behavior for args vs kwargs
2 participants