Skip to content

Introduce ArgOrKeyword to keep call parameter order#7302

Merged
konstin merged 2 commits intomainfrom
arg-or-keyword
Sep 13, 2023
Merged

Introduce ArgOrKeyword to keep call parameter order#7302
konstin merged 2 commits intomainfrom
arg-or-keyword

Conversation

@konstin
Copy link
Copy Markdown
Member

@konstin konstin commented Sep 12, 2023

Motivation

The ast::Arguments for call argument are split into positional arguments (args) and keywords arguments (keywords). We currently assume that call consists of first args and then keywords, which is generally the case, but not always:

f(*args, a=2, *args2, **kwargs)

class A(*args, a=2, *args2, **kwargs):
    pass

The consequence is accidentally reordering arguments (#7268).

Summary

Arguments::args_and_keywords returns an iterator of an ArgOrKeyword enum that yields args and keywords in the correct order. I've fixed the obvious args and keywords usages, but there might be some cases with wrong assumptions remaining.

Test Plan

The generator got new test cases, otherwise the stacked PR (#7268) which uncovered this.

@konstin
Copy link
Copy Markdown
Member Author

konstin commented Sep 12, 2023

Current dependencies on/for this PR:

This comment was auto-generated by Graphite.

Comment thread crates/ruff_python_ast/src/visitor.rs Outdated
}
for keyword in &arguments.keywords {
visitor.visit_keyword(keyword);
for arg_or_keyword in arguments.args_and_keywords() {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The ordering in this visitor should be the same as the execution order at runtime.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Input:

def f(*args, **kwargs):
    pass

def g(x):
    print(x)
    return x


f(*g([1]), a=g(2), *g([3]), **g({"4": 5}))

Output:

[1]
[3]
2
{'4': 5}

😵‍💫

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

thanks for making me check

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Sep 12, 2023

PR Check Results

Ecosystem

✅ ecosystem check detected no changes.

@konstin konstin marked this pull request as ready for review September 12, 2023 14:51
@konstin konstin requested a review from MichaReiser September 12, 2023 14:51
@MichaReiser MichaReiser added the internal An internal refactor or improvement label Sep 12, 2023
Comment thread crates/ruff_python_ast/src/nodes.rs Outdated
Comment thread crates/ruff_python_ast/src/helpers.rs Outdated
@konstin konstin enabled auto-merge (squash) September 13, 2023 08:34
@konstin konstin merged commit 56440ad into main Sep 13, 2023
@konstin konstin deleted the arg-or-keyword branch September 13, 2023 08:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

internal An internal refactor or improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants