File tree Expand file tree Collapse file tree 2 files changed +73
-0
lines changed Expand file tree Collapse file tree 2 files changed +73
-0
lines changed Original file line number Diff line number Diff line change @@ -60,6 +60,12 @@ def add_future(node, symbol):
60
60
61
61
import_ = fixer_util .FromImport ('__future__' ,
62
62
[Leaf (token .NAME , symbol , prefix = " " )])
63
+
64
+ # If we're inserting as the first element, ensure any shebang prefix is maintained.
65
+ if idx == 0 and node .prefix .startswith ('#!' ):
66
+ import_ .prefix = node .prefix
67
+ node .prefix = ''
68
+
63
69
children = [import_ , fixer_util .Newline ()]
64
70
root .insert_child (idx , Node (syms .simple_stmt , children ))
65
71
Original file line number Diff line number Diff line change 29
29
import foo
30
30
""" )
31
31
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
+ """ )
32
87
33
88
def test_no_imports ():
34
89
check_on_input (* NO_IMPORTS )
@@ -41,3 +96,15 @@ def test_only_normal_imports():
41
96
42
97
def test_normal_and_future_imports ():
43
98
check_on_input (* NORMAL_AND_FUTURE_IMPORTS )
99
+
100
+ def test_import_with_docstring ():
101
+ check_on_input (* DOCSTRING )
102
+
103
+ def test_import_with_shebang ():
104
+ check_on_input (* SHEBANG )
105
+
106
+ def test_import_with_docstring_and_shebang ():
107
+ check_on_input (* DOCSTING_AND_SHEBANG )
108
+
109
+ def test_import_with_copyright_and_shebang ():
110
+ check_on_input (* COPYRIGHT_AND_SHEBANG )
You can’t perform that action at this time.
0 commit comments