mirror of
https://github.com/astral-sh/ruff.git
synced 2025-09-29 13:25:17 +00:00

## 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.
59 lines
753 B
Python
59 lines
753 B
Python
# 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}"}"
|
||
|
||
# Regression tests for https://github.com/astral-sh/ruff/issues/10434
|
||
f"{{}}+-\d"
|
||
f"\n{{}}+-\d+"
|
||
f"\n{{}}<EFBFBD>+-\d+"
|