Skip to content

Commit dafb523

Browse files
committed
Merge pull request #86 from python-modernize/85-test-fix-import
Tests for fix_import; also change the fixer to add 'from __future__ import absolute_import' even if the 2to3 fixer makes no changes.
2 parents edd83f8 + 32baf14 commit dafb523

File tree

5 files changed

+58
-4
lines changed

5 files changed

+58
-4
lines changed

libmodernize/fixes/fix_import.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from __future__ import absolute_import
22

33
from lib2to3.fixes import fix_import
4+
from lib2to3.fixer_util import syms
45
import libmodernize
56

67

@@ -11,8 +12,13 @@ class FixImport(fix_import.FixImport):
1112
run_order = 1
1213

1314
def transform(self, node, results):
14-
results = super(FixImport, self).transform(node, results)
15-
if results is None:
15+
if self.skip:
1616
return
17+
# We're not interested in __future__ imports here
18+
if node.type == syms.import_from \
19+
and getattr(results['imp'], 'value', None) == '__future__':
20+
return
21+
22+
# If there are any non-future imports, add absolute_import
1723
libmodernize.add_future(node, 'absolute_import')
18-
return results
24+
return super(FixImport, self).transform(node, results)

libmodernize/fixes/fix_int_long_tuple.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from lib2to3 import fixer_base
44
from lib2to3 import fixer_util
5+
from libmodernize import touch_import
56

67

78
class FixIntLongTuple(fixer_base.BaseFix):
@@ -20,6 +21,6 @@ def transform(self, node, results):
2021
name = results['name']
2122
name.replace(fixer_util.Name('int', prefix=name.prefix))
2223
else:
23-
fixer_util.touch_import(None, 'six', node)
24+
touch_import(None, 'six', node)
2425
pair = results['pair']
2526
pair.replace(fixer_util.Name('six.integer_types', prefix=pair.prefix))

tests/test_fix_import.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from __future__ import absolute_import
2+
3+
from utils import check_on_input
4+
5+
6+
NO_IMPORTS = ("""\
7+
""", """\
8+
""")
9+
10+
ONLY_FUTURE_IMPORTS = ("""\
11+
from __future__ import print_function
12+
""", """\
13+
from __future__ import print_function
14+
""")
15+
16+
ONLY_NORMAL_IMPORTS = ("""\
17+
import foo
18+
""", """\
19+
from __future__ import absolute_import
20+
import foo
21+
""")
22+
23+
NORMAL_AND_FUTURE_IMPORTS = ("""\
24+
from __future__ import print_function
25+
import foo
26+
""", """\
27+
from __future__ import print_function
28+
from __future__ import absolute_import
29+
import foo
30+
""")
31+
32+
33+
def test_no_imports():
34+
check_on_input(*NO_IMPORTS)
35+
36+
def test_only_future_imports():
37+
check_on_input(*ONLY_FUTURE_IMPORTS)
38+
39+
def test_only_normal_imports():
40+
check_on_input(*ONLY_NORMAL_IMPORTS)
41+
42+
def test_normal_and_future_imports():
43+
check_on_input(*NORMAL_AND_FUTURE_IMPORTS)

tests/test_fix_imports_six.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import ConfigParser
1818
ConfigParser.ConfigParser()
1919
""", """\
20+
from __future__ import absolute_import
2021
import six.moves.configparser
2122
six.moves.configparser.ConfigParser()
2223
""")
@@ -25,6 +26,7 @@
2526
from ConfigParser import ConfigParser
2627
ConfigParser()
2728
""", """\
29+
from __future__ import absolute_import
2830
from six.moves.configparser import ConfigParser
2931
ConfigParser()
3032
""")

tests/test_fix_int_long_tuple.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66
INT_LONG_ISINSTANCE = ("""\
77
isinstance(1, (int, long))
88
""", """\
9+
from __future__ import absolute_import
910
import six
1011
isinstance(1, six.integer_types)
1112
""")
1213

1314
LONG_INT_ISINSTANCE = ("""\
1415
isinstance(1, (long, int))
1516
""", """\
17+
from __future__ import absolute_import
1618
import six
1719
isinstance(1, six.integer_types)
1820
""")

0 commit comments

Comments
 (0)