-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update
W605
to check in f-strings (#7329)
This PR updates the `W605` (invalid-escape-sequence) to check inside f-strings. It also adds support to report violation on invalid escape sequence within f-strings w.r.t. the curly braces. So, the following cases will be identified: ```python f"\{1}" f"\{{1}}" f"{1:\}" ``` The new CPython parser also gives out a syntax warning for such cases: ``` fstring.py:1: SyntaxWarning: invalid escape sequence '\{' f"\{1}" fstring.py:2: SyntaxWarning: invalid escape sequence '\{' f"\{{1}}" fstring.py:3: SyntaxWarning: invalid escape sequence '\}' f"{1:\}" ``` Nested f-strings are supported here, so the generated fix is aware of that and will create an edit for the proper f-string. Add new test cases for f-strings. fixes: #7295
- Loading branch information
1 parent
70fba75
commit 9c75fa3
Showing
5 changed files
with
355 additions
and
29 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
crates/ruff_linter/resources/test/fixtures/pycodestyle/W605_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,54 @@ | ||
# Same as `W605_0.py` but using f-strings instead. | ||
|
||
#: W605:1:10 | ||
regex = f'\.png$' | ||
|
||
#: W605:2:1 | ||
regex = f''' | ||
\.png$ | ||
''' | ||
|
||
#: W605:2:6 | ||
f( | ||
f'\_' | ||
) | ||
|
||
#: W605:4:6 | ||
f""" | ||
multi-line | ||
literal | ||
with \_ somewhere | ||
in the middle | ||
""" | ||
|
||
#: W605:1:38 | ||
value = f'new line\nand invalid escape \_ here' | ||
|
||
|
||
#: Okay | ||
regex = fr'\.png$' | ||
regex = f'\\.png$' | ||
regex = fr''' | ||
\.png$ | ||
''' | ||
regex = fr''' | ||
\\.png$ | ||
''' | ||
s = f'\\' | ||
regex = f'\w' # noqa | ||
regex = f''' | ||
\w | ||
''' # noqa | ||
|
||
regex = f'\\\_' | ||
value = f'\{{1}}' | ||
value = f'\{1}' | ||
value = f'{1:\}' | ||
value = f"{f"\{1}"}" | ||
value = rf"{f"\{1}"}" | ||
|
||
# Okay | ||
value = rf'\{{1}}' | ||
value = rf'\{1}' | ||
value = rf'{1:\}' | ||
value = f"{rf"\{1}"}" |
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.