-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Rule W605 cause panic #10434
Comments
Probably this is due to I'm now thinking of adding new token which matches only |
@dhruvmanila any chance this is fixed in the new parser. I'm asking because you made some changes to string lexing/parsing |
No, this isn't fixed in the new parser. Although I might know why the problem is occurring. I'll need to look at the A minimal way to reproduce this is using the following code: # The curly brace escape is important
f"{{}}+-\d" And running it using: $ cargo run -- check --select W605 --no-cache --preview --output-format full --isolated ~/playground/ruff/src/play.py
/Users/dhruv/playground/ruff/src/play.py:1:7: W605 [*] Invalid escape sequence: `\d`
|
1 | f"{{}}+-\d"
| ^^ W605
|
= help: Use a raw string literal
Found 1 error.
[*] 1 fixable with the `--fix` option. As you can see, the highlighted range is incorrect. This creates other errors like for example if there's a newline in the string, which is a valid escape sequence, the fix can't be to prefix the f-string with $ cargo run -- check --select W605 --fix --diff --no-cache --preview --output-format full --isolated ~/playground/ruff/src/play.py
--- /Users/dhruv/playground/ruff/src/play.py
+++ /Users/dhruv/playground/ruff/src/play.py
@@ -1 +1 @@
-f"\n{{}}+-\d+"
+f"\n{{}\}\+\\\-\d+"
Would fix 5 errors. And, when you include the weird character in the mix, you get the panic: f"\n{{}}�+-\d+" And, we can even reproduce it in a way that the fix never converges. |
I can look at it tonight. |
## Summary This PR fixes a panic in the linter for `W605`. Consider the following f-string: ```python f"{{}}ab" ``` The `FStringMiddle` token would contain `{}ab`. Notice that the escaped braces have _reduced_ the string. This means we cannot use the text value from the token to determine the location of the escape sequence but need to extract it from the source code. fixes: #10434 ## Test Plan Add new test cases and update the snapshots.
ruff 0.3.3
(latest changes from main branch)
file content:
error
python_compressed.zip
The text was updated successfully, but these errors were encountered: