Skip to content

Commit 89434db

Browse files
committed
Have fix_open subclass fix_file
That guarantees that any open-related scenario is fixed. Closes #74
1 parent 638cba8 commit 89434db

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

libmodernize/fixes/fix_open.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1+
from libmodernize.fixes import fix_file
12
from lib2to3 import fixer_base
23
from lib2to3.fixer_util import touch_import
34

45

5-
class FixOpen(fixer_base.ConditionalFix):
6+
class FixOpen(fix_file.FixFile):
67

78
BM_compatible = True
89
order = "pre"
9-
skip_on = "io.open"
1010

1111
PATTERN = """
12-
power< 'open' trailer< '(' any+ ')' > >
12+
power< name=('open'|'file') trailer< '(' any+ ')' > any* >
1313
"""
1414

1515
def transform(self, node, results):
16-
if self.should_skip(node):
17-
return
1816
touch_import(u'io', u'open', node)
17+
if len(results['name']) == 1 and results['name'][0].value == 'file':
18+
results['name'] = results['name'][0]
19+
super(FixOpen, self).transform(node, results)

tests/test_fix_open.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,19 @@
88
open('some/path')
99
""")
1010

11+
FILE = ("""\
12+
file('some/path')
13+
""", """\
14+
from io import open
15+
open('some/path')
16+
""")
17+
1118

1219
def test_open():
1320
check_on_input(*OPEN, extra_flags=['-f', 'libmodernize.fixes.fix_open'])
1421

1522
def test_open_optional():
1623
check_on_input(OPEN[0], OPEN[0])
24+
25+
def test_file():
26+
check_on_input(*FILE, extra_flags=['-f', 'libmodernize.fixes.fix_open'])

0 commit comments

Comments
 (0)