Skip to content

Commit 2400d08

Browse files
committed
Merge pull request #123 from jeremyh/master
Import statements should be placed after the shebang.
2 parents 8b21240 + aa29024 commit 2400d08

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

libmodernize/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ def add_future(node, symbol):
6060

6161
import_ = fixer_util.FromImport('__future__',
6262
[Leaf(token.NAME, symbol, prefix=" ")])
63+
64+
# Place after any comments or whitespace. (copyright, shebang etc.)
65+
import_.prefix = node.prefix
66+
node.prefix = ''
67+
6368
children = [import_, fixer_util.Newline()]
6469
root.insert_child(idx, Node(syms.simple_stmt, children))
6570

tests/test_fix_import.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,82 @@
2929
import foo
3030
""")
3131

32+
DOCSTRING = ("""\
33+
\"""
34+
Docstring
35+
\"""
36+
import foo
37+
""", """\
38+
\"""
39+
Docstring
40+
\"""
41+
from __future__ import absolute_import
42+
import foo
43+
""")
44+
45+
SHEBANG = ("""\
46+
#!/usr/bin/env python
47+
import foo
48+
""", """\
49+
#!/usr/bin/env python
50+
from __future__ import absolute_import
51+
import foo
52+
""")
53+
54+
DOCSTING_AND_SHEBANG = ("""\
55+
#!/usr/bin/env python
56+
\"""
57+
Docstring
58+
\"""
59+
import foo
60+
""", """\
61+
#!/usr/bin/env python
62+
\"""
63+
Docstring
64+
\"""
65+
from __future__ import absolute_import
66+
import foo
67+
""")
68+
69+
COPYRIGHT_AND_SHEBANG = ("""\
70+
#!/usr/bin/env python
71+
72+
#
73+
# Copyright notice
74+
#
75+
76+
import foo
77+
""", """\
78+
#!/usr/bin/env python
79+
80+
#
81+
# Copyright notice
82+
#
83+
84+
from __future__ import absolute_import
85+
import foo
86+
""")
87+
88+
89+
COPYRIGHT_AND_DOCSTRING = ("""\
90+
#
91+
# Copyright notice
92+
#
93+
94+
\"""Docstring\"""
95+
96+
import foo
97+
""", """\
98+
#
99+
# Copyright notice
100+
#
101+
102+
\"""Docstring\"""
103+
104+
from __future__ import absolute_import
105+
import foo
106+
""")
107+
32108

33109
def test_no_imports():
34110
check_on_input(*NO_IMPORTS)
@@ -41,3 +117,18 @@ def test_only_normal_imports():
41117

42118
def test_normal_and_future_imports():
43119
check_on_input(*NORMAL_AND_FUTURE_IMPORTS)
120+
121+
def test_import_with_docstring():
122+
check_on_input(*DOCSTRING)
123+
124+
def test_import_with_shebang():
125+
check_on_input(*SHEBANG)
126+
127+
def test_import_with_docstring_and_shebang():
128+
check_on_input(*DOCSTING_AND_SHEBANG)
129+
130+
def test_import_with_copyright_and_shebang():
131+
check_on_input(*COPYRIGHT_AND_SHEBANG)
132+
133+
def test_import_with_copyright_and_docstring():
134+
check_on_input(*COPYRIGHT_AND_DOCSTRING)

0 commit comments

Comments
 (0)