Skip to content

Commit

Permalink
fix e402 fixed method when import some modules (for #447)
Browse files Browse the repository at this point in the history
  • Loading branch information
hhatto committed Oct 30, 2018
1 parent 92e6a66 commit 4343c41
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
12 changes: 10 additions & 2 deletions autopep8.py
Expand Up @@ -873,11 +873,19 @@ def fix_e401(self, result):
def fix_e402(self, result):
(line_index, offset, target) = get_index_offset_contents(result,
self.source)
for i in range(1, 100):
line = "".join(self.source[line_index:line_index+i])
try:
generate_tokens("".join(line))
except (SyntaxError, tokenize.TokenError):
continue
break
if not (target in self.imports and self.imports[target] != line_index):
mod_offset = get_module_imports_on_top_of_file(self.source,
line_index)
self.source[mod_offset] = target + self.source[mod_offset]
self.source[line_index] = ''
self.source[mod_offset] = line + self.source[mod_offset]
for offset in range(i):
self.source[line_index+offset] = ''

def fix_long_line_logically(self, result, logical):
"""Try to make lines fit within --max-line-length characters."""
Expand Down
23 changes: 23 additions & 0 deletions test/test_autopep8.py
Expand Up @@ -2416,6 +2416,29 @@ def test_e402_duplicate_module(self):
with autopep8_context(line) as result:
self.assertEqual(fixed, result)

def test_e402_import_some_modules(self):
line = """\
a = 1
from csv import (
reader,
writer,
)
import os
print(os, reader, writer)
import os
"""
fixed = """\
import os
from csv import (
reader,
writer,
)
a = 1
print(os, reader, writer)
"""
with autopep8_context(line) as result:
self.assertEqual(fixed, result)

def test_e501_basic(self):
line = """\
Expand Down

0 comments on commit 4343c41

Please sign in to comment.