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

Subscript formatting fails when element is parenthesized #5733

Closed
konstin opened this issue Jul 13, 2023 · 0 comments · Fixed by #5882
Closed

Subscript formatting fails when element is parenthesized #5733

konstin opened this issue Jul 13, 2023 · 0 comments · Fixed by #5882
Labels
bug Something isn't working formatter Related to the formatter

Comments

@konstin
Copy link
Member

konstin commented Jul 13, 2023

def f(x):
    x[(1) :: ]

fails to format:

$ cargo run --bin ruff_python_formatter -- --emit stdout scratch.py
Error: Failed to format node

Caused by:
    syntax error

I passes when removing the parentheses around the 1, so this is somehow caused by incorrect parentheses handling.

def f(x):
    x[1 :: ]
@konstin konstin added bug Something isn't working formatter Related to the formatter labels Jul 13, 2023
konstin added a commit that referenced this issue Jul 19, 2023
**Summary** Fix the formatter crash with `x[(1) :: ]` and related code.

**Problem** For assigning comments in slices in subscripts, we need to find the positions of the colons to assign comments before and after the colon to the respective lower/upper/step node (or dangling in that section). Formatting `x[(1) :: ]` was broken because we were looking for a `:` after the `1` but didn't consider that there could be a `)` outside the range of the lower node, which contains just the `1` and no optional parentheses.

**Solution** Use the simple tokenizer directly and skip all closing parentheses.

**Test Plan** I added regression tests.

Closes #5733
konstin added a commit that referenced this issue Jul 19, 2023
**Summary** Fix the formatter crash with `x[(1) :: ]` and related code.

**Problem** For assigning comments in slices in subscripts, we need to find the positions of the colons to assign comments before and after the colon to the respective lower/upper/step node (or dangling in that section). Formatting `x[(1) :: ]` was broken because we were looking for a `:` after the `1` but didn't consider that there could be a `)` outside the range of the lower node, which contains just the `1` and no optional parentheses.

**Solution** Use the simple tokenizer directly and skip all closing parentheses.

**Test Plan** I added regression tests.

Closes #5733
konstin added a commit that referenced this issue Jul 19, 2023
**Summary** Fix the formatter crash with `x[(1) :: ]` and related code.

**Problem** For assigning comments in slices in subscripts, we need to find the positions of the colons to assign comments before and after the colon to the respective lower/upper/step node (or dangling in that section). Formatting `x[(1) :: ]` was broken because we were looking for a `:` after the `1` but didn't consider that there could be a `)` outside the range of the lower node, which contains just the `1` and no optional parentheses.

**Solution** Use the simple tokenizer directly and skip all closing parentheses.

**Test Plan** I added regression tests.

Closes #5733
konstin added a commit that referenced this issue Jul 19, 2023
**Summary** Fix the formatter crash with `x[(1) :: ]` and related code.

**Problem** For assigning comments in slices in subscripts, we need to find the positions of the colons to assign comments before and after the colon to the respective lower/upper/step node (or dangling in that section). Formatting `x[(1) :: ]` was broken because we were looking for a `:` after the `1` but didn't consider that there could be a `)` outside the range of the lower node, which contains just the `1` and no optional parentheses.

**Solution** Use the simple tokenizer directly and skip all closing parentheses.

**Test Plan** I added regression tests.

Closes #5733
konstin added a commit that referenced this issue Jul 19, 2023
**Summary** Fix the formatter crash with `x[(1) :: ]` and related code.

**Problem** For assigning comments in slices in subscripts, we need to
find the positions of the colons to assign comments before and after the
colon to the respective lower/upper/step node (or dangling in that
section). Formatting `x[(1) :: ]` was broken because we were looking for
a `:` after the `1` but didn't consider that there could be a `)`
outside the range of the lower node, which contains just the `1` and no
optional parentheses.

**Solution** Use the simple tokenizer directly and skip all closing
parentheses.

**Test Plan** I added regression tests.

Closes #5733
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working formatter Related to the formatter
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant