Skip to content

Commit 5e6ba63

Browse files
committed
Merge pull request #67 from brettcannon/fix_open
Add an opt-in fixer for open() -> io.open()
2 parents e5d1e29 + 8dbb154 commit 5e6ba63

File tree

4 files changed

+45
-5
lines changed

4 files changed

+45
-5
lines changed

libmodernize/fixes/__init__.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,9 @@
3939
'libmodernize.fixes.fix_unicode_type',
4040
'libmodernize.fixes.fix_xrange_six',
4141
'libmodernize.fixes.fix_zip',
42-
])
42+
])
43+
44+
# Fixes that are opt-in only.
45+
opt_in_fix_names = set([
46+
'libmodernize.fixes.fix_open',
47+
])

libmodernize/fixes/fix_open.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from lib2to3 import fixer_base
2+
from lib2to3.fixer_util import touch_import
3+
4+
5+
class FixOpen(fixer_base.ConditionalFix):
6+
7+
BM_compatible = True
8+
order = "pre"
9+
skip_on = "io.open"
10+
11+
PATTERN = """
12+
power< 'open' trailer< '(' any+ ')' > >
13+
"""
14+
15+
def transform(self, node, results):
16+
if self.should_skip(node):
17+
return
18+
touch_import(u'io', u'open', node)

libmodernize/main.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from lib2to3.main import warn, StdoutRefactoringTool
88
from lib2to3 import refactor
99

10-
from libmodernize.fixes import lib2to3_fix_names, six_fix_names
10+
from libmodernize.fixes import lib2to3_fix_names, six_fix_names, opt_in_fix_names
1111

1212

1313
def main(args=None):
@@ -81,6 +81,7 @@ def main(args=None):
8181

8282
# Initialize the refactoring tool
8383
unwanted_fixes = set(options.nofix)
84+
default_fixes = avail_fixes.difference(opt_in_fix_names)
8485

8586
# Remove unicode fixers depending on command line options
8687
if options.six_unicode:
@@ -101,9 +102,9 @@ def main(args=None):
101102
all_present = True
102103
else:
103104
explicit.add(fix)
104-
requested = avail_fixes.union(explicit) if all_present else explicit
105+
requested = default_fixes.union(explicit) if all_present else explicit
105106
else:
106-
requested = avail_fixes.union(explicit)
107+
requested = default_fixes
107108
fixer_names = requested.difference(unwanted_fixes)
108109
rt = StdoutRefactoringTool(sorted(fixer_names), flags, sorted(explicit),
109110
options.nobackups, not options.no_diffs)
@@ -118,7 +119,7 @@ def main(args=None):
118119
options.processes)
119120
except refactor.MultiprocessingUnsupported:
120121
assert options.processes > 1
121-
print("Sorry, -j isn't supported on this platform.",
122+
print("Sorry, -j isn't supported on this platform.",
122123
file=sys.stderr)
123124
return 1
124125
rt.summarize()

tests/test_fix_open.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from utils import check_on_input
2+
3+
4+
OPEN = ("""\
5+
open('some/path')
6+
""", """\
7+
from io import open
8+
open('some/path')
9+
""")
10+
11+
12+
def test_open():
13+
check_on_input(*OPEN, extra_flags=['-f', 'libmodernize.fixes.fix_open'])
14+
15+
def test_open_optional():
16+
check_on_input(OPEN[0], OPEN[0])

0 commit comments

Comments
 (0)