Preserve comma with starred expr in subscript#10997
Preserve comma with starred expr in subscript#10997dhruvmanila merged 2 commits intodhruv/parserfrom
Conversation
CodSpeed Performance ReportMerging #10997 will not alter performanceComparing Summary
|
07e850c to
c836049
Compare
7ec230a to
a2e71a8
Compare
MichaReiser
left a comment
There was a problem hiding this comment.
We should add a few tests for this new syntax. Or are there pre-existing tests for this?
There are: https://github.com/astral-sh/ruff/blob/a2e71a8460b07761bd280eac3ab9dd4bd013ba92/crates/ruff_python_formatter/resources/test/fixtures/black/cases/pep_646.py although it seems there's none for trailing commas. I'll add them. |
| # https://peps.python.org/pep-0646/#change-2-args-as-a-typevartuple | ||
| def function_with_variadic_generics(*args: *tuple[int]): ... | ||
| def function_with_variadic_generics( | ||
| *args: *tuple[int], |
There was a problem hiding this comment.
Is it intentional that you test here what happens if you have a trailing parameter comma? Should it instead test if there's a trailing tuple comma (in the subscript?)
There was a problem hiding this comment.
Yes, in the presence of variadic generics, the parameter formatting shouldn't change even though this PR doesn't touch that part of the code. It's mostly to make sure of any future changes, probably not needed.
c836049 to
a0799ca
Compare
There have been some grammar changes in [PEP 646] which were not accounted for in the old parser. The new parser has been updated with the correct AST. This is the case when there's a starred expression inside a subscript expression like the following example: ```python data[*x] ``` This gives us the AST where the slice element is actually a tuple expression with one element (starred expression) instead of just a starred expression. Now, the formatter's current behavior is to always add a trailing comma in a tuple with a single element. This PR updates the formatter to use the "preserve" behavior in trailing comma as well. So, trailing comma will not be added in the above example and if there's a trailing comma in the above example, it'll be preserved. This retains the current behavior without the AST change. [PEP 646]: https://peps.python.org/pep-0646/#change-1-star-expressions-in-indexes
4beb382 to
4a382e5
Compare
|
## Summary There have been some grammar changes in [PEP 646] which were not accounted for in the old parser. The new parser has been updated with the correct AST. This is the case when there's a starred expression inside a subscript expression like the following example: ```python data[*x] ``` This gives us the AST where the slice element is actually a tuple expression with one element (starred expression) instead of just a starred expression. Now, the formatter's current behavior is to always add a trailing comma in a tuple with a single element. This PR updates the formatter to use the "preserve" behavior in trailing comma as well. So, trailing comma will not be added in the above example and if there's a trailing comma in the above example, it'll be preserved. This retains the current behavior without the AST change. [PEP 646]: https://peps.python.org/pep-0646/#change-1-star-expressions-in-indexes ## Test Plan Run `cargo insta test -p ruff_python_formatter` and make sure there are no changes.
Summary
There have been some grammar changes in PEP 646 which were not accounted for in the old parser. The new parser has been updated with the correct AST. This is the case when there's a starred expression inside a subscript expression like the following example:
This gives us the AST where the slice element is actually a tuple expression with one element (starred expression) instead of just a starred expression. Now, the formatter's current behavior is to always add a trailing comma in a tuple with a single element.
This PR updates the formatter to use the "preserve" behavior in trailing comma as well. So, trailing comma will not be added in the above example and if there's a trailing comma in the above example, it'll be preserved. This retains the current behavior without the AST change.
Test Plan
Run
cargo insta test -p ruff_python_formatterand make sure there are no changes.