Skip to content

Commit

Permalink
Merge pull request #467 from Hanaasagi/fix/import-statement-moved-abo…
Browse files Browse the repository at this point in the history
…ve-docstring-by-E402

fix import statement moved above the module doc by E402
  • Loading branch information
hhatto authored Jan 13, 2019
2 parents 89886b4 + 59318cb commit 3addaa7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
18 changes: 18 additions & 0 deletions autopep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ class documentation for more information.
COMPARE_NEGATIVE_REGEX_THROUGH = re.compile(r'\b(not\s+in|is\s+not)\s')
BARE_EXCEPT_REGEX = re.compile(r'except\s*:')
STARTSWITH_DEF_REGEX = re.compile(r'^(async\s+def|def)\s.*\):')
DOCSTRING_START_REGEX = re.compile(r'^u?r?(?P<kind>["\']{3})')

EXIT_CODE_OK = 0
EXIT_CODE_ERROR = 1
Expand Down Expand Up @@ -1412,11 +1413,28 @@ def is_future_import(line):
return False

allowed_try_keywords = ('try', 'except', 'else', 'finally')
in_docstring = False
docstring_kind = '"""'
for cnt, line in enumerate(source):
if not in_docstring:
m = DOCSTRING_START_REGEX.match(line.lstrip())
if m is not None:
in_docstring = True
docstring_kind = m.group('kind')
remain = line[m.end(): m.endpos].rstrip()
if remain[-3:] == docstring_kind: # one line doc
in_docstring = False
continue
if in_docstring:
if line.rstrip()[-3:] == docstring_kind:
in_docstring = False
continue

if not line.rstrip():
continue
elif line.startswith('#'):
continue

if line.startswith('import ') or line.startswith('from '):
if cnt == import_line_index or is_future_import(line):
continue
Expand Down
16 changes: 15 additions & 1 deletion test/test_autopep8.py
Original file line number Diff line number Diff line change
Expand Up @@ -2422,6 +2422,20 @@ def test_e402_with_future_import(self):
with autopep8_context(line) as result:
self.assertEqual(fixed, result)

def test_e402_with_module_doc(self):
line1 = '"""\nmodule doc\n"""\na = 1\nimport os\n'
fixed1 = '"""\nmodule doc\n"""\nimport os\na = 1\n'
line2 = '# comment\nr"""\nmodule doc\n"""\na = 1\nimport os\n'
fixed2 = '# comment\nr"""\nmodule doc\n"""\nimport os\na = 1\n'
line3 = "u'''one line module doc'''\na = 1\nimport os\n"
fixed3 = "u'''one line module doc'''\nimport os\na = 1\n"
line4 = "'''\n\"\"\"\ndoc'''\na = 1\nimport os\n"
fixed4 = "'''\n\"\"\"\ndoc'''\nimport os\na = 1\n"
for line, fixed in [(line1, fixed1), (line2, fixed2),
(line3, fixed3), (line4, fixed4)]:
with autopep8_context(line) as result:
self.assertEqual(fixed, result)

def test_e402_import_some_modules(self):
line = """\
a = 1
Expand Down Expand Up @@ -4882,7 +4896,7 @@ def test_get_module_imports_case_of_autopep8(self):
target_line_index = 11
result = get_module_imports_on_top_of_file(line.splitlines(),
target_line_index)
self.assertEqual(result, 5)
self.assertEqual(result, 10)


class CommandLineTests(unittest.TestCase):
Expand Down

0 comments on commit 3addaa7

Please sign in to comment.