Skip to content

Commit 0e8ca68

Browse files
authored
Merge pull request #1165 from PyCQA/w605-fstrings
3.12+ handle W605 for fstrings
2 parents 69f822b + bb8ed07 commit 0e8ca68

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

pycodestyle.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1620,32 +1620,32 @@ def python_3000_invalid_escape_sequence(logical_line, tokens, noqa):
16201620
'U',
16211621
]
16221622

1623-
for token_type, text, start, end, line in tokens:
1624-
if token_type == tokenize.STRING:
1625-
start_line, start_col = start
1626-
quote = text[-3:] if text[-3:] in ('"""', "'''") else text[-1]
1623+
prefixes = []
1624+
for token_type, text, start, _, _ in tokens:
1625+
if token_type in {tokenize.STRING, FSTRING_START}:
16271626
# Extract string modifiers (e.g. u or r)
1628-
quote_pos = text.index(quote)
1629-
prefix = text[:quote_pos].lower()
1630-
start = quote_pos + len(quote)
1631-
string = text[start:-len(quote)]
1627+
prefixes.append(text[:text.index(text[-1])].lower())
16321628

1633-
if 'r' not in prefix:
1634-
pos = string.find('\\')
1629+
if token_type in {tokenize.STRING, FSTRING_MIDDLE}:
1630+
if 'r' not in prefixes[-1]:
1631+
start_line, start_col = start
1632+
pos = text.find('\\')
16351633
while pos >= 0:
16361634
pos += 1
1637-
if string[pos] not in valid:
1638-
line = start_line + string.count('\n', 0, pos)
1635+
if text[pos] not in valid:
1636+
line = start_line + text.count('\n', 0, pos)
16391637
if line == start_line:
1640-
col = start_col + len(prefix) + len(quote) + pos
1638+
col = start_col + pos
16411639
else:
1642-
col = pos - string.rfind('\n', 0, pos) - 1
1640+
col = pos - text.rfind('\n', 0, pos) - 1
16431641
yield (
16441642
(line, col - 1),
1645-
"W605 invalid escape sequence '\\%s'" %
1646-
string[pos],
1643+
f"W605 invalid escape sequence '\\{text[pos]}'"
16471644
)
1648-
pos = string.find('\\', pos + 1)
1645+
pos = text.find('\\', pos + 1)
1646+
1647+
if token_type in {tokenize.STRING, FSTRING_END}:
1648+
prefixes.pop()
16491649

16501650

16511651
########################################################################

testsuite/W60.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
with \_ somewhere
1616
in the middle
1717
"""
18+
#: W605:1:3
19+
f"\d"
1820
#: Okay
1921
regex = r'\.png$'
2022
regex = '\\.png$'

0 commit comments

Comments
 (0)