Skip to content

Commit 6fbd8c3

Browse files
committed
Handle parenthesised future imports
Modernising code that did from __future__ import (...) was failing. I couldn't get the test for this to fail - I'm not sure why. But this change made it work on the actual code I was testing with.
1 parent ad24ae2 commit 6fbd8c3

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

libmodernize/__init__.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,15 @@ def check_future_import(node):
1919
node.children[1].type == token.NAME and
2020
node.children[1].value == u'__future__'):
2121
return set()
22-
node = node.children[3]
22+
23+
if node.children[3].type == token.LPAR:
24+
# from __future__ import (..
25+
node = node.children[4]
26+
else:
27+
# from __future__ import ...
28+
node = node.children[3]
2329
# now node is the import_as_name[s]
30+
2431
# print(python_grammar.number2symbol[node.type])
2532
if node.type == syms.import_as_names:
2633
result = set()

tests/test_future_behaviour.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,20 @@ def test_problematic_file():
111111
print("abc")
112112
""")
113113

114+
FUTURE_IMPORT_PAREN = ("""\
115+
from __future__ import (print_function, division)
116+
print("abc")
117+
""", """\
118+
from __future__ import (print_function, division)
119+
print("abc")
120+
"""
121+
)
122+
114123
def test_future_import_as():
115124
check_on_input(*FUTURE_IMPORT_AS)
116125

117126
def test_future_import_as_multiple():
118127
check_on_input(*FUTURE_IMPORT_AS_MULTIPLE)
128+
129+
def test_future_import_paren():
130+
check_on_input(*FUTURE_IMPORT_PAREN)

0 commit comments

Comments
 (0)