-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid moving back the lexer for triple-quoted fstring (#11939)
## Summary This PR avoids moving back the lexer for a triple-quoted f-string during the re-lexing phase. The reason this is a problem is that for a triple-quoted f-string the newlines are part of the f-string itself, specifically they'll be part of the `FStringMiddle` token. So, if we moved the lexer back, there would be a `Newline` token whose range would be in between an `FStringMiddle` token. This creates a panic in downstream usage. fixes: #11937 ## Test Plan Add test cases and validate the snapshots.
- Loading branch information
1 parent
22733cb
commit ed948ea
Showing
10 changed files
with
377 additions
and
156 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
crates/ruff_python_parser/resources/invalid/re_lexing/triple_quoted_fstring_1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# There are trailing whitespace before the newline character but those whitespaces are | ||
# part of the comment token. | ||
# https://github.com/astral-sh/ruff/issues/11929 | ||
|
||
f"""hello {x # comment | ||
y = 1 |
6 changes: 6 additions & 0 deletions
6
crates/ruff_python_parser/resources/invalid/re_lexing/triple_quoted_fstring_2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# The lexer can't be moved back for a triple-quoted f-string because the newlines are | ||
# part of the f-string itself. | ||
# https://github.com/astral-sh/ruff/issues/11937 | ||
|
||
f'''{foo:.3f | ||
''' |
7 changes: 7 additions & 0 deletions
7
crates/ruff_python_parser/resources/invalid/re_lexing/triple_quoted_fstring_3.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Here, the nesting level is 2 when the parser is trying to recover from an unclosed `{` | ||
# This test demonstrates that we need to reduce the nesting level when recovering from | ||
# within an f-string but the lexer shouldn't go back. | ||
|
||
if call(f'''{x:.3f | ||
''' | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.