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

Infinite loop with invalid syntax #10295

Closed
jc-louis opened this issue Mar 8, 2024 · 1 comment · Fixed by #10315
Closed

Infinite loop with invalid syntax #10295

jc-louis opened this issue Mar 8, 2024 · 1 comment · Fixed by #10315
Labels
bug Something isn't working fixes Related to suggested fixes for violations help wanted Contributions especially welcome

Comments

@jc-louis
Copy link

jc-louis commented Mar 8, 2024

With this invalid syntax

a = (1 or)

I get with ruff v0.3.1

error: Failed to converge after 100 iterations.

This indicates a bug in Ruff. If you could open an issue at:

    https://github.com/astral-sh/ruff/issues/new?title=%5BInfinite%20loop%5D

...quoting the contents of `test.py`, the rule codes E225, E275, E999, along with the `pyproject.toml` settings and executed command, we'd be very appreciative!

error: Failed to parse test.py:1:10: Unexpected token ')'
test.py:1:8: E225 Missing whitespace around operator
  |
1 | a = (1 or)
  |        ^^ E225
  |
  = help: Add missing whitespace

test.py:1:8: E275 Missing whitespace after keyword
  |
1 | a = (1 or)
  |        ^^ E275
  |
  = help: Added missing whitespace after keyword

test.py:1:10: E999 SyntaxError: Unexpected token ')'
  |
1 | a = (1 or)
  |          ^ E999
  |

Found 153 errors (150 fixed, 3 remaining).
[*] 2 fixable with the `--fix` option.

Not sure if it is really a bug given the invalid syntax, but the output could be nicer.

@dhruvmanila
Copy link
Member

I think it's because the auto-fix for E225 tries to add a whitespace after or making it a = (1 or ) but then there's E202 which tries to remove the whitespace before ). This makes it an infinite loop.

@dhruvmanila dhruvmanila added bug Something isn't working fixes Related to suggested fixes for violations labels Mar 8, 2024
@MichaReiser MichaReiser added the help wanted Contributions especially welcome label Mar 8, 2024
hoel-bagard added a commit to hoel-bagard/ruff that referenced this issue Mar 9, 2024
hoel-bagard added a commit to hoel-bagard/ruff that referenced this issue Mar 9, 2024
charliermarsh pushed a commit that referenced this issue Mar 11, 2024
…is a ')' (#10315)

## Summary

Fixes #10295.

`E225` (`Missing whitespace around operator`) and `E275` (`Missing
whitespace after keyword`) try to add a white space even when the next
character is a `)` (which is a syntax error in most cases, the
exceptions already being handled). This causes `E202` (`Whitespace
before close bracket`) to try to remove the added whitespace, resulting
in an infinite loop when `E225`/`E275` re-add it.
This PR adds an exception in `E225` and `E275` to not trigger in case
the next token is a `)`. It is a bit simplistic, but it solves the
example given in the issue without introducing a change in behavior
(according to the fixtures).

## Test Plan

`cargo test` and the `ruff-ecosystem` check were used to check that the
PR's changes do not have side-effects.
A new fixture was added to check that running the 3 rules on the example
given in the issue does not cause ruff to fail to converge.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixes Related to suggested fixes for violations help wanted Contributions especially welcome
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants