Skip to content

Commit cb8402a

Browse files
committed
Make W605 point to the invalid sequence
Instead of having W605 point to the beginning of the string literal, make it point to the precise line and column of the invalid escape sequence. This is more helpful when you have multiline string literals.
1 parent b49f5e7 commit cb8402a

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

pycodestyle.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,7 +1522,7 @@ def python_3000_invalid_escape_sequence(logical_line, tokens):
15221522

15231523
for token_type, text, start, end, line in tokens:
15241524
if token_type == tokenize.STRING:
1525-
orig_start = start
1525+
start_line, start_col = start
15261526
quote = text[-3:] if text[-3:] in ('"""', "'''") else text[-1]
15271527
# Extract string modifiers (e.g. u or r)
15281528
quote_pos = text.index(quote)
@@ -1535,8 +1535,13 @@ def python_3000_invalid_escape_sequence(logical_line, tokens):
15351535
while pos >= 0:
15361536
pos += 1
15371537
if string[pos] not in valid:
1538+
line = start_line + string[:pos].count('\n')
1539+
if line == start_line:
1540+
col = start_col + len(prefix) + len(quote) + pos
1541+
else:
1542+
col = pos - string.rfind('\n', 0, pos) - 1
15381543
yield (
1539-
orig_start,
1544+
(line, col - 1),
15401545
"W605 invalid escape sequence '\\%s'" %
15411546
string[pos],
15421547
)

testsuite/W60.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,23 @@
1313
x = 0
1414
#: W604
1515
val = `1 + 2`
16-
#: W605:1:9
16+
#: W605:1:10
1717
regex = '\.png$'
18-
#: W605:1:9
18+
#: W605:2:1
1919
regex = '''
2020
\.png$
2121
'''
22-
#: W605:2:5
22+
#: W605:2:6
2323
f(
2424
'\_'
2525
)
26+
#: W605:4:6
27+
"""
28+
multi-line
29+
literal
30+
with \_ somewhere
31+
in the middle
32+
"""
2633
#: Okay
2734
regex = r'\.png$'
2835
regex = '\\.png$'

0 commit comments

Comments
 (0)