Skip to content

Don't reorder parameters in function calls#7268

Merged
konstin merged 2 commits intomainfrom
dont-reorder-parameters-in-function-calls
Sep 13, 2023
Merged

Don't reorder parameters in function calls#7268
konstin merged 2 commits intomainfrom
dont-reorder-parameters-in-function-calls

Conversation

@konstin
Copy link
Copy Markdown
Member

@konstin konstin commented Sep 11, 2023

Summary

In f(*args, a=b, *args2, **kwargs) the args (*args, *args2) and keywords (a=b, **kwargs) are interleaved, which we previously didn't handle.

Fixes #6498

main

project similarity index total files changed files
cpython 0.76083 1789 1632
django 0.99966 2760 58
transformers 0.99930 2587 447
twine 1.00000 33 0
typeshed 0.99983 3496 18
warehouse 0.99825 648 22
zulip 0.99950 1437 27

PR

project similarity index total files changed files
cpython 0.76083 1789 1632
django 0.99967 2760 53
transformers 0.99930 2587 447
twine 1.00000 33 0
typeshed 0.99983 3496 18
warehouse 0.99825 648 22
zulip 0.99950 1437 27

Test Plan

New fixtures


group(&format_inner).fmt(f)
}),
&format_with(|f| format_function_header(f, item)),
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.

This isn't really part of the PR but i got confused so i split this into it's own function (no functional changes). It's a separate commit already i can easily split the branch if you want

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.

I think I would prefer if you write it as (I'm somewhat certain that this should work):

fn format_function_header(item: &StmtFunctionDef) -> impl Format<PyFormatContext<'_>> {
	format_with(move |f| {
		... 
	})
}

It avoids the nested function call.

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.

i prefer the flatter function body

Comment thread crates/ruff_python_formatter/src/other/arguments.rs Outdated
@konstin konstin added the formatter Related to the formatter label Sep 11, 2023
Copy link
Copy Markdown
Member

@MichaReiser MichaReiser left a comment

Choose a reason for hiding this comment

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

Can you add the similarity index comparison between the baseline and this PR?


group(&format_inner).fmt(f)
}),
&format_with(|f| format_function_header(f, item)),
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.

I think I would prefer if you write it as (I'm somewhat certain that this should work):

fn format_function_header(item: &StmtFunctionDef) -> impl Format<PyFormatContext<'_>> {
	format_with(move |f| {
		... 
	})
}

It avoids the nested function call.

Comment thread crates/ruff_python_formatter/src/other/arguments.rs Outdated
Comment thread crates/ruff_python_formatter/src/other/arguments.rs Outdated
Comment thread crates/ruff_python_formatter/src/other/arguments.rs Outdated
Comment thread crates/ruff_python_formatter/src/other/arguments.rs Outdated
@konstin konstin force-pushed the dont-reorder-parameters-in-function-calls branch from 8b4a36a to 78867ef Compare September 12, 2023 09:48
@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.

@konstin konstin force-pushed the dont-reorder-parameters-in-function-calls branch from 78867ef to f6e0492 Compare September 12, 2023 09:49
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Sep 12, 2023

PR Check Results

Ecosystem

✅ ecosystem check detected no changes.

@charliermarsh charliermarsh changed the title Dont reorder parameters in function calls Don't reorder parameters in function calls Sep 12, 2023
@konstin konstin changed the base branch from main to arg-or-keyword September 12, 2023 12:58
@konstin konstin force-pushed the dont-reorder-parameters-in-function-calls branch 2 times, most recently from f394565 to 492cd7a Compare September 12, 2023 13:43
@konstin konstin marked this pull request as draft September 12, 2023 13:44
@konstin konstin force-pushed the dont-reorder-parameters-in-function-calls branch from 492cd7a to d374956 Compare September 12, 2023 14:37
Comment on lines +530 to +531
*args, # d
# c
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.

these get switched but that seems fine to me

@konstin konstin marked this pull request as ready for review September 12, 2023 15:19
Base automatically changed from arg-or-keyword to main September 13, 2023 08:45
konstin added a commit that referenced this pull request Sep 13, 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:

```python
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 konstin force-pushed the dont-reorder-parameters-in-function-calls branch from d374956 to b30e268 Compare September 13, 2023 08:46
@konstin konstin enabled auto-merge (squash) September 13, 2023 08:46
@konstin konstin merged commit f4c7bff into main Sep 13, 2023
@konstin konstin deleted the dont-reorder-parameters-in-function-calls branch September 13, 2023 09:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

formatter Related to the formatter

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Formatter: Don't reorder parameters in function calls

2 participants