Skip to content

Commit

Permalink
Merge pull request #580 from hhatto/fix-w605
Browse files Browse the repository at this point in the history
change: not convert raw string for w605 fixed method
  • Loading branch information
hhatto committed Dec 26, 2020
2 parents 55887dc + 91de31e commit 831fbe5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 56 deletions.
54 changes: 4 additions & 50 deletions autopep8.py
Expand Up @@ -1384,56 +1384,10 @@ def fix_w504(self, result):
next_line[next_line_indent:])

def fix_w605(self, result):
(line_index, _, target) = get_index_offset_contents(result,
self.source)
try:
tokens = list(generate_tokens(target))
except (SyntaxError, tokenize.TokenError):
return
for (pos, _msg) in get_w605_position(tokens):
if target[pos - 1] == "r":
# ignore special case
if self.options.verbose:
print("invalid line: line_number={}, line: {}".format(
line_index + 1, target))
return
self.source[line_index] = '{}r{}'.format(
target[:pos], target[pos:])


def get_w605_position(tokens):
"""workaround get pointing out position by W605."""
# TODO: When this PR(*) change is released, use pos of pycodestyle
# *: https://github.com/PyCQA/pycodestyle/pull/747
valid = [
'\n', '\\', '\'', '"', 'a', 'b', 'f', 'n', 'r', 't', 'v',
'0', '1', '2', '3', '4', '5', '6', '7', 'x',

# Escape sequences only recognized in string literals
'N', 'u', 'U',
]

for token_type, text, start_pos, _end_pos, _line in tokens:
if token_type == tokenize.STRING:
quote = text[-3:] if text[-3:] in ('"""', "'''") else text[-1]
# Extract string modifiers (e.g. u or r)
quote_pos = text.index(quote)
prefix = text[:quote_pos].lower()
start = quote_pos + len(quote)
string = text[start:-len(quote)]

if 'r' not in prefix:
pos = string.find('\\')
while pos >= 0:
pos += 1
if string[pos] not in valid:
yield (
# No need to search line, token stores position
start_pos[1],
"W605 invalid escape sequence '\\%s'" %
string[pos],
)
pos = string.find('\\', pos + 1)
(line_index, offset, target) = get_index_offset_contents(result,
self.source)
self.source[line_index] = '{}\\{}'.format(
target[:offset + 1], target[offset + 1:])


def get_module_imports_on_top_of_file(source, import_line_index):
Expand Down
12 changes: 6 additions & 6 deletions test/test_autopep8.py
Expand Up @@ -5037,32 +5037,32 @@ def test_w604_with_multiple_lines(self):

def test_w605_simple(self):
line = "escape = '\\.jpg'\n"
fixed = "escape = r'\\.jpg'\n"
fixed = "escape = '\\\\.jpg'\n"
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)

def test_w605_identical_token(self):
# ***NOTE***: The --pep8-passes option is required to prevent an infinite loop in
# the old, failing code. DO NOT REMOVE.
line = "escape = foo('\\.bar', '\\.kilroy')\n"
fixed = "escape = foo(r'\\.bar', r'\\.kilroy')\n"
fixed = "escape = foo('\\\\.bar', '\\\\.kilroy')\n"
with autopep8_context(line, options=['--aggressive', '--pep8-passes', '5']) as result:
self.assertEqual(fixed, result, "Two tokens get r added")

line = "escape = foo('\\.bar', r'\\.kilroy')\n"
fixed = "escape = foo(r'\\.bar', r'\\.kilroy')\n"
line = "escape = foo('\\.bar', '\\\\.kilroy')\n"
fixed = "escape = foo('\\\\.bar', '\\\\.kilroy')\n"
with autopep8_context(line, options=['--aggressive', '--pep8-passes', '5']) as result:
self.assertEqual(fixed, result, "r not added if already there")

# Test Case to catch bad behavior reported in Issue #449
line = "escape = foo('\\.bar', '\\.bar')\n"
fixed = "escape = foo(r'\\.bar', r'\\.bar')\n"
fixed = "escape = foo('\\\\.bar', '\\\\.bar')\n"
with autopep8_context(line, options=['--aggressive', '--pep8-passes', '5']) as result:
self.assertEqual(fixed, result)

def test_w605_with_invalid_syntax(self):
line = "escape = rr'\\.jpg'\n"
fixed = "escape = rr'\\.jpg'\n"
fixed = "escape = rr'\\\\.jpg'\n"
with autopep8_context(line, options=['--aggressive']) as result:
self.assertEqual(fixed, result)

Expand Down

0 comments on commit 831fbe5

Please sign in to comment.