Skip to content

Commit df7eab7

Browse files
committed
Greatly simplify fix_open
Use run ordering to prevent having to subclass.
1 parent 89434db commit df7eab7

File tree

2 files changed

+11
-19
lines changed

2 files changed

+11
-19
lines changed

libmodernize/fixes/fix_open.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
from libmodernize.fixes import fix_file
21
from lib2to3 import fixer_base
32
from lib2to3.fixer_util import touch_import
43

54

6-
class FixOpen(fix_file.FixFile):
5+
class FixOpen(fixer_base.BaseFix):
76

7+
run_order = 10 # Must run after fix_file.
88
BM_compatible = True
9-
order = "pre"
10-
9+
# Fixers don't directly stack, so make sure the 'file' case is covered.
1110
PATTERN = """
12-
power< name=('open'|'file') trailer< '(' any+ ')' > any* >
11+
power< ('open' | 'file') trailer< '(' any+ ')' > >
1312
"""
1413

1514
def transform(self, node, results):
1615
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: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,21 @@
22

33

44
OPEN = ("""\
5-
open('some/path')
6-
""", """\
7-
from io import open
8-
open('some/path')
9-
""")
10-
11-
FILE = ("""\
12-
file('some/path')
5+
{0}('some/path')
136
""", """\
147
from io import open
158
open('some/path')
169
""")
1710

1811

1912
def test_open():
20-
check_on_input(*OPEN, extra_flags=['-f', 'libmodernize.fixes.fix_open'])
13+
check_on_input(OPEN[0].format('open'), OPEN[1],
14+
extra_flags=['-f', 'libmodernize.fixes.fix_open'])
2115

2216
def test_open_optional():
23-
check_on_input(OPEN[0], OPEN[0])
17+
check_on_input(OPEN[0].format('open'), OPEN[0].format('open'))
2418

2519
def test_file():
26-
check_on_input(*FILE, extra_flags=['-f', 'libmodernize.fixes.fix_open'])
20+
flags = ['-f', 'libmodernize.fixes.fix_open',
21+
'-f', 'libmodernize.fixes.fix_file']
22+
check_on_input(OPEN[0].format('file'), OPEN[1], extra_flags=flags)

0 commit comments

Comments
 (0)