Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 2c10fa5

Browse filesBrowse files
committed
Add --all-imports option to futurize. Now by default again fewer imports are added
1 parent c7694cd commit 2c10fa5
Copy full SHA for 2c10fa5

3 files changed

+25-35Lines changed: 25 additions & 35 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎libfuturize/fixes2/__init__.py‎

Copy file name to clipboardExpand all lines: libfuturize/fixes2/__init__.py
+2-4Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@
3636
'lib2to3.fixes.fix_xreadlines',
3737
])
3838

39-
# The following fixers add a dependency on the ``future`` package:
39+
# The following fixers add a dependency on the ``future`` package on order to
40+
# support Python 2:
4041
lib2to3_fix_names_stage2 = set([
4142
'lib2to3.fixes.fix_basestring',
4243
# 'lib2to3.fixes.fix_buffer', # perhaps not safe. Test this.
@@ -64,7 +65,6 @@
6465
])
6566

6667
libfuturize_2fix_names_stage1 = set([
67-
'libfuturize.fixes2.fix_add__future__imports_except_unicode_literals', # we force adding all imports for now, even if they don't seem necessary, for ease of testing
6868
'libfuturize.fixes2.fix_absolute_import',
6969
'libfuturize.fixes2.fix_division',
7070
'libfuturize.fixes2.fix_print_with_import',
@@ -73,8 +73,6 @@
7373
])
7474

7575
libfuturize_2fix_names_stage2 = set([
76-
'libfuturize.fixes2.fix_add__future__imports',
77-
'libfuturize.fixes2.fix_add_future_standard_library_import', # we force adding this import for now, even if it doesn't seem necessary to the fix_future_standard_library fixer, for ease of testing
7876
'libfuturize.fixes2.fix_future_builtins',
7977
'libfuturize.fixes2.fix_future_standard_library',
8078
'libfuturize.fixes2.fix_metaclass',
Collapse file

‎libfuturize/fixes2/fix_unicode_literals_import.py‎

Copy file name to clipboardExpand all lines: libfuturize/fixes2/fix_unicode_literals_import.py
-12Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,6 @@
33
44
from __future__ import unicode_literals
55
6-
7-
Based on lib2to3/fixes/fix_import.py
8-
9-
If spam is being imported from the local directory, this import:
10-
from spam import eggs
11-
becomes:
12-
from .spam import eggs
13-
14-
and this import:
15-
import spam
16-
becomes:
17-
from . import spam
186
"""
197

208
from lib2to3 import fixer_base
Collapse file

‎libfuturize/main.py‎

Copy file name to clipboardExpand all lines: libfuturize/main.py
+23-19Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ def main(args=None):
8484
"""
8585
# Set up option parser
8686
parser = optparse.OptionParser(usage="futurize [options] file|dir ...")
87+
parser.add_option("-a", "--all-imports", action="store_true",
88+
help="Adds all __future__ and future imports to each module")
8789
parser.add_option("-d", "--doctests_only", action="store_true",
8890
help="Fix up doctests only")
8991
parser.add_option("-b", "--tobytes", action="store_true",
@@ -128,14 +130,11 @@ def main(args=None):
128130
if options.from3:
129131
assert not (options.stage1 or options.stage2)
130132
fixer_pkg = 'libfuturize.fixes3'
131-
# avail_fixes = set(refactor.get_fixers_from_package(fixer_pkg))
132-
# avail_fixes.update(libfuturize_3fix_names)
133133
avail_fixes = libfuturize_3fix_names
134134
flags["print_function"] = True
135135
else:
136136
fixer_pkg = 'libfuturize.fixes2'
137137
avail_fixes = set()
138-
# avail_fixes = set(refactor.get_fixers_from_package(fixer_pkg))
139138
if not (options.stage1 or options.stage2):
140139
options.both_stages = True
141140
else:
@@ -168,7 +167,9 @@ def main(args=None):
168167
if options.write:
169168
print("Can't write to stdin.", file=sys.stderr)
170169
return 2
171-
# Is this ever needed?
170+
171+
# If this option were ever needed, it would probably mean the --from3 flag
172+
# had been forgotten.
172173
# if options.print_function:
173174
# flags["print_function"] = True
174175

@@ -177,24 +178,27 @@ def main(args=None):
177178
logging.basicConfig(format='%(name)s: %(message)s', level=level)
178179

179180
# Initialize the refactoring tool
180-
# unwanted_fixes = set(options.nofix)
181-
unwanted_fixes = set(fixer_pkg + ".fix_" + fix for fix in options.nofix)
181+
nofix = set(options.nofix)
182+
183+
# The 'all-imports' option forces adding all imports __future__ and "from
184+
# future import standard_library", even if they don't seem necessary for
185+
# the current state of each module. (This can simplify testing, and can
186+
# reduce the need to think about Py2 compatibility when editing the code
187+
# further.)
188+
if options.all_imports:
189+
if options.stage1:
190+
nofix.add('fix_add__future__imports_except_unicode_literals')
191+
else:
192+
# In case the user hasn't run stage1 for some reason:
193+
nofix.add('fix_add__future__imports')
194+
nofix.add('fix_add_future_standard_library_import')
195+
196+
unwanted_fixes = set(fixer_pkg + ".fix_" + fix for fix in nofix)
182197

183198
# Remove all fixes except one if the input is already Py3
184199
explicit = set()
185-
# if options.fix:
186-
# all_present = False
187-
# for fix in options.fix:
188-
# if fix == "all":
189-
# all_present = True
190-
# else:
191-
# explicit.add(fixer_pkg + ".fix_" + fix)
192-
# # explicit.add(fix)
193-
# requested = avail_fixes.union(explicit) if all_present else explicit
194-
# else:
195-
# requested = avail_fixes.union(explicit)
196-
requested = avail_fixes
197-
fixer_names = requested.difference(unwanted_fixes)
200+
fixer_names = avail_fixes.difference(unwanted_fixes)
201+
198202
rt = StdoutRefactoringTool(sorted(fixer_names), flags, sorted(explicit),
199203
options.nobackups, not options.no_diffs)
200204

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.